Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase/Flutter: reload() not refreshing user.isEmailVerified

I'm sending a verification link when a user registers in the app, but when I try to create a stream that listens for when the user has clicked the verify link in the email.

I'm aware that I somehow need to refresh the user token, but I can't seem to get it to work. I thought reload() method was the one, but maybe I'm just not implementing it correctly.

The problem is that the Stream always returns isEmailVerified == false, only way to make it true is for the user to log out and log in again, which is something I'd like to avoid. How do I do this?

I've created this future:

//CHECKS IF EMAIL IS VERIFIED
  Future<bool> checkIfEmailIsVerified() async {
    FirebaseUser currUser = await _auth.currentUser();
    await currUser.reload();
    currUser = await _auth.currentUser();
    final bool flag = currUser.isEmailVerified;

    if (currUser != null) {
      return flag;
    } else {
      return false;
    }
  }

and this stream:

//IS EMAILVERIFIED STREAM
  Stream<EmailVerified> get emailVerified async* {
    final bool isEmailVerified = await checkIfEmailIsVerified();
    yield EmailVerified(isEmailVerified);
  }
like image 504
Rasmus Lian Avatar asked Jul 10 '26 19:07

Rasmus Lian


1 Answers

Unfortunately it's necessary to get fresh instance of the user after reload:

User user = FirebaseAuth.instance.currentUser;
if (user != null) {
    await user.reload();
    user = FirebaseAuth.instance.currentUser;
    if (user.emailVerified) {
      ...
    }
}
like image 67
Kamil Svoboda Avatar answered Jul 13 '26 14:07

Kamil Svoboda