FirebaseUser isEmailVerified is always returning false even after verifying email

Viewed 416

I have a static AuthProvider class that centralizes all authentication.

I have the following registration code.

  AuthResult newUser = await auth.createUserWithEmailAndPassword(
      email: email, password: password);
  if (newUser == null) {
    print(
        'AuthProvider: empty user is returned from createUserWithEmailAndPassword');
    return false;
  }
  await newUser.user.sendEmailVerification();
  return true;

After signing up on the app, I received a verification email, so I clicked on it. When I try to sign in next time, isEmailVerified is returning false. After some research, I think I am supposed to reload the user object as follows:

FirebaseUser user = await auth.currentUser();
await user.reload();
user = await auth.currentUser();

print('${user.isEmailVerified}');

Unfortunately, isEmailVerified is still returning false. Does anyone have any idea why this is happening?

2 Answers

The isEmailVerified isn't updated until the next time an ID token is generated for the user.

You have a few options to accomplish this:

  1. The ID token is auto-refreshed every 55 minutes, so you can wait for it to refresh.
  2. You can sign the user out, and tell them to sign them back in again, which will also generate a new ID token with the updated isEmailVerified value.
  3. You can call getIdToken(true) on the user, which forces it to refresh the ID token and thus get the updated isEmailVerified value.

firebase loses all stored information after the app is closed.

if you want auto login function,

you use shared_preferences flutter plugin

url link : https://pub.dev/packages/shared_preferences

save email and password(have to encryto) and

when start app load data

  await _auth
        .signInWithEmailAndPassword(
      email: saveData,
      password: pwData,
    )

and user.isEmailVerified function is return true

Related