I don’t understand how to get a server auth code from the GetGoogleIdOption sign up/in flow. I understand that GetGoogleIdOption is used for authentication and the server auth code maybe comes from an authorization flow.
I read this guide and there are some details here, but I don’t know how to apply them practically.
From what I understand, authorization is used to let the user have access to a google service like Google Drive, not necessary for plain google sign up/in.
This is what I have currently,
val credentialManager = CredentialManager.create(context)
val rawNonce = UUID.randomUUID().toString()
val bytes = rawNonce.toByteArray()
val md = MessageDigest.getInstance("SHA-256")
val digest = md.digest(bytes)
val hashedNonce = digest.fold("") { str, it -> str + "%02x".format(it) }
val googleIdOption: GetGoogleIdOption = GetGoogleIdOption.Builder()
.setFilterByAuthorizedAccounts(false)
.setServerClientId(context.getString(R.string.web_client_id))
.setAutoSelectEnabled(false)
.setNonce(hashedNonce)
.build()
val request: GetCredentialRequest = GetCredentialRequest.Builder()
.addCredentialOption(googleIdOption)
.build()
scope.launch {
try {
val result = credentialManager.getCredential(
request = request,
context = context
)
val credential = result.credential
val googleIdTokenCredential =
GoogleIdTokenCredential.createFrom(credential.data)
val name = googleIdTokenCredential.displayName ?: "unavailable"
val email = googleIdTokenCredential.id
val googleIdToken = googleIdTokenCredential.idToken
val serverAuthCode = ""
val userId = ""
onEvent(SignUpEvents.OnLogInWithGoogle(
name = name,
email = email,
serverAuthCode = serverAuthCode,
googleTokenId = googleIdToken,
userId = userId
))
// Use the googleIdToken for further processing
} catch (e: GetCredentialCancellationException) {
// Handle the case where the user cancelled the operation
Log.e("GoogleSignIn", "User cancelled the sign-in process.")
} catch (e: GetCredentialException) {
// Handle other credential exceptions
Log.e("GoogleSignIn", "Credential error: ${e.message}")
Toast.makeText(context, "An error occurred", Toast.LENGTH_SHORT).show()
} catch (e: Exception) {
// Handle any other exceptions
Log.e("GoogleSignIn", "Unexpected error: ${e.message}")
Toast.makeText(context, "An error occurred", Toast.LENGTH_SHORT).show()
} finally {
signInWithGoogleLoading = false
}
}
The backend I’m working with requires a name, email, serverAuthCode, googleTokenId and userId. Then in return it sends back an accessToken and authorization token.
Any details to help me get on the right track would be appreciated!