49

Does anyone know how long would it take for the token to expire? There no option now to set the token validity on the console.

4 Answers 4

60

Since May 2016 Firebase Authentication login sessions don't expire anymore. Instead they use a combination of long-lived account tokens and short-lived, auto-refreshed access/ID tokens to get the best of both worlds.

If you want to end a user's session, you can call signOut().

8
  • 2
    My token expire every one hour, i have added SHA-1 in firebase console, I send the token to my custom server for verification. Could anyone help me why my expiry time is just one hour.
    – Bikash
    Commented Oct 30, 2016 at 3:54
  • 1
    It seems this has changed yet again, and now the tokens do expire after a while. Commented May 16, 2017 at 14:39
  • 10
    This hasn't changed. Login sessions don't expire. But the short-lived tokens (I think they're called ID tokens) are valid for only an hour. When you use a Firebase SDK, it auto-refreshes those short-lived tokens. Commented May 16, 2017 at 14:45
  • 4
    Since the ID tokens expire once an hour, how can we use them to authenticate with a custom server? I followed the directions on how to verify id tokens on my server, but now the server can't recognize a logged in user after only an hour passes.
    – django09
    Commented Jun 30, 2017 at 3:16
  • 2
    options 1: have a response intercept and refresh the token if the token is invalid and remake the API call. Commented Sep 16, 2017 at 14:42
32

Its does expire. After one hour logged in the token id expire. If you try to verify sdk returns a error "Error: Firebase ID token has expired. Get a fresh token from your client app and try again. See https://firebase.google.com/docs/auth/server/verify-id-tokens for details on how to retrieve an ID token."

Is There such a way to change expiration time to Firebase token, not custom token.

Anybody that know how this really works.

3
  • Any solutions running into this issue?
    – Lion789
    Commented Jan 16, 2017 at 0:20
  • 1
    you could always use currentUser.getIdToken() but my problem is that this returns a promise :-/ I don't want to do async stuff in my request interceptors... Commented Sep 20, 2017 at 8:44
  • 1
    @ProblemsOfSumit you can pass a boolean 'false' as a parameter to getIdToken to make it synchronous. Commented Sep 27, 2019 at 10:57
7

For anyone that is still confused, it is all explained in great detail here

If your app includes a custom backend server, ID tokens can and should be used to communicate securely with it. Instead of sending requests with a user’s raw uid which can be easily spoofed by a malicious client, send the user's ID token which can be verified via a Firebase Admin SDK (or even a third-party JWT library if Firebase does not have an Admin SDK in your language of choice). To facilitate this, the modern client SDKs provide convenient methods for retrieving ID tokens for the currently logged-in user. The Admin SDK ensures the ID token is valid and returns the decoded token, which includes the uid of the user it belongs to as well as any custom claims added to it.

1
  • 5
    the token still expires after one hour and this does not solve the issue Commented Jul 15, 2021 at 16:31
6

If the above answer is still confusing to you, This is what i did:

firebase.auth().onAuthStateChanged(async user => {
    if (user) {
        const lastSignInTime = new Date(user.metadata.lastSignInTime);
        const lastSignInTimeTimeStamp = Math.round(lastSignInTime.getTime() / 1000);
        const yesterdayTimeStamp = Math.round(new Date().getTime() / 1000) - (24 * 3600);
        if(lastSignInTimeTimeStamp < yesterdayTimeStamp){
          await firebase.auth().signOut()
          this.setState({
            loggedIn: false
          });
          return false;
        }
        this.setState({
          loggedIn: true,
          user
        });
      }
    })

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.