3
votes

I'm merging an ionic app from firebase v2 to firebase v3.

When I use the new (v3) firebase email/password login API, I am not authenticated have to log in every time the app is re-started.

The user should stay authenticated when first logged in into the app. Or at least this was default in the firebase v2 API.

How can I persist the login?

Thanks, Jørgen

1
By merging, you mean you've brought over code from v2 but using script Firebase 3 .js? - KhoPhi
With firebase v3 the token never expires. Just be sure to only use the Firebase sdk v3 - Devid Farinelli
I only have firebase v3.1 in the project right now. - Jørgen Svennevik Notland
I log in, re-build the ionic app and when i try to log "firebase.auth().currentUser" it returns null - Jørgen Svennevik Notland

1 Answers

7
votes

You should use onAuthStateChanged(). This function will be called any time there's a change in the authentication state.

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

If you are already logged in when you open your app, or you call signInWithEmailAndPassword, this function will be called and user will contain firebase.auth().currentUser.