允许其字段"IsApproved is true"登录的安全规则



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

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

当用户尝试sign in时,如何检查IsApproved字段是true还是false用于登录的代码

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 elseIf(!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的用户访问我的应用程序,而其他用户则被拒绝访问。

如果将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关于基于属性和角色的访问控制的文档,我从中复制和修改了上面的例子。

相关内容

  • 没有找到相关文章

最新更新