Why does Firebase return "null" account after successful Google Play authentication?
class MyActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
[...]
val activity = this
val gso = GoogleSignInOptions
.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN)
.requestServerAuthCode("<some-id>.apps.googleusercontent.com")
.build()
val signInClient = GoogleSignIn.getClient(activity, gso)
val acc = GoogleSignIn.getLastSignedInAccount(activity) // This is null
val currentUser = Firebase.auth.currentUser // This is null
signInClient.silentSignIn().addOnCompleteListener(activity) { task ->
if (task.isSuccessful) {
val account = task.result // This is com.google.android.gms.auth.api.signin.GoogleSignInAccount@123456
account.displayName // This is null
account.id // This is null
Firebase.auth.currentUser // Still null
}
}
}
}
According to the docs user id should never be "null" if "isSuccessful" was triggered: https://firebase.google.com/docs/auth/android/play-games
2
Answers
Try using this:
instead of:
Indeed, you can only implement authentication with Google but if you want to also use Firebase products, then you should also authenticate in Firebase too. This means that you should follow these steps. First, sign in with Google. Once the authentication is successful, get the Google credentials and pass them to the FirebaseAuth#signInWithCredential(com.google.firebase.auth.AuthCredential) method. Please check below the documentation for One Tap sign-in and sign-up. And here is the documentation for implementing Firebase sign-in with Google.
Here is also a resource that might help, with the corresponding repo.
While the above solution will definitely work, please note that the One Tap sign-in will be soon deprecated. So you should consider Integrate Credential Manager with Sign in with Google.