问题
I have implemented Firebase authentication with email and password, here is my code
mFirebaseAuth.createUserWithEmailAndPassword(edtEmail.getText().toString(), edtPassword.getText().toString()).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
FirebaseUser user = task.getResult().getUser();
Toast.makeText(RegisterActivity.this, "User registered successfully " + user.getEmail(), Toast.LENGTH_SHORT).show();
user.sendEmailVerification().addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
finish();
Log.e("RegisterActivity", "onComplete: " + task.getResult().toString());
} else
Log.e("RegisterActivity", "onComplete: " + task.getException().toString());
}
});
} else {
Toast.makeText(RegisterActivity.this, task.getException().toString(), Toast.LENGTH_SHORT).show();
}
}
});
After getting registered successfully i am sending confirmation mail to user, but getting this error.
com.google.firebase.FirebaseException: An internal error has occurred. [ USER_NOT_FOUND ]
I have checked it in firebase also, my user is registered and it is available there, but somehow i am not able to send confirmation mail.
回答1:
I copied and ran your code with Firebase 9.6.1. When I passed an email address to createUserWithEmailAndPassword()
that had never been used before, sendEmailVerification()
completed successfully. I observed the failure you reported when I used an email address for a user that I had previously created and then deleted at the Firebase console. Are you seeing the failure when you use addresses you have used before and then deleted?
Note that createUserWithEmailAndPassword() not only creates the user, but also, if successful, signs the user in. When the creation and sign-in occurs when there is an existing signed-in user, there appears to be a Firebase bug related to signing out and clearing the cache for the previous user.
I was able to make your code work for a previously signed-in and later deleted user by calling signOut() before createUserWithEmailAndPassword()
.
回答2:
You should use AuthStateListener()
to see when a user is signed in that will ensure that user has successfully created and logged in then you can verify your user.
private FirebaseAuth.AuthStateListener mAuthListener;
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
VerifyYourUserWithEmail();
} else {
// User is signed out
Log.d(TAG, "onAuthStateChanged:signed_out");
}
// ...
}
};
@Override
public void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
@Override
public void onStop() {
super.onStop();
if (mAuthListener != null) {
mAuth.removeAuthStateListener(mAuthListener);
}
}
来源:https://stackoverflow.com/questions/40043508/com-google-firebase-firebaseexception-an-internal-error-has-occurred-user-no