When the user wants to change his password, I prompt him with a Dialog to Re-authenticate. In this dialog, he can re-auth with password or via Google/Facebook. But when I update the firebase email, the user gets signed out and I want to avoid this. Consider this code:
private fun btnGoogle(){
val acct = GoogleSignIn.getLastSignedInAccount(context)
Timber.d(acct?.email)
if (acct != null) {
val credential = GoogleAuthProvider.getCredential(acct.idToken, null)
auth(credential)
}
}
private fun btnFacebook(){
val token = AccessToken.getCurrentAccessToken()
if(token!=null){
val credential = FacebookAuthProvider.getCredential(token.token)
auth(credential)
}else{
activity?.showBackgroundToast(getString(R.string.no_facebook_auth), Toast.LENGTH_LONG)
}
}
private fun auth(credential: AuthCredential) {
FirebaseAuth.getInstance().currentUser!!.reauthenticate(credential)
.addOnFailureListener{e -> activity?.showBackgroundToast(e.localizedMessage, Toast.LENGTH_LONG)}
.addOnCompleteListener { task ->
if (task.isSuccessful) {
mListener.onReAuthentication(true)
dialog.dismiss()
} else {
activity?.showBackgroundToast(task.exception?.localizedMessage, Toast.LENGTH_LONG)
}
}
}
Then I call this function to actually update the email:
fun updateEmail(newEmail: String): Completable {
return Completable.create { emitter ->
val currentUser = FirebaseAuth.getInstance().currentUser
currentUser!!.updateEmail(newEmail).addOnCompleteListener {
if (it.isSuccessful) {
Timber.d("updated email to %s", newEmail)
emitter.onComplete()
} else {
emitter.onError(Throwable(it.exception?.localizedMessage))
}
}
}
}
Everything works fine until I finally update the email. When I do, Firebase signs the user out every time!! (the following is from Android Studio logcat)
D/FirebaseAuth: Notifying id token listeners about a sign-out event.
D/FirebaseAuth: Notifying auth state listeners about a sign-out event.
It only happens when I change the email. Since I have an auth state listener
, the user gets redirected to login screen after successfully updating the email, which makes no sense to me. Why? How can I avoid this?