From acdeb6f542c58732689099d7a6d44af83c8e1ca0 Mon Sep 17 00:00:00 2001 From: "Andres Gomez, Thomas (ITDV RL)" Date: Wed, 5 Jun 2024 20:46:14 +0200 Subject: [PATCH] Fix the firebase login. --- .../authentication/FirebaseRepository.kt | 4 +- .../authentication/AuthenticationViewModel.kt | 42 ++++++++++++++++--- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/pixelized/rplexicon/data/repository/authentication/FirebaseRepository.kt b/app/src/main/java/com/pixelized/rplexicon/data/repository/authentication/FirebaseRepository.kt index 3712547..95f2b9a 100644 --- a/app/src/main/java/com/pixelized/rplexicon/data/repository/authentication/FirebaseRepository.kt +++ b/app/src/main/java/com/pixelized/rplexicon/data/repository/authentication/FirebaseRepository.kt @@ -70,7 +70,7 @@ class FirebaseRepository @Inject constructor( } }.stateIn( scope = CoroutineScope(Dispatchers.Default + Job()), - started = SharingStarted.Eagerly, + started = SharingStarted.Lazily, initialValue = emptyMap() ) @@ -102,7 +102,7 @@ class FirebaseRepository @Inject constructor( } }.stateIn( scope = CoroutineScope(Dispatchers.Default + Job()), - started = SharingStarted.Eagerly, + started = SharingStarted.Lazily, initialValue = NetworkThrowMap(), ) diff --git a/app/src/main/java/com/pixelized/rplexicon/ui/screens/authentication/AuthenticationViewModel.kt b/app/src/main/java/com/pixelized/rplexicon/ui/screens/authentication/AuthenticationViewModel.kt index 752642c..d4e82a3 100644 --- a/app/src/main/java/com/pixelized/rplexicon/ui/screens/authentication/AuthenticationViewModel.kt +++ b/app/src/main/java/com/pixelized/rplexicon/ui/screens/authentication/AuthenticationViewModel.kt @@ -7,10 +7,16 @@ import androidx.credentials.GetCredentialRequest import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.google.android.libraries.identity.googleid.GetSignInWithGoogleOption +import com.google.android.libraries.identity.googleid.GoogleIdTokenCredential +import com.google.firebase.auth.GoogleAuthProvider +import com.google.firebase.auth.ktx.auth +import com.google.firebase.ktx.Firebase import com.pixelized.rplexicon.R +import com.pixelized.rplexicon.data.repository.authentication.FirebaseRepository import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import kotlinx.coroutines.tasks.await import kotlinx.coroutines.withContext import javax.inject.Inject @@ -18,7 +24,9 @@ import javax.inject.Inject * https://developer.android.com/identity/sign-in/credential-manager-siwg */ @HiltViewModel -class AuthenticationViewModel @Inject constructor() : ViewModel() { +class AuthenticationViewModel @Inject constructor( + private val firebaseRepository: FirebaseRepository, +) : ViewModel() { val authenticationState = mutableStateOf( AuthenticationStateUio.Initial @@ -36,14 +44,38 @@ class AuthenticationViewModel @Inject constructor() : ViewModel() { .build() viewModelScope.launch(Dispatchers.IO) { - withContext(Dispatchers.Main) { - authenticationState.value = AuthenticationStateUio.Initial - } try { - credentialManager.getCredential( + withContext(Dispatchers.Main) { + authenticationState.value = AuthenticationStateUio.Progress + } + // login to goole. + val result = credentialManager.getCredential( request = request, context = activity, ) + // extract the token Id from the credential + val tokenIdCredential = GoogleIdTokenCredential.createFrom( + data = result.credential.data, + ) + // wrap the tokenId inside a google credential + val googleIdCredential = GoogleAuthProvider.getCredential( + tokenIdCredential.idToken, + null, + ) + // use that credential to login to firebase. + val firebase = Firebase.auth.signInWithCredential( + googleIdCredential + ).await() + // throw if firebase login fail. + if (firebase.credential == null) { + error("Fail to authenticate to firebase") + } + // Launch a preload collect to warm the repository flow. + // Can't use Eager for that because of auth permission. + launch(Dispatchers.Default) { + firebaseRepository.getAlterationStatus().collect { } + } + // propagate the auth success. withContext(Dispatchers.Main) { authenticationState.value = AuthenticationStateUio.Success }