Angular firebase email verification false after ve

2020-02-11 13:57发布

问题:

I'm setting up an authorisation function with e-mail and password. Everything works fine but when i create a new user, the application sends an e-mail with a verification link. After i verify the e-mail adres I want to login so i get back to the login form. the emial_verified keeps staying at 'false', after i hard reload the page this is 'true', but not when i come frome the verification page back to the login page. Can someone help me?

  constructor(
    public afs: AngularFirestore,   // Inject Firestore service
    public afAuth: AngularFireAuth, // Inject Firebase auth service
    public router: Router,  
    public ngZone: NgZone // NgZone service to remove outside scope warning
  ) {   

    /* Saving user data in localstorage when 
    logged in and setting up null when logged out */
    this.afAuth.authState.subscribe(user => {
      if (user) {
        this.userData = user;
        localStorage.setItem('uid', this.userData.uid);
        localStorage.setItem('user', JSON.stringify(this.userData));
        JSON.parse(localStorage.getItem('user'));
      } else {
        localStorage.setItem('user', null);
        JSON.parse(localStorage.getItem('user'));
      }
    })
  }

// Sign up with email/password
  SignUp(email, password) {
    return this.afAuth.auth.createUserWithEmailAndPassword(email, password)
      .then((result) => {
        console.log(result);
        /* Call the SendVerificaitonMail() function when new user sign 
        up and returns promise */
        this.SendVerificationMail();
        this.SetUserData(result.user);
      }).catch((error) => {
        window.alert(error.message)
      })
  }

  // Send email verfificaiton when new user sign up
  SendVerificationMail() {
    return this.afAuth.auth.currentUser.sendEmailVerification()
    .then(() => {
      this.router.navigate(['verify-email-address']);
    })
  }

回答1:

This is not a bug, but expected behavior.

Email verification happens out of band (as in: you click a link in your email client, not in the app), so the app is not aware of the verification status being changed. This means that it can't automatically refresh the ID token, which is where the client gets profile information comes from, in the client.

The token auto-refreshes every hour, so it will update eventually. If you want to get the updated value sooner, you can force the token to refresh.

Also see:

  • Firebase email verification not updating status
  • the answer I just gave here: Firebase custom claim how to set?
  • Firebase firestore not updating email verification status
  • Firebase email verification doesn't verify account
  • user.emailVerified doesn't change after clicking email verification link firebase