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 1 commit
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 @@ -25,24 +25,43 @@ internal class GoogleAuthUiProviderImpl(
return try {
val credential = credentialManager.getCredential(
context = activityContext,
request = getCredentialRequest()
request = getCredentialRequest(checkPreviouslyUsedAccounts = true)
mirzemehdi marked this conversation as resolved.
Show resolved Hide resolved
).credential
getGoogleUserFromCredential(credential)
mirzemehdi marked this conversation as resolved.
Show resolved Hide resolved
} catch (e: GetCredentialException) {
println("GoogleAuthUiProvider error: ${e.message}")
val shouldCheckLegacyAuthServices = when (e) {
is GetCredentialProviderConfigurationException -> true
is NoCredentialException -> true
is GetCredentialUnsupportedException -> true
else -> false
} catch (e: NoCredentialException) {
try {
val credential = credentialManager.getCredential(
context = activityContext,
request = getCredentialRequest(checkPreviouslyUsedAccounts = false)
).credential
getGoogleUserFromCredential(credential)
} 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()
Expand Down Expand Up @@ -70,15 +89,20 @@ internal class GoogleAuthUiProviderImpl(
}
}

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

private fun getGoogleIdOption(serverClientId: String): GetGoogleIdOption {
private fun getGoogleIdOption(serverClientId: String, checkPreviouslyUsedAccounts: Boolean): GetGoogleIdOption {
mirzemehdi marked this conversation as resolved.
Show resolved Hide resolved
return GetGoogleIdOption.Builder()
.setFilterByAuthorizedAccounts(false)
.setFilterByAuthorizedAccounts(checkPreviouslyUsedAccounts)
.setAutoSelectEnabled(true)
.setServerClientId(serverClientId)
.build()
Expand Down