Skip to content Skip to sidebar Skip to footer

Explicit Renewal Of Session Tokens In Firebase Js Sdk

I have implemented the signin method using Firebase Auth for several providers like that: firebase.auth().setPersistence(firebase.auth.Auth.Persistence.LOCAL).then(() => { let

Solution 1:

Firebase tokens are set to expire after 60 min. Then it gets refreshed for you automatically. There is no way to configure the expiration time, and you don't need to do anything special in your front-end code for that.

The only trick is that, you need to grant your application API key the permission to use the Token Service API to be able to mint a new id token for you once it's expired. This is done in the GCP console, API & Services (Credentials).

So, the code should be simple as the following

  1. Add the user authentication state change Listener

    
    fbAuth.onAuthStateChanged(user => {
      if (user) {
        // User is logged in// Proceed with your logged in user logic
      } else {
        // USer is not logged in// Redirect to the login page
      }
    })
    
  2. Implement the login logic

    
    fbAuth.setPersistence(firebase.auth.Auth.Persistence.LOCAL)
     .then(() => {
        return fbAuth.signInWithEmailAndPassword(email, password)
            .then(userCredential => {
                // Login success path
            })
            .catch(error => {
                // Login error path
            });
     })
     .catch(error => {
        // Persistence setting error path
     })
    

You can set the Authentication State Persistence before the login, depending on your use cases auth-state-persistence.

  1. Make sure that your application API key has access to the Token Service API in the GCP console This is under GCP Console | APIs & Services | Credentials Then edit the corresponding key to your deployment environment to grant the API key the access to the Token Service API.

Good luck ;)

Solution 2:

Hello first I am gonna say sorry fro my bad english. I dont really understand firebase but i think it should work if you write something like this:

firebase.auth().setPersistence(firebase.auth.Auth.Persistence.SESSION).then(() => {
let provider = new firebase.auth.GoogleAuthProvider(); // + facebook, gitHub
provider.addScope('profile');
provider.addScope('email');
firebase.auth().signInWithPopup(provider).then(result => {
// app logic here

I meant that you should have firebase.auth.Auth.Persistence.SESSION insted of LOCAL

Post a Comment for "Explicit Renewal Of Session Tokens In Firebase Js Sdk"