Fix the firebase login.
This commit is contained in:
		
							parent
							
								
									b38d3537f6
								
							
						
					
					
						commit
						acdeb6f542
					
				
					 2 changed files with 39 additions and 7 deletions
				
			
		| 
						 | 
					@ -70,7 +70,7 @@ class FirebaseRepository @Inject constructor(
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }.stateIn(
 | 
					    }.stateIn(
 | 
				
			||||||
        scope = CoroutineScope(Dispatchers.Default + Job()),
 | 
					        scope = CoroutineScope(Dispatchers.Default + Job()),
 | 
				
			||||||
        started = SharingStarted.Eagerly,
 | 
					        started = SharingStarted.Lazily,
 | 
				
			||||||
        initialValue = emptyMap()
 | 
					        initialValue = emptyMap()
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -102,7 +102,7 @@ class FirebaseRepository @Inject constructor(
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }.stateIn(
 | 
					    }.stateIn(
 | 
				
			||||||
        scope = CoroutineScope(Dispatchers.Default + Job()),
 | 
					        scope = CoroutineScope(Dispatchers.Default + Job()),
 | 
				
			||||||
        started = SharingStarted.Eagerly,
 | 
					        started = SharingStarted.Lazily,
 | 
				
			||||||
        initialValue = NetworkThrowMap(),
 | 
					        initialValue = NetworkThrowMap(),
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -7,10 +7,16 @@ import androidx.credentials.GetCredentialRequest
 | 
				
			||||||
import androidx.lifecycle.ViewModel
 | 
					import androidx.lifecycle.ViewModel
 | 
				
			||||||
import androidx.lifecycle.viewModelScope
 | 
					import androidx.lifecycle.viewModelScope
 | 
				
			||||||
import com.google.android.libraries.identity.googleid.GetSignInWithGoogleOption
 | 
					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.R
 | 
				
			||||||
 | 
					import com.pixelized.rplexicon.data.repository.authentication.FirebaseRepository
 | 
				
			||||||
import dagger.hilt.android.lifecycle.HiltViewModel
 | 
					import dagger.hilt.android.lifecycle.HiltViewModel
 | 
				
			||||||
import kotlinx.coroutines.Dispatchers
 | 
					import kotlinx.coroutines.Dispatchers
 | 
				
			||||||
import kotlinx.coroutines.launch
 | 
					import kotlinx.coroutines.launch
 | 
				
			||||||
 | 
					import kotlinx.coroutines.tasks.await
 | 
				
			||||||
import kotlinx.coroutines.withContext
 | 
					import kotlinx.coroutines.withContext
 | 
				
			||||||
import javax.inject.Inject
 | 
					import javax.inject.Inject
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -18,7 +24,9 @@ import javax.inject.Inject
 | 
				
			||||||
 * https://developer.android.com/identity/sign-in/credential-manager-siwg
 | 
					 * https://developer.android.com/identity/sign-in/credential-manager-siwg
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
@HiltViewModel
 | 
					@HiltViewModel
 | 
				
			||||||
class AuthenticationViewModel @Inject constructor() : ViewModel() {
 | 
					class AuthenticationViewModel @Inject constructor(
 | 
				
			||||||
 | 
					    private val firebaseRepository: FirebaseRepository,
 | 
				
			||||||
 | 
					) : ViewModel() {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    val authenticationState = mutableStateOf<AuthenticationStateUio>(
 | 
					    val authenticationState = mutableStateOf<AuthenticationStateUio>(
 | 
				
			||||||
        AuthenticationStateUio.Initial
 | 
					        AuthenticationStateUio.Initial
 | 
				
			||||||
| 
						 | 
					@ -36,14 +44,38 @@ class AuthenticationViewModel @Inject constructor() : ViewModel() {
 | 
				
			||||||
            .build()
 | 
					            .build()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        viewModelScope.launch(Dispatchers.IO) {
 | 
					        viewModelScope.launch(Dispatchers.IO) {
 | 
				
			||||||
            withContext(Dispatchers.Main) {
 | 
					 | 
				
			||||||
                authenticationState.value = AuthenticationStateUio.Initial
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            try {
 | 
					            try {
 | 
				
			||||||
                credentialManager.getCredential(
 | 
					                withContext(Dispatchers.Main) {
 | 
				
			||||||
 | 
					                    authenticationState.value = AuthenticationStateUio.Progress
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					                // login to goole.
 | 
				
			||||||
 | 
					                val result = credentialManager.getCredential(
 | 
				
			||||||
                    request = request,
 | 
					                    request = request,
 | 
				
			||||||
                    context = activity,
 | 
					                    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) {
 | 
					                withContext(Dispatchers.Main) {
 | 
				
			||||||
                    authenticationState.value = AuthenticationStateUio.Success
 | 
					                    authenticationState.value = AuthenticationStateUio.Success
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue