【发布时间】:2019-06-05 13:36:25
【问题描述】:
尊敬的 Stackoverflow 社区,
我们使用 Firebase Auth 进行身份验证。为此,我们提供使用 google 和 facebook 的社交登录以及使用电子邮件和密码登录。以下代码显示了以后想要使用电子邮件和密码登录的用户的注册页面。
import { Router } from '@angular/router';
import { Component, OnInit } from '@angular/core';
import { AngularFireAuth } from '@angular/fire/auth';
import { auth } from 'firebase/app';
@Component({
selector: 'app-register',
templateUrl: './register.component.html',
styleUrls: ['./register.component.css']
})
export class RegisterComponent implements OnInit {
public emailAddress="";
public firstName="";
public lastName="";
public password="";
constructor(public afAuth: AngularFireAuth, private router: Router) {
}
ngOnInit() {
}
reset() {
this.emailAddress=""
this.password=""
this.firstName=""
this.lastName=""
}
register() {
let emailAddress = this.emailAddress
let password = this.password
let firstName = this.firstName
let lastName = this.lastName
this.reset()
console.log(emailAddress)
console.log(password)
auth().createUserWithEmailAndPassword(emailAddress, password).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
console.log(errorCode)
var errorMessage = error.message;
console.log(errorMessage)
// ...
});
}
}
电子邮件、密码、名字、姓氏的数据来自输入字段,该输入字段通过与组件的数据绑定交换数据。
<input type="email" [(ngModel)]="emailAddress" required>
当我尝试获取用户显示名称时,它仅在用户使用社交提供商登录时才有效。这很明显,因为他在使用电子邮件和密码注册时没有设置它。现在我想问你是否有人知道如何在 firebase 中存储 firstName 和 lastName 以便显示 user.displayName。
<div *ngIf="afAuth.user | async as user; else notAuth">
Hello {{ user.displayName }}
</div>
【问题讨论】:
-
成功创建用户后,您需要手动更新该用户的
displayName。您可以使用updateProfile()。作为方法,您可以使用createUserWithEmailAndPassword的then()来执行updateProfile()。
标签: angular firebase firebase-authentication