问题
After reading so many posts and empty(or not?) solutions, I figured that the best thing was to post of my own. So my goal is just to get the displayName of the user so in a list of posts or whatever, the authorship doesnt look ugly with a uid... Here's what I find relevant for the problem:
Signup reactive form, later called on onInit:
createForm() {
this.registerForm = this.fb.group(
{
name: ['', Validators.required],
email: ["", [Validators.required]],
password: [null, [Validators.required, Validators.minLength(5)]],
retype: ["", Validators.required]
},
{
validator: PasswordValidators.passwordsShouldMatch
}
);
}
The submit method:
onSubmit() {
if (this.registerForm.invalid) return;
this.registered = true
this.authService.registerUser(this.email.value, this.password.value).then(data=>{
console.log(data)
})
}
The authService calling the uid and the register method:
constructor( private afAuth: AngularFireAuth, private router: Router) {
this.afAuth.authState.subscribe(user=>{
this.userId = user.uid;
})
}
registerUser(email:string, password:string) {
return this.afAuth.auth.createUserWithEmailAndPassword(email, password)
.then((result) => {
this.router.navigate(['/login']);
console.log(result.user)
}).catch((error) => {
window.alert(error.message)
})
}
When logging this line this.afAuth.authState.subscribe(user=>{
this.userId = user.uid;
, actually logging the user, i can see the whole object and the displayName, but since the register method only accepts 2 arguments, email and pass, how to I workaround this? I've tried an uproach dealing with updateprofile but I got stuck on an error...Is there any solution? Thank you
回答1:
If you wan to set the display name of a user account, you won't be able to do that at the time of account creation using createUserWithEmailAndPassword. You'll have to perform a followup call to updateProfile() on the user object you get back.
result.user.updateProfile({
displayName: ...
})
.then(...)
.catch(...)
You will obviously have to pass along the name in the call to registerUser()
.
来源:https://stackoverflow.com/questions/60405997/angular-8-firebase-how-do-i-set-the-displayname-when-creating-user-with-email