-1

My app has basic authentication form for registered admin user. In the SignIn form Email and pass are required and optionally 'Remember me' could be checked. Initially I set Auth persist to 'session' or 'local' depending on user choice, like showed in the docs.

firebase.auth().setPersistence(firebase.auth.Auth.Persistence.SESSION)   // or .LOCAL
  .then(function() { 
    firebase.auth().signInWithEmailAndPassword(email, password) ...

I have logout button calling firebase.auth().signOut(). It seems to logout the user, and redirects to login screen, but if page is reloaded, user is again logged in, without going through the SignIn form.

I thought about adding setPersistence(firebase.auth.Auth.Persistence.NONE) to the logout logic, but (at least in the docs) it needs user credentials to signInWithEmailAndPassword. Problem is at this point if the account is previously persisted, and loged in, user didn't leave email and password. Well, at least the password.

Is my approach fundamentally wrong, or do I need to clear local storage in some other way? Or maybe set persistUser to 'none' without calling signInWithEmailAndPassword again?

1 Answer 1

2

set the persistence depending on the "Remember me checkbox"

// Checked
await firebase.auth().setPersistence(firebase.auth.Auth.Persistence.LOCAL)

// Unchecked
await firebase.auth().setPersistence(firebase.auth.Auth.Persistence.SESSION)

afterwards you can log in user with credentials without worrying about storing anything in the browser Local/Session storage and whenever you call

await firebase.auth().signOut() 

user will be logged out

Not the answer you're looking for? Browse other questions tagged or ask your own question.