3

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?

nvoigt
  • 75,013
  • 26
  • 93
  • 142
yangrm
  • 41
  • 2

2 Answers2

5

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.
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    Thx. This is very helpful. I am calling `getIdToken(refresh:true)`. I am able to verify the token is being refreshed. However, `isEmailVerified` is still not updated. Is there anything else I need to do or is there any specific order these instructions need to be executed? Is there any way to see the underlying user properties from Firebase dashboard? – yangrm Aug 05 '20 at 13:41
  • No, unfortunately the Firebase console doesn't show those properties. I typically use a very simple node script with the Admin SDK: https://firebase.google.com/docs/auth/admin/manage-users#retrieve_user_data – Frank van Puffelen Aug 05 '20 at 14:34
  • Thx. Do I need to manually update `isEmailVerified` upon user clicking the link? Or does it automatically get updated? When I dump `user.email_verified` using python library, the value shows `false` even after I click on the link. – yangrm Aug 05 '20 at 16:13
0

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

Junsu Cho
  • 826
  • 7
  • 16
  • 1
    "firebase loses all stored information after the app is closed." Information about the user's sign-in state is automatically persisted by Firebase, and restored when the app is restarted. – Frank van Puffelen Aug 05 '20 at 03:53