Skip to content

Commit

Permalink
set up google sign in
Browse files Browse the repository at this point in the history
  • Loading branch information
OkelloSam21 committed Feb 19, 2024
1 parent 774ac5e commit df3d697
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.samuelokello.kazihub.presentation.shared.authentication.SignIn.Google

import android.content.Context
import android.content.Intent
import android.content.IntentSender
import com.google.android.gms.auth.api.identity.BeginSignInRequest
import com.google.android.gms.auth.api.identity.BeginSignInRequest.GoogleIdTokenRequestOptions
import com.google.android.gms.auth.api.identity.SignInClient
import com.google.firebase.auth.GoogleAuthProvider
import com.google.firebase.auth.ktx.auth
import com.google.firebase.ktx.Firebase
import com.samuelokello.kazihub.R
import com.samuelokello.kazihub.presentation.shared.authentication.SignIn.SignInResult
import com.samuelokello.kazihub.presentation.shared.authentication.SignIn.UserData
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.tasks.await

class GoogleAuthUiClient(
private val context: Context,
private val oneTapClient: SignInClient
) {
private val auth = Firebase.auth

suspend fun signIn(): IntentSender? {
val result = try {
oneTapClient.beginSignIn(
buildSignInRequest()
).await()
} catch(e: Exception) {
e.printStackTrace()
if(e is CancellationException) throw e
null
}
return result?.pendingIntent?.intentSender
}

suspend fun signInWithIntent(intent: Intent): SignInResult {
val credential = oneTapClient.getSignInCredentialFromIntent(intent)
val googleIdToken = credential.googleIdToken
val googleCredentials = GoogleAuthProvider.getCredential(googleIdToken, null)
return try {
val user = auth.signInWithCredential(googleCredentials).await().user
SignInResult(
data = user?.run {
UserData(
userId = uid,
username = displayName,
profilePictureUrl = photoUrl?.toString()
)
},
errorMessage = null
)
} catch(e: Exception) {
e.printStackTrace()
if(e is CancellationException) throw e
SignInResult(
data = null,
errorMessage = e.message
)
}
}

suspend fun signOut() {
try {
oneTapClient.signOut().await()
auth.signOut()
} catch(e: Exception) {
e.printStackTrace()
if(e is CancellationException) throw e
}
}

fun getSignedInUser(): UserData? = auth.currentUser?.run {
UserData(
userId = uid,
username = displayName,
profilePictureUrl = photoUrl?.toString()
)
}

private fun buildSignInRequest(): BeginSignInRequest {
return BeginSignInRequest.Builder()
.setGoogleIdTokenRequestOptions(
GoogleIdTokenRequestOptions.builder()
.setSupported(true)
.setFilterByAuthorizedAccounts(false)
.setServerClientId(context.getString(R.string.web_client_id))
.build()
)
.setAutoSelectEnabled(true)
.build()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.samuelokello.kazihub.presentation.shared.authentication.SignIn

sealed interface SignInEvent {
data class onEmailChanged(val email: String): SignInEvent
data class onPasswordChanged(val password: String): SignInEvent
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.samuelokello.kazihub.presentation.shared.authentication.SignIn

data class SignInResult(
val data: UserData?,
val errorMessage: String?
)

data class UserData(
val userId: String,
val username: String?,
val profilePictureUrl: String?
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.samuelokello.kazihub.presentation.shared.authentication.SignIn

import androidx.lifecycle.ViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update

class SignInViewModel : ViewModel() {

private val _state = MutableStateFlow(SignInState())
val state = _state.asStateFlow()

fun onSignInResult(result: SignInResult) {
_state.update {
it.copy(
isSignInSuccessful = result.data != null,
signInError = result.errorMessage
)
}
}

fun resetState() {
_state.update { SignInState() }
}

fun onSignInClicked() {

}

fun onEvent(event: SignInEvent) {
when (event) {
is SignInEvent.onEmailChanged -> {
_state.update {
it.copy(email = event.email)
}
}

is SignInEvent.onPasswordChanged -> {
_state.update {
it.copy(password = event.password)
}
}
}
}
}

0 comments on commit df3d697

Please sign in to comment.