我一直在尝试使用 auth.updateProfile
方法设置用户自定义属性或属性,但它只保存 displayName
属性,因为它在其 docs 中被提及:
this.af.auth.createUser({
email: formData.value.email,
password: formData.value.password,
}).then((user) => {
let userProfile = formData.value;
userProfile.role = AuthService.ROLE_PATIENT;
userProfile.displayName = `${formData.value.firstName} ${formData.value.lastName}`;
user.auth.updateProfile(userProfile).then(function(){
this.router.navigate(['/']);
});
现在的问题是如何设置用户的自定义属性,以便在 FirebaseAuthState
中获取它们
问题:为什么我需要为每个用户保存自定义属性?
回答:因为我正在使用 Auth Guard使用保存在数据库示例中的 role
属性激活特定路由:
canActivate(route: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
//Currently I'm using this which only check that user is logged in or not
return this.auth
.take(1)
.map((authState: FirebaseAuthState) => !!authState)
.do(authenticated => {
if (!authenticated) this.router.navigate(['/login']);
});
}
但我想要再检查一次以验证用户是否具有我在上面创建用户时保存的患者 Angular 色,例如:
return user.role == '患者'
最佳答案
Firebase Authentication 中现有的身份提供者无法添加自定义属性。如果你想添加自定义属性,你要么必须实现自定义标识提供程序(参见 minting custom tokens in a trusted process 和 signing in with a custom token ),要么执行 client-side look-up of the additional information from an alternate data source (例如 Firebase 数据库)。
关于Angular2 Firebase 设置用户自定义属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43216067/