My situation is quite straightforward. I have developed a Firebase web application and I am logging in with my Google account. The problem arises when I have to keep logging back in every time the page is refreshed, as depicted in these steps:
- Initialize Firebase
- Sign up using Google
- Check the current user - it shows an authenticated Google user
- Refresh the page
- Initialize Firebase again
- Check the current user - it displays as undefined
The code snippet provided is fairly simple:
firebase.initializeApp(config);
var provider = new firebase.auth.GoogleAuthProvider();
firebase.auth().signInWithPopup(provider);
...
public onAuthStateChanged(context: any, user: any) {
if (user) { ...
...
//currentUser is defined
get currentUser(): any {
return firebase.auth().currentUser;
}
When you refresh the page:
//currentUser becomes undefined
get currentUser(): any {
return firebase.auth().currentUser;
}
...
if(!currentUser) {
firebase.auth().signOut();
firebase.initializeApp(config);
}
I am aware of the options regarding persistence of Firebase sessions, but it seems that this behavior is not the default setting. You can refer to the documentation for more information:
https://firebase.google.com/docs/auth/web/auth-state-persistence
As a precautionary measure, I have added this line to my code, although it doesn't seem to make a difference:
firebase.auth().setPersistence(firebase.auth.Auth.Persistence.SESSION)
I have also confirmed that the same issue occurs with Anonymous authentication.