Reputation: 13061
I'm using Firebase with only the phone number authentication, using the FirebaseUI library to manage the authentication phase as shown below:
startActivityForResult(
AuthUI.getInstance()
.createSignInIntentBuilder()
.setIsSmartLockEnabled(true)
.setAvailableProviders(
Arrays.asList(new AuthUI.IdpConfig.Builder(AuthUI.PHONE_VERIFICATION_PROVIDER).build()))
.build(),
RC_SIGN_IN);
The activity result is handled by onActivityResult
in this way:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
IdpResponse response = IdpResponse.fromResultIntent(data);
if (resultCode == RESULT_OK) {
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
afterRegister(user);
finish();
return;
} else {
// Sign in failed
if (response == null) {
// User pressed back button
ToastUtil.showSnackbar(this,R.string.sign_in_cancelled);
return;
}
if (response.getErrorCode() == ErrorCodes.NO_NETWORK) {
ToastUtil.showSnackbar(this,R.string.no_internet_connection);
return;
}
if (response.getErrorCode() == ErrorCodes.UNKNOWN_ERROR) {
ToastUtil.showSnackbar(this,R.string.unknown_error);
return;
}
}
ToastUtil.showSnackbar(this,R.string.unknown_sign_in_response);
}
}
What I'm trying to do after the registration is to update the email and the display name on the FirebaseUser instance:
final String email = "[email protected]";
final FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
firebaseUser.updateEmail(email).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
boolean success = task.isSuccessful();
if(!success){
task.getException().printStackTrace();
}
}
});
The update operation fails throwing that exception:
com.google.firebase.auth.FirebaseAuthRecentLoginRequiredException:
This operation is sensitive and requires recent authentication. Log in again before retrying this request.
My question is now: How can I "login again"? The documentation is poor about this specific case.
Upvotes: 3
Views: 499
Reputation: 335
Change email is sensitive info, if you catch exception FirebaseAuthRecentLoginRequiredException you must re-auth user with OTP, and back again to finish your task to update user email. for me it's work.
btnSave.setOnClickListener {
FirebaseAuth.getInstance().currentUser?.updateEmail(edEmail.text.toString())
?.addOnCompleteListener { task ->
if (task.isSuccessful) {
Log.d("UPDATE EMAIL", "User email address updated success.")
}else{
try {
throw task.exception!!
} catch (e: FirebaseAuthRecentLoginRequiredException) {
/**
* handle this to re-auth User, e.g Open OTP page
* */
} catch (e: FirebaseAuthInvalidCredentialsException) {
} catch (e: FirebaseAuthUserCollisionException) {
} catch (e: Exception) {
}
}
}
}
you can read how to update email with important notes in here: https://firebase.google.com/docs/auth/android/manage-users
Upvotes: 3