0

I want to log in using google, but I still get task.isSuccessful = false. I cannot go through this condition

I will also add that on firebase there is added SHA-1 and updated JSON file. Any suggestions on how to solve this? :)

My code

class GoogleAuth : StartActivity() {

    private lateinit var firebaseAuth: FirebaseAuth
    private lateinit var googleSignInClient: GoogleSignInClient

    private companion object {
        private const val RC_SIGN_IN = 100
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestIdToken(getString(R.string.web_client_token_id))
            .requestEmail()
            .build()

        googleSignInClient = GoogleSignIn.getClient(this, gso)

        googleSignIn()
    }

    /**Google SignIn*/
    private fun googleSignIn() {
        val signInIntent = googleSignInClient.signInIntent
        startActivityForResult(signInIntent, RC_SIGN_IN)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (requestCode == RC_SIGN_IN) {
            val task = GoogleSignIn.getSignedInAccountFromIntent(data)
            val exception = task.exception

            if (task.isSuccessful) {
                try {
                    // Google Sign In was successful, authenticate with Firebase
                    val account = task.getResult(ApiException::class.java)!!
                    Log.d("GoogleSignInActivity", "firebaseAuthWithGoogle:" + account.id)
                    firebaseAuthWithGoogle(account.idToken!!)
                } catch (e: ApiException) {
                    // Google Sign In failed, update UI appropriately
                    Log.w("GoogleSignInActivity", "Google sign in failed", e)
                }
            } else {
                Log.w("GoogleSignInActivity", exception.toString())
            }
        }
    }
}
Cerber
  • 63
  • 1
  • 5
  • That's the oldest way of implementing Firebase sign-in with Google, which will be at some point in time [deprecated](https://developers.google.com/identity/sign-in/android/sign-in-identity). Besides that, [OnActivityResult method is already deprecated](https://stackoverflow.com/questions/62671106/). To achieve what you want, I recommend use [One Tap](https://developers.google.com/identity/one-tap) and read this [article](https://medium.com/@alex.mamo/how-to-authenticate-to-firebase-using-google-one-tap-in-jetpack-compose-60b30e621d0d). – Alex Mamo May 18 '22 at 12:00
  • If you still want to keep it that way, have you tried to check the error message when the task is not successful? – Alex Mamo May 18 '22 at 12:01
  • https://github.com/firebase/snippets-android/blob/37a067ad4eda50fcbf464b1d1717119f5950f347/auth/app/src/main/java/com/google/firebase/quickstart/auth/kotlin/GoogleSignInActivity.kt – Crebain May 18 '22 at 12:20
  • If the task fails, you're logging the exception here: `Log.w("GoogleSignInActivity", exception.toString())`. If you look that up in the logcat output, it'll show you the cause of the failure. – Frank van Puffelen May 18 '22 at 13:26
  • I have now managed to move on, but now I want to save the user with firebaseAuth.signInWithCredential(), and this task failed... – Cerber May 18 '22 at 13:57
  • @Cerber What is the error message when the operation fails? – Alex Mamo May 19 '22 at 05:46

0 Answers0