Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add flow for checking previously used accounts #40

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,47 @@ internal class GoogleAuthUiProviderImpl(
GoogleAuthUiProvider {
override suspend fun signIn(): GoogleUser? {
return try {
val credential = credentialManager.getCredential(
context = activityContext,
request = getCredentialRequest()
).credential
getGoogleUserFromCredential(credential)
} catch (e: GetCredentialException) {
println("GoogleAuthUiProvider error: ${e.message}")
val shouldCheckLegacyAuthServices = when (e) {
is GetCredentialProviderConfigurationException -> true
is NoCredentialException -> true
is GetCredentialUnsupportedException -> true
else -> false
getGoogleUserFromCredential(filterByAuthorizedAccounts = true)
} catch (e: NoCredentialException) {
try {
getGoogleUserFromCredential(filterByAuthorizedAccounts = false)
} catch (e: GetCredentialException) {
handleCredentialException(e)
} catch (e: NullPointerException) {
null
}
if (shouldCheckLegacyAuthServices) checkLegacyGoogleSignIn()
else null
} catch (e: GetCredentialException) {
handleCredentialException(e)
} catch (e: NullPointerException) {
null
}
}

private suspend fun handleCredentialException(e: GetCredentialException): GoogleUser? {
println("GoogleAuthUiProvider error: ${e.message}")
val shouldCheckLegacyAuthServices = when (e) {
is GetCredentialProviderConfigurationException -> true
is NoCredentialException -> true
is GetCredentialUnsupportedException -> true
else -> false
}
return if (shouldCheckLegacyAuthServices) {
checkLegacyGoogleSignIn()
} else {
null
}
}

private suspend fun checkLegacyGoogleSignIn(): GoogleUser? {
println("GoogleAuthUiProvider: Checking Outdated Google Sign In...")
return googleLegacyAuthentication.signIn()
}

private fun getGoogleUserFromCredential(credential: Credential): GoogleUser? {
private suspend fun getGoogleUserFromCredential(filterByAuthorizedAccounts: Boolean): GoogleUser? {
val credential = credentialManager.getCredential(
context = activityContext,
request = getCredentialRequest(filterByAuthorizedAccounts)
).credential
return when {
credential is CustomCredential && credential.type == GoogleIdTokenCredential.TYPE_GOOGLE_ID_TOKEN_CREDENTIAL -> {
try {
Expand All @@ -70,15 +85,20 @@ internal class GoogleAuthUiProviderImpl(
}
}

private fun getCredentialRequest(): GetCredentialRequest {
private fun getCredentialRequest(filterByAuthorizedAccounts: Boolean): GetCredentialRequest {
return GetCredentialRequest.Builder()
.addCredentialOption(getGoogleIdOption(serverClientId = credentials.serverId))
.addCredentialOption(
getGoogleIdOption(
serverClientId = credentials.serverId,
filterByAuthorizedAccounts = filterByAuthorizedAccounts
)
)
.build()
}

private fun getGoogleIdOption(serverClientId: String): GetGoogleIdOption {
private fun getGoogleIdOption(serverClientId: String, filterByAuthorizedAccounts: Boolean): GetGoogleIdOption {
return GetGoogleIdOption.Builder()
.setFilterByAuthorizedAccounts(false)
.setFilterByAuthorizedAccounts(filterByAuthorizedAccounts)
.setAutoSelectEnabled(true)
.setServerClientId(serverClientId)
.build()
Expand Down