I\'ve set up authentication using firebase for an angular app in an auth service, and I\'m trying to ensure session state persistence following successful login.
I\'
You're importing the entire firebase module. Only import auth. The following works for me:
// Don't import the whole module, only `auth`.
import { auth } from 'firebase/app';
constructor(private readonly afAuth: AngularFireAuth) { }
signIn() {
this.afAuth.auth.setPersistence(auth.Auth.Persistence.LOCAL).then(() => {
// Now sign-in using your chosen method.
return this.afAuth.auth.signInAnonymously();
}).catch((error) => {
// Handle errors here.
let errorCode = error.code;
let errorMessage = error.message;
console.error(errorCode, errorMessage);
});
}
add
this.firebaseAuth
.auth
.setPersistence(firebase.auth.Auth.Persistence.LOCAL)
.signInWithEmailAndPassword(email, password)
It is likely you are not using onAuthStateChanged listener to detect the initial Auth state on page load.
firebase.auth().onAuthStateChanged(user => {
if (user) {
// User signed in.
} else {
// User not signed in.
}
});