加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Angular2 Firebase设置用户自定义属性

发布时间:2020-12-17 18:10:17 所属栏目:安全 来源:网络整理
导读:我一直在尝试使用auth.updateProfile方法设置用户自定义属性或属性,但它只保存displayName属性,因为它在 docs中提到: this.af.auth.createUser({ email: formData.value.email,password: formData.value.password,}).then((user) = { let userProfile = for
我一直在尝试使用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']);
      });   
  }

但是我想要再检查一下以验证用户是否具有患者的角色,我在创建上述用户时保存了这个角色,例如:
?return user.role ==’patient’

解决方法

Firebase身份验证中的现有身份提供程序无法添加自定义属性.如果要添加自定义属性,则必须实现自定义标识提供程序(请参阅 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数据库).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读