Search code examples
firebasefirebase-authenticationangularfire2

auth().currentUser.reload() returns undefined


I am trying to check firebase emailVerified without logging out the user (Angular 6 web app), but auth().currentUser.reload() returns "undefined"

Tried:

import { auth } from 'firebase/app';

auth().currentUser.reload().then((u) => {
  console.log(u);
});

Tried:

import { AngularFireAuth } from '@angular/fire/auth';
this.afAuth.auth.currentUser.reload().then((u) => {
  console.log(u);
});

These related issues DO NOT HELP.

Update the email verification status without reloading page

Firebase: Observe email verification status in real time

Auth.auth().currentUser?.reload() doesn't refresh currentUser.isEmailVerified


Solution

  • The firebase core doesn't include the auth module. You need to add the auth module:

    import firebase from 'firebase/app';
    import 'firebase/auth';
    

    If no user is currently authenticated, currentUser will be null and will not have a reload() function. Trying to call it would give you and error.

    The reload() function returns a promise so you can use .then, but the promise doesn't return the currentUser object. To view the currentUser after the reload, just refer to the currentUser...

    if (firebase.auth().currentUser) {
      firebase.auth().currentUser.reload().then(() => {
        console.log(JSON.stringify(firebase.auth().currentUser));
      });
    } else {
      console.log('No authenticated user');
    }