I have a react app using firebase auth for authentication.
After a person logs in, it redirects them to a books page. Here's the submitLogin handler
import { auth, firebase } from "./firebase";
async function googleLogin() {
const provider = new firebase.auth.GoogleAuthProvider();
await auth.signInWithPopup(provider).then(
async (result) => {
const token = await auth?.currentUser?.getIdToken(true);
if (token) {
localStorage.setItem("@token", token);
navigate("/book-list");
}
},
function (error) {
console.log(error);
}
);
}
Then in the books page, I try getting currentUser so that I can call getIdToken(), but currentUser is returning null.
import { auth } from "./firebase";
const currentUser = auth.currentUser
Here's the firebase.js file:
import firebase from 'firebase/compat/app';
import 'firebase/compat/auth';
import 'firebase/compat/firestore';
const firebaseConfig = {...};
firebase.initializeApp(firebaseConfig);
const auth = firebase.auth();
export { auth, firebase };
How can I call currentUser from another file after a user is signed in?