I am currently using firebase for auth in my web application and one particular issue that I have been facing is that even though the user did not sign out from the application if I reload the page and try to access the current user's accessToken using the following snippet of onAuthStateChanged
import { getAuth, onAuthStateChanged } from "firebase/auth";
const auth = getAuth();
onAuthStateChanged(auth, (user) => {
const userAccessToken = user?.accesstoken;
doStuff(userAccessToken); // some placeholder function which takes the `userAccessToken` as one of the parameters
});
The user object is null thus indicating the user is signed out. How do I prevent this from happening? Is there a way to ensure that the user stays signed in unless he explicitly signs out from the application?
The onAuthStateChanged must always return the user as not null unless he signs out. The page reloads or any other kind of navigation or operations must not affect this state.
This has particularly been a problem since I require the jwt token of the currently signed-in user to carry out authorization tasks in the backend. The firebase-admin function
const decodedToken = await admin.auth().verifyIdToken(authJwtToken);
upon receiving null errors out
FirebaseAuthError: Decoding Firebase ID token failed. Make sure you passed the entire string JWT which represents an ID token.