Persistence of firebase auth on Ionic 4 App

Hello,

I’m creating a hybrid app with Ionic 4 and using Firebase to authentication. However, I don’t know how to set persistence for this authentication so the user can login in app just one time and his session will always be saved until him logout.

Anyone knows how to set persistence for this authentication?

Thank you

See: Authentication State Persistence

1 Like

I saw that before, but this works even if I build for Android? I also saw a ionic native plugin for this task and tried that, but didn’t work.

You can specify how the Authentication state persists when using the Firebase JS SDK. This includes the ability to specify whether a signed in user should be indefinitely persisted until explicit sign out, cleared when the window is closed or cleared on page reload.

Also, see: The official Angular library for Firebase

Thank you! I was using the wrong function to check persistence. A deep reader on these links helped me.

Hi! not sure how I can implement it… It would be great if you can help me! :slight_smile:

So far I am trying

 loginUser(value){
   return new Promise<any>((resolve, reject) => {
     firebase.auth().signInWithEmailAndPassword(value.email, value.password)
     .then(
       res => {
         resolve(res),
         this.authenticationState.next(true)
         firebase.auth().setPersistence(firebase.auth.Auth.Persistence.LOCAL)
       },
       err => reject(err))
   })
  }

I used the code like this:

loginUser(value){
    return this.fireAuth.auth.setPersistence(fire.localPersistence())
      .then( () => {
        return this.fireAuth.auth.signInWithEmailAndPassword(value.email, value.password);
      }).catch(err => {
        return Promise.reject(err);
      })
  }

(obs. You can change fire.localPersistence() by firebase.auth.Auth.Persistence.LOCAL. Another thing is that I used the module fireAuth instead of firebase directly, but this doesn’t change the idea)

1 Like

Thanks a ton @claudiocfls!!

1 Like

Local Persistence is DEFAULT so far I know. That means for Ionic applications you don’t have to set it explicit.

1 Like