Reputation: 31
After creating new user with auth.createUserWithEmailAndPassword and updating profile, firebase auth listener (onAuthStateChanged) showing displayName in user object but it is not accessible. Uploading screenshots of this behavior as well as code.
null - displayName displayName can be seen on expanding object
export var signup = userData => {
return async dispatch => {
try {
var { username, email, password } = userData;
var createdUser = await auth.createUserWithEmailAndPassword(
email,
password
);
//updating auth profile
await createdUser.user.updateProfile({
displayName: username,
email: email
});
//storing user info in firestore
var userObj = {
username: username,
email: email,
joinedAt: serverTimestamp()
};
await firestore
.collection("users")
.doc(`${createdUser.user.uid}`)
.set(userObj);
//setting up auth state
dispatch(
setCurrentUser({
username: username,
uid: createdUser.user.uid
})
);
} catch (error) {
console.log(error.meesage)
}
};
};
componentDidMount = () => {
auth.onAuthStateChanged((user) => {
if(user){
console.log(user)
console.log(user.uid)
console.log(user.displayName);
console.log(user.email)
}
})
};
Upvotes: 0
Views: 487
Reputation: 317372
The auth state listener is going to trigger immediately after the user is created when you call createUserWithEmailAndPassword()
. At that moment, the user will not have a displayName property. It's not until after you call updateProfile()
that the account will have a displayName. However, the call to updateProfile()
will not trigger the auth state listener again. You should instead maintain your own record, in memory or in some database, of the name that you added with updateProfile()
, and use that value later.
The user object delivered to the auth state listener will not see the new value of displayName until the user signs out and back in again.
Upvotes: 4