I ran into some strange issue with Google-Authentication through Firebase.
When I install it from google play store, google-authentication fails to work, but in the other hand when I install it locally from Android studio (same version), it does work.
the following is the authentication function -
private fun signInWithGoogle() {
// Configure Google Sign In
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build()
googleSignInClient = GoogleSignIn.getClient(requireContext(), gso)
val signInIntent = googleSignInClient.signInIntent
getResult.launch(signInIntent)
}
private val getResult =
registerForActivityResult(
ActivityResultContracts.StartActivityForResult()) {
if (it.resultCode == Activity.RESULT_OK) {
val task = GoogleSignIn.getSignedInAccountFromIntent(it.data)
if (task.isSuccessful) {
try {
// Google Sign In was successful, authenticate with Firebase
val account = task.getResult(ApiException::class.java)!!
firebaseAuthWithGoogle(account.idToken!!)
} catch (e: ApiException) {
// Google Sign In failed, update UI appropriately
}
} else {
println(task.exception?.message)
}
}
}
private fun firebaseAuthWithGoogle(idToken: String) {
val credential = GoogleAuthProvider.getCredential(idToken, null)
mAuth.signInWithCredential(credential)
.addOnCompleteListener { task ->
if (task.isSuccessful) {
// Sign in success, update UI with the signed-in user's information
println("signInWithCredential:success")
startActivity(Intent(activity, MainActivityApplication::class.java))
activity?.finish()
} else {
// If sign in fails, display a message to the user.
println("signInWithCredential:failure ${task.exception}")
}
}
.addOnFailureListener {
println(it.message)
binding.pbGoogleBtn.visibility= View.GONE
}
}