首页 > 解决方案 > 允许登录其字段“IsApproved 为 true”的安全规则

问题描述

在注册帐户时,我已将默认值设置false为字段IsApproved。我想编写安全规则以允许那些用户登录IsApproved:true并路由到accessdeniedIsApproved:false. 用于用户注册的代码

async register(){
    if(this.firstname && this.lastname && this.email && this.password){
      const loading =await this.loadingCtrl.create({
        message:'Processing...',
        spinner:'crescent',
        showBackdrop:true
      });
      loading.present();
      
      this.afauth.createUserWithEmailAndPassword(this.email,this.password)
      .then((data)=>{
        this.afs.collection('user').doc(data.user.uid).set({
          'userId':data.user.uid,
          'IsApproved':false,
          'userEmail':this.email,
          'userFirstname':this.firstname,
          'userLastname':this.lastname
        })
        .then(()=>{
          loading.dismiss();
          this.toast('Registration Success','success');
          this.router.navigate(['/login']);
        })
        .catch(error=>{
          loading.dismiss();
          this.toast(error.message,'danger')
        })
      })
    }
  }

如何检查IsApproved字段是否truefalse当用户尝试sign in 编码用于登录时

async SignIn(email,password)
    {
     const loading =await this.LoadingCtrl.create({
       message:'Authenticating..',
       spinner:"crescent",
       showBackdrop:true
     });
     loading.present();
     this.afauth.setPersistence(firebase.default.auth.Auth.Persistence.LOCAL)
     .then(()=>{
       this.afauth.signInWithEmailAndPassword(email,password)
       .then((data)=>{
         if(!data.user){
           loading.dismiss();
           this.toast('Please check your credentials','warning');
           this.afauth.signOut();
         }else{
           loading.dismiss();
           this.router.navigate(['/menu']);
         }
       })
       .catch(error=>{
         loading.dismiss();
         this.toast(error.message,'danger');
       })
     })
     .catch(error=>{
       loading.dismiss();
       this.toast(error.message,'danger');
     });
    }

我尝试使用 If else 检查 If(!data.user.IsApproved)

  if(!data.user){
       loading.dismiss();
       this.toast('Please check your credentials','warning');
       this.afauth.signOut();
     }else{
       loading.dismiss();
        if(data.user.IsApproved===true){
          this.router.navigate(['/menu']);
         }else{
            this.router.navigate(['/accessdenied']);
      }
       
    }
   })

但是当我尝试时它说Property 'IsApproved' does not exist on type 'User'. 我的模型看起来像

export interface User {
    userId:string;
    IsApproved:boolean;
    userEmail:string;
    userPhoto:string;
    userFirstname:string;
    userLastname:string;
}

我试图更改安全规则 allow read,write:if request.auth.uid.IsApproved!=false; 它说unknown error occurred 我试图只允许那些其IsApproved字段是true访问我的应用程序的用户,而我试图路由的其他用户访问被拒绝。

标签: javascriptangulartypescriptgoogle-cloud-firestorefirebase-authentication

解决方案


如果您将IsApproved值存储在 Firestore 中特定于用户的配置文件文档中,则可以get()将该文档存储在您的安全规则中并使用其中的值。

service cloud.firestore {
  match /databases/{database}/documents {
    allow read: if get(/databases/$(database)/documents/user/$(request.auth.uid)).data.IsApproved == true;
  }
}

对于另一个示例,我建议查看有关基于属性和角色的访问控制的 Firebase 文档,我从中复制和修改了上面的示例。


推荐阅读