3

This is about Flutter Firebase Authentication plugin.
I am trying to send a verification email after creating a new user, but sendEmailVerification() internally uses currentUser(). This looks like a bug to me, but just in case, I am posting a question to stackoverflow. The error is the same on Android and IOS.

First two lines return FirebaseUser. The third returns null. Forth, if third is commented throws a null reference error.

Thanks,
Boris

user = await _auth.createUserWithEmailAndPassword(email: email, password: password); 
// result is FirebaseUser
user = await _auth.signInWithEmailAndPassword(email: email, password: password);
// result is FirebaseUser    
user = await _auth.currentUser();
// result is null    
await user.sendEmailVerification();
//null reference, because it internally uses .currentUser()

3 Answers 3

2

Here is what I found, and I am no sure is it still a bug or not, but there is a way to make it work. FirebaseUser returned by signInWithEmailAndPassword is not really authenticated. Only user returned by onAuthStateChanged stream is. So, if you call user.sendEmailVerification() from onAuthStateChanged context, then everything is fine.

This will work:

_firebaseUserChanged = _auth.onAuthStateChanged.listen((FirebaseUser user) {
    if (user != null && !user.isEmailVerified) {
        user.sendEmailVerification(); 
   }
});

Of course, this code is simplified. We do not want to send verification email on every login attempt.

Sign up to request clarification or add additional context in comments.

Comments

1

Looking at the firebase_auth code I see that signInWithEmailAndPassword does instantiate and return a user. That's probably the user you need to call sendEmailVerification with.

final user = await _auth.signInWithEmailAndPassword(email: email, password: password);
await user.sendEmailVerification();

4 Comments

This is exactly what I wrote in my question. It does return FirebaseUser, but once you call sendEmailVerification() on it, the call fails.
This does look weird. Looking into the java plugin it does call firebaseAuth.signInWithEmailAndPassword which means currentUser should be set, unless there was some problem with the sign in. Are you sure the user exists and the email/password are correct?
If you have an existing user in Firebase and after calling _auth.signInWithEmailAndPassword with correct credentials, _auth.currentUser returns null, this is a bug.
Yes, I have no problems authenticating with signInWithEmailAndPassword, and getting back FirebaseUser. I think it must be a bug.
1

What if you reload the returned FirebaseUser? This worked for me.

const User currentUser = await auth.currentUser;
if (currentUser != null) {
  await currentUser.reload();
}

Clearly simplified code. Note: auth.currentUser() is now auth.currentUser.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.