1

I wish to do the google signIn auth with firebase and this could have been achieved this way but now this method onActivityResult is deprecated so what is the appropriate replacement for this please help me out

    const val REQUEST_CODE_SIGN_IN = 0

    class MainActivity : AppCompatActivity() {

        lateinit var auth: FirebaseAuth

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            auth = FirebaseAuth.getInstance()

            btnGoogleSignIn.setOnClickListener {
                val options = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                    .requestIdToken(getString(R.string.webclient_id))
                    .requestEmail()
                    .build()
                val signInClient = GoogleSignIn.getClient(this, options)
                signInClient.signInIntent.also {
                    startActivityForResult(it, REQUEST_CODE_SIGN_IN)
                }
            }
        }

        private fun googleAuthForFirebase(account: GoogleSignInAccount) {
            val credentials = GoogleAuthProvider.getCredential(account.idToken, null)
            CoroutineScope(Dispatchers.IO).launch {
                try {
                    auth.signInWithCredential(credentials).await()
                    withContext(Dispatchers.Main) {
                        Toast.makeText(this@MainActivity, "Successfully logged in", Toast.LENGTH_LONG).show()
                    }
                } catch(e: Exception) {
                    withContext(Dispatchers.Main) {
                        Toast.makeText(this@MainActivity, e.message, Toast.LENGTH_LONG).show()
                    }
                }
            }
        }

        override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
            super.onActivityResult(requestCode, resultCode, data)
            if(requestCode == REQUEST_CODE_SIGN_IN) {
                val account = GoogleSignIn.getSignedInAccountFromIntent(data).result
                account?.let {
                    googleAuthForFirebase(it)
                }
            }
        }
    }
  • If you want to implement Firebase sign-in with Google, then this [resource](https://medium.com/firebase-developers/how-to-authenticate-to-firebase-using-google-one-tap-in-jetpack-compose-60b30e621d0d) will definitely help. Here is the corresponding [repo](https://github.com/alexmamo/FirebaseSignInWithGoogle). – Alex Mamo Oct 25 '22 at 18:31

0 Answers0