Angular 8/Firebase:使用电子邮件和密码创建用户时,如何设置displayName? [英] Angular 8 /Firebase: how do I set the displayName when creating user with email and password?

查看:176
本文介绍了Angular 8/Firebase:使用电子邮件和密码创建用户时,如何设置displayName?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读了这么多帖子和空的(或没有?)解决方案之后,我发现最好的方法就是发布自己的帖子.所以我的目标只是获取用户的displayName,以便在帖子列表或其他内容中,作者身份看起来并不像uid一样丑陋...这是我发现与该问题相关的内容:

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:

注册反应形式,稍后调用onInit:

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
      }
    );
  }

submit方法:

 onSubmit() {
    if (this.registerForm.invalid) return;
    this.registered = true
    this.authService.registerUser(this.email.value, this.password.value).then(data=>{
      console.log(data)
    })

  }

调用uid和register方法的authService:

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)
        })
    }

记录此行时 this.afAuth.authState.subscribe(user => {this.userId = user.uid; ,实际上是在记录用户,我可以看到整个对象和displayName,但是由于register方法仅接受2个参数(电子邮件和传递),我该如何解决呢?我尝试了处理updateprofile的麻烦,但是遇到了错误...是否有解决方案?谢谢

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

推荐答案

如果要设置用户帐户的显示名称,则在使用createUserWithEmailAndPassword创建帐户时将无法执行此操作.您必须对 updateProfile()在用户对象上返回.

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(...)

您显然必须在调用 registerUser()的过程中传递名称.

You will obviously have to pass along the name in the call to registerUser().

这篇关于Angular 8/Firebase:使用电子邮件和密码创建用户时,如何设置displayName?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆