getAuth()
is synchronous, so that is not the problem. The problem is that there is no current user, so your user.currentUser
returns undefined
and you can't call .uid
on that.
If this code runs when the app/page loads, it might be that Firebase is still restoring the user state, which requires it to call to the server (to check a.o. if the account was suspected) and is an asynchronous operation for that reason. If this is the case, you'll want to use an auth state listener as shown in first code snippet in the documentation on getting the current user:
const auth = getAuth()
onAuthStateChanged(auth, (user) => {
if (user) {
// User is signed in, see docs for a list of available properties
// https://firebase.google.com/docs/reference/js/firebase.User
const uid = user.uid;
getDoc(doc(db, 'users', uid))
// ...
} else {
// User is signed out
// ...
}
});