I'm currently facing an issue while creating a new user with email and password using firebase authentication. The problem arises when I try to access the displayName from the returned async call before the updateProfile function, as it returns a null value. To work around this issue, I have resorted to passing the username directly in the function to set it as the displayName. Additionally, I am also attempting to create a new document within the users collection. To access all the necessary fields, I had to use .then after the updateProfile function, which I believe is not the most efficient method. Could someone please review my code and point out any potential errors?
async signUp(email: string, password: string, username: string, phoneNumber: string){
await this.afAuth.createUserWithEmailAndPassword(email, password)
.then((result) => {
this.router.navigate(['/home'])
return result.user?.updateProfile({
displayName: username
})
.then( () => {
this.afs.collection("users").doc(result.user?.uid).set({
uid: result.user?.uid,
displayName: result.user?.displayName,
phoneNumber: phoneNumber,
photoURL: result.user?.photoURL,
providerId: result.user?.providerId,
})
})
})
.catch((error) => {
window.alert(error.message);
})
}