i have created a separate file for all pages to check that user is logged in or not and get the uid from it,so I created a a file call GetLoginedUser.js
Code :
import { app, db } from "./FiiireBaseConfig";
import { getAuth, onAuthStateChanged } from "firebase/auth";
const auth = getAuth();
export async function getUser() {
var uId ="Uid Will be here";
console.log("Get User Started");
await onAuthStateChanged(auth, (user) => {
if (user) {
uId = user.uid;
} else {
alert("No User Found");
}
});
console.log("get user ended");
return uId;
}
Now I imported this function into another file
Code :-
const { getUser } = require("./GetLoginedUser");
var user ;
async function updateUserImage() {
user = await getUser();
console.log("user:" + user);
}
updateUserImage();
But the problem is it don’t wait for onAuthStateChanged and returns the initial value. So the output will come like User: Uid will be here not updated!