I have this firebase.js file:
// Import the functions you need from the SDKs you need
import { initializeApp } from "firebase/app";
const firebaseConfig = {...};
// Initialize Firebase
export const app = initializeApp(firebaseConfig);
const analytics = getAnalytics(app);
import { getAuth, onAuthStateChanged } from "firebase/auth";
export const auth = getAuth(app);
export let uid = null;
onAuthStateChanged(auth, (user) => {
if (user) {
uid = user.uid;
} else {
uid = null;
}
});
In another function I have been trying to do:
import {uid, auth} from "./firebase.js";
if (auth.currentUser)
or
if (uid)
However, neither works consistently. After sign the user will exist but after a page reload they disappear again. Is this the right way to check the user is signed in?