Switch to google bottom sheet authentication.
This commit is contained in:
parent
f20fa023f6
commit
466855f1d7
1 changed files with 25 additions and 21 deletions
|
|
@ -6,7 +6,7 @@ import androidx.credentials.CredentialManager
|
||||||
import androidx.credentials.GetCredentialRequest
|
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.GetGoogleIdOption
|
||||||
import com.google.android.libraries.identity.googleid.GoogleIdTokenCredential
|
import com.google.android.libraries.identity.googleid.GoogleIdTokenCredential
|
||||||
import com.google.firebase.auth.GoogleAuthProvider
|
import com.google.firebase.auth.GoogleAuthProvider
|
||||||
import com.google.firebase.auth.ktx.auth
|
import com.google.firebase.auth.ktx.auth
|
||||||
|
|
@ -16,14 +16,13 @@ import com.pixelized.rplexicon.data.repository.authentication.FirebaseRepository
|
||||||
import com.pixelized.rplexicon.data.repository.character.CharacterSheetRepository
|
import com.pixelized.rplexicon.data.repository.character.CharacterSheetRepository
|
||||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.async
|
import kotlinx.coroutines.flow.collect
|
||||||
import kotlinx.coroutines.awaitAll
|
|
||||||
import kotlinx.coroutines.flow.firstOrNull
|
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.tasks.await
|
import kotlinx.coroutines.tasks.await
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* https://developer.android.com/identity/sign-in/credential-manager-siwg
|
* https://developer.android.com/identity/sign-in/credential-manager-siwg
|
||||||
*/
|
*/
|
||||||
|
|
@ -38,26 +37,31 @@ class AuthenticationViewModel @Inject constructor(
|
||||||
)
|
)
|
||||||
|
|
||||||
fun signIn(activity: Activity) {
|
fun signIn(activity: Activity) {
|
||||||
val credentialManager = CredentialManager.create(context = activity)
|
|
||||||
|
|
||||||
val signInWithGoogleOption: GetSignInWithGoogleOption = GetSignInWithGoogleOption
|
|
||||||
.Builder(serverClientId = activity.getString(R.string.google_sign_in_id))
|
|
||||||
.build()
|
|
||||||
|
|
||||||
val request: GetCredentialRequest = GetCredentialRequest.Builder()
|
|
||||||
.addCredentialOption(credentialOption = signInWithGoogleOption)
|
|
||||||
.build()
|
|
||||||
|
|
||||||
viewModelScope.launch(Dispatchers.IO) {
|
viewModelScope.launch(Dispatchers.IO) {
|
||||||
try {
|
try {
|
||||||
withContext(Dispatchers.Main) {
|
// create the credential manager
|
||||||
authenticationState.value = AuthenticationStateUio.Progress
|
val credentialManager = CredentialManager.create(
|
||||||
}
|
context = activity,
|
||||||
|
)
|
||||||
|
// build the google authentication bottom sheet.
|
||||||
|
val googleIdOption: GetGoogleIdOption = GetGoogleIdOption.Builder()
|
||||||
|
.setFilterByAuthorizedAccounts(false)
|
||||||
|
.setAutoSelectEnabled(true)
|
||||||
|
.setServerClientId(activity.getString(R.string.google_sign_in_id))
|
||||||
|
.build()
|
||||||
|
// build the request from the google Id.
|
||||||
|
val request: GetCredentialRequest = GetCredentialRequest.Builder()
|
||||||
|
.addCredentialOption(credentialOption = googleIdOption)
|
||||||
|
.build()
|
||||||
// login to google.
|
// login to google.
|
||||||
val result = credentialManager.getCredential(
|
val result = credentialManager.getCredential(
|
||||||
request = request,
|
request = request,
|
||||||
context = activity,
|
context = activity,
|
||||||
)
|
)
|
||||||
|
// the google UI Took care up to now of the scrim.
|
||||||
|
withContext(Dispatchers.Main) {
|
||||||
|
authenticationState.value = AuthenticationStateUio.Progress
|
||||||
|
}
|
||||||
// extract the token Id from the credential
|
// extract the token Id from the credential
|
||||||
val tokenIdCredential = GoogleIdTokenCredential.createFrom(
|
val tokenIdCredential = GoogleIdTokenCredential.createFrom(
|
||||||
data = result.credential.data,
|
data = result.credential.data,
|
||||||
|
|
@ -77,13 +81,13 @@ class AuthenticationViewModel @Inject constructor(
|
||||||
}
|
}
|
||||||
// Launch a preload collect to warm the repository flow.
|
// Launch a preload collect to warm the repository flow.
|
||||||
// Can't use Eager for that because of auth permission.
|
// Can't use Eager for that because of auth permission.
|
||||||
val firebaseRequest = async(Dispatchers.Default) {
|
launch(Dispatchers.Default) {
|
||||||
firebaseRepository.getAlterationStatus().firstOrNull()
|
firebaseRepository.getAlterationStatus().collect()
|
||||||
}
|
}
|
||||||
val sheetRequest = async(Dispatchers.Default) {
|
// prefetch the character sheet
|
||||||
|
withContext(Dispatchers.Default) {
|
||||||
sheetRepository.fetchCharacterSheet()
|
sheetRepository.fetchCharacterSheet()
|
||||||
}
|
}
|
||||||
awaitAll(firebaseRequest, sheetRequest)
|
|
||||||
// propagate the auth success.
|
// 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