L.Rencoret
L.Rencoret

Reputation: 155

Authentication State Persistence not working as expected ReactJS

I'm developing a ReactJS app with firebase and my auth persistency is not working as expected. During development it works well in my browser, but when I deploy it to firebase hosting, It doesn't save my auth state in my web browser neither mobile browser.

import * as firebase from 'firebase'
export function signIn(e) { e.preventDefault(); auth.signOut()
  auth.setPersistence(firebase.auth.Auth.Persistence.LOCAL).then(function() {
    return auth.signInWithEmailAndPassword(email, pass).then(() => { 
        .....
    }).catch(err => {
        if (err.code === 'auth/wrong-password') {... }
        else {  alert(err.message) }
    })
  })
}

I'm using onAuthStateChanged to get the user signed in.

auth.onAuthStateChanged(() => {
        if (auth.currentUser)
            ....    
    })  

I forced the persistence to LOCAL, knowing that's the default, but even though it's working like session. I've read the docs Auth Persistence Docs, but the persistence isn't working as it should. Tested with Android, iOS and Web, and auth state doesn't persist.

Is it a firebase hosting problem?

Upvotes: 4

Views: 1295

Answers (1)

Nikhil Kothari
Nikhil Kothari

Reputation: 250

The Firebase SDK sets the Authentication persistence to "LOCAL" by default. If you are using onAuthStateChanged when your application loads, then the problem might be that the Firebase SDK is not able to refresh the idToken using the Refresh Token. To refresh the token, the SDK uses the Token Service API. Check if the API key for your Firebase project has access to the Token Service API by going to the Google Cloud Platform console. I faced a similar issue earlier, and the problem was that I had placed restrictions on the API key in Google Cloud platform.

Upvotes: 1

Related Questions