Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to maintain user session after exiting app in Firebase

I have an app using Firebase as backend. My App implements Firebase-Authentication API using simple Email and Password login for authenticating.

Everything works fine except for one issue. I would like to maintain the user session even after a user closes the App.

FIRAuth.auth()?.currentUser? has all the user properties (ex: uid, email, token etc...) for the current user which works great. The problem I noticed is that FIRAuth.auth()?.currentUser? is not persistent. So after exiting the app and launching again, it returns null.

I would not like to ask a user every time they open the app to login again. Is there a way around this?

like image 940
Lavvo Avatar asked May 31 '16 03:05

Lavvo


People also ask

How do I clear the Firebase Auth session state?

An explicit sign out is needed to clear that state. Note that Firebase Auth web sessions are single host origin and will be persisted for a single domain only. Indicates that the state will only persist in the current session or tab, and will be cleared when the tab or window in which the user authenticated is closed. Applies only to web apps.

How to get the current user in Firebase account?

The recommended way to get the current user is by setting an observer on the Auth object: firebase.auth ().onAuthStateChanged (function (user) { if (user) { // User is signed in. } else { // No user is signed in. } }); By using an observer, you ensure that the Auth object isn't in an intermediate state—such as initialization—when you get ...

What is persistent authentication state in Firebase JS?

Authentication State Persistence. 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.

Do Firebase refresh tokens expire?

Refresh tokens expire only when one of the following occurs: A major account change is detected for the user. This includes events like password or email address updates. The Firebase Admin SDK provides the ability to revoke refresh tokens for a specified user.


1 Answers

Firebase should already be keeping track of your user through app launches.

Your issue could be with using FIRAuth.auth()?.currentUser? directly rather than the recommended approach.

The recommended way to get the current user is by setting a listener on the Auth object:

FIRAuth.auth()?.addStateDidChangeListener { auth, user in
  if let user = user {
    // User is signed in.
  } else {
    // No user is signed in.
  }
}

By using a listener, you ensure that the Auth object isn't in an intermediate state—such as initialisation—when you get the current user.

In my own application, I use both. If the user doesn't exist with the first method, it moves onto the second, and if that fails, present login/signup screen.

like image 68
Beau Nouvelle Avatar answered Oct 13 '22 10:10

Beau Nouvelle