Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic: Authentication not persistent when merging to firebase v3

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

like image 249
Jørgen Svennevik Notland Avatar asked Sep 24 '26 01:09

Jørgen Svennevik Notland


1 Answers

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.

like image 199
Devid Farinelli Avatar answered Sep 26 '26 22:09

Devid Farinelli