Currently, I am attempting to retrieve the authenticated user's uid from Google authentication for a specific collection in the "User" document. To ensure that the data does not become null after refreshing the webpage, I am utilizing onAuthStateChanged
. The code snippet is provided below:
import { getDoc, doc } from 'firebase/firestore'
import { onAuthStateChanged } from 'firebase/auth'
let UserUid = ""
onAuthStateChanged(auth, (user) => {
if (user != null) {
UserUid = user.uid
}
else {
console.log("User is null")
}
})
const docSnap = await getDoc(doc(db, "User", UserUid))
interface DataUser {
FullName: string;
Email: string;
ContactNo: string;
CountryCode: string;
ICNO: string;
HomeAddress: string;
image: string;
userType: UserType;
}
export const DataUser:DataUser = {
FullName: docSnap.data()!["Name"],
Email: docSnap.data()!["Email"],
ContactNo: docSnap.data()!["Contact"],
CountryCode: docSnap.data()!["CountryCode"],
ICNO: docSnap.data()!["ICNO"],
HomeAddress: docSnap.data()!["HomeAddress"],
image: docSnap.data()!["Image"],
userType: 0,
}
An error has been encountered where the last line displays an empty string for UserUid
. It appears that the value is not being passed to UserUid
from onAuthStateChanged
.
Is there a way to successfully pass the value to UserUid
?
I attempted using this.UserUid
, but it returned an error stating that this
was undefined.