Tako Tako - 3 months ago 96
AngularJS Question

Firebase 3. authentication doesn't persist

I'm working with firebase in a angularJS app, using the email and password authentication.
I just update my app from Firebase 2.x to 3.x and AngularFire from 1.x to 2.x.

I followed these 2 docs to do the migration :



But now, each time I refresh my page, I need to re-authenticate, like there is no persistent session.

I've checked the localStorage key
firebase:authUser
I have an
expirationTime
in the past (actually it's set with the timestamp of my login).

To check if the user is loggedin I use :
$firebaseAuth().$getAuth()


EDIT

Here is a working example of my problem (login: toto@mail.fr / password: toto123)
https://plnkr.co/edit/463Hse?p=preview

Does anybody know why this behavior ?

Answer

I am gonna guess that you are checking the currentUser directly without waiting for the initial auth state to resolve. You need to add an observer:

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    // User is signed in.
  } else {
    // No user is signed in.
  }
});

https://firebase.google.com/docs/auth/web/manage-users

Also, auth state is stored in web storage so make sure you have that enabled. (state will not persist in safari private mode browsing for example).

Comments