mirzemehdi / KMPAuth

Kotlin Multiplatform Authentication Library targetting android and iOS
https://mirzemehdi.com/KMPAuth/
Apache License 2.0
231 stars 19 forks source link

Access token in Google Sign In is null #52

Open wanek21 opened 1 month ago

wanek21 commented 1 month ago

Sometimes, I can't retrieve the email from googleTokenId on my backend (explained here) so I need to send an additional request with the accessToken on the backend. The issue is that onGoogleSignInResult returns a null accessToken in the GoogleUser object:

GoogleButtonUiContainer(onGoogleSignInResult = { googleUser ->
        if (googleUser != null) {
                val idToken = googleUser.idToken
                println("id token: $idToken")
                println("access token: ${googleUser.accessToken}") // this is always null
            }
        }) {}

I checked the source code and found this lines (source):

GoogleUser(
    idToken = googleIdTokenCredential.idToken,
    accessToken = null,
    displayName = googleIdTokenCredential.displayName ?: "",
    profilePicUrl = googleIdTokenCredential.profilePictureUri?.toString()
)

So, the accessToken is always null. Perhaps I'm looking in the wrong place, and there’s another way to obtain the accessToken. Could you take a look at this, please?

P.S.: Thanks for the great library!

mirzemehdi commented 1 month ago

@wanek21 So here's what I found so far:

in ios side Google Sign in returns accessToken directly. But in android, looks like it doesn't provide accessToken directly, it provides idToken, which then you can use in the server side to get email. But as you pointed out sometimes this doesn't work.

You can check this one too: https://stackoverflow.com/questions/78911767/android-kotlin-google-credential-manager-get-access-token

What I found so far that, instead of using Google One Tap Sign in, we can use Google Legacy Sign in, and using requestServerAuthCode() would return serverAuthCode which can be changed in the server side with accessToken. Not sure exact details however.

Do you have any example code that you used in android side (not KMP) for solving this?