I’m trying to code an Android app using Jetpack Compose.
I’ve been following a tutorial to let the users authenticate with Google and, up to a few days ago, everything was working just fine.
I’ve connected my Firebase project, enabled the authentication with Google and even set the SHA-1 key. At the same time, I’ve specified my web client id and addedd the google-services.json file to Android Studio.
A few days ago, when clicking the signin button, it would’ve shown the list of Google accounts on the device or, a form to log in with Google. Now it just throws the exception without displaying anything.
This the class I’m using:
class GoogleAuthClient(
private val context: Context,
private val oneTapClient: SignInClient
) {
private val auth = Firebase.auth
suspend fun <T> Task<T>.await(): T {
return suspendCancellableCoroutine { continuation ->
addOnCompleteListener {
if (it.isSuccessful) {
continuation.resume(it.result, null)
} else {
continuation.cancel(it.exception ?: Exception("Something went wrong during await"))
}
}
}
}
suspend fun signIn(): IntentSender? {
val result = try {
oneTapClient.beginSignIn(
buildSignInRequest()
).await()
} catch (e: Exception) {
e.printStackTrace()
if (e is CancellationException) throw e
null
}
return result?.pendingIntent?.intentSender
}
suspend fun signInWithIntent(intent: Intent): SignInResult {
val credential = oneTapClient.getSignInCredentialFromIntent(intent)
val googleIdToken = credential.googleIdToken
val googleCredentials = GoogleAuthProvider.getCredential(googleIdToken, null)
return try {
val user = auth.signInWithCredential(googleCredentials).await().user
SignInResult(data = user?.run {
UserData(
userId = uid,
username = displayName,
profilePicUrl = photoUrl?.toString()
)
}, errorMessage = null)
} catch (e: Exception) {
e.printStackTrace()
if (e is CancellationException) throw e
SignInResult(data = null, errorMessage = e.message)
}
}
private fun buildSignInRequest(): BeginSignInRequest {
return BeginSignInRequest.builder()
.setGoogleIdTokenRequestOptions(
GoogleIdTokenRequestOptions.builder()
.setSupported(true)
.setFilterByAuthorizedAccounts(false)
.setServerClientId(context.getString(R.string.def_web_client_id))
.build()
)
.setAutoSelectEnabled(true)
.build()
}
}
And this is the point in which I’m calling the class:
val viewModel = viewModel<SignInViewModel>()
val state by viewModel.state.collectAsState()
val launcher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.StartIntentSenderForResult(),
onResult = {result ->
if (result.resultCode == RESULT_OK) {
lifecycleOwner.lifecycleScope.launch {
val signInResult = authClient.signInWithIntent(
intent = result.data ?: return@launch
)
// Here we already got the result from the intent
viewModel.onSigninResult(signInResult)
if (signInResult.data != null)
navController.navigate(NavigationScreen.GoalSelection.name)
else
Toast.makeText(context, "Something went wrong during singin.", Toast.LENGTH_LONG)
}
}
}
)
GoogleSignInButton(
state = state,
onGoogleSignIn = {
lifecycleOwner.lifecycleScope.launch {
val signinIntentSender = authClient.signIn()
launcher.launch(
IntentSenderRequest.Builder(
signinIntentSender ?: return@launch
).build()
)
}
}
)
@Composable
fun GoogleSignInButton(
state: SignInState,
onSigninClick: () -> Unit
) {
val context = LocalContext.current
LaunchedEffect(key1 = state.signInError) {
state.signInError?.let { error ->
Toast.makeText(
context,
error,
Toast.LENGTH_LONG
).show()
}
}
Button(onClick = onSigninClick) {
Text(text = "Continue with Google")
}
}
At the moment the error is catched in the .await
function (called in signIn()
). At completiiton it
is valorized but isSuccessful
is false and the result contains the exception. It seems as if the exception is directly thrown by the Task.
I really can’t wrap my head around the fact that it was working just fine a few days ago.
I’ve tried putting breakpoints here and there. I’ve isolated the result of buildSignInRequest()
, hoping to find some null values. However, the result seemed correctly valorized and, all the previous steps didn’t give any problem.
I’ve also tried going back in the project history to find if there were any breaking changes but, nothing caught my eye.
Does anyone know what could be the problem here and how to fix it?
Zay is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.