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)
}
}
}
}