javascript - 允许登录其字段“IsApproved 为 true”的安全规则
问题描述
在注册帐户时,我已将默认值设置false
为字段IsApproved
。我想编写安全规则以允许那些用户登录IsApproved:true
并路由到accessdenied
其IsApproved: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
字段是否true
或false
当用户尝试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
访问我的应用程序的用户,而我试图路由的其他用户访问被拒绝。
解决方案
如果您将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 文档,我从中复制和修改了上面的示例。
推荐阅读
- node.js - npm firstline 包不返回第一行(字符串)而是一个 promise 对象,如何获取第一行字符串?
- python - 查找列表中的每个值都出现在 df 中的索引
- microsoft-cognitive - 为什么部署后的测试结果和 ASR 输出在 Microsoft 自定义语音中不同?
- kotlin - Kotlin 协程并行化
- python - 如何打印给定输入的前一个和下一个数字?
- java - 在 JPQL 的本机查询中使用 BETWEEN
- javascript - 如何在纯javascript中更改类的css
- python - 在 pandas 中搜索特定的子字符串
- java - java在xml文件中搜索特定属性名称
- c# - c#替换CSV中的标题文本