Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Firebase云Firestore规则/权限问题_Javascript_Angular_Google Cloud Firestore_Firebase Authentication_Firebase Security - Fatal编程技术网

Javascript Firebase云Firestore规则/权限问题

Javascript Firebase云Firestore规则/权限问题,javascript,angular,google-cloud-firestore,firebase-authentication,firebase-security,Javascript,Angular,Google Cloud Firestore,Firebase Authentication,Firebase Security,我正在尝试设置权限,以便具有给定电子邮件地址的用户只能读取具有匹配docId的文档 这是文件集: /cats/a@test.com /cats/b@test.com /cats/c@test.com 以下内容在“规则操场”中非常有效,但在我的Angular应用程序中,它始终没有返回任何结果 rules_version = '2'; service cloud.firestore { match /databases/{database}/documents { match /ca

我正在尝试设置权限,以便具有给定电子邮件地址的用户只能读取具有匹配docId的文档

这是文件集:

/cats/a@test.com
/cats/b@test.com
/cats/c@test.com
以下内容在“规则操场”中非常有效,但在我的Angular应用程序中,它始终没有返回任何结果

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {

    match /cats/{catDocId} {
      allow read: if request.auth.token.email.lower() == catDocId;
      allow write: if false;
    }

  }
}
以下是应用程序代码:

myPage.component.ts:

import { AngularFireAuth } from '@angular/fire/auth';
import { AngularFirestore } from '@angular/fire/firestore';
import { Observable } from 'rxjs';

// ...

cats: Observable<any[]>;

// ...

ngOnInit(): void {
    this.afAuth.authState.subscribe(user => {
        this.cats = this.firestore.collection('cats').valueChanges();
    });
}
<ul>
    <li class="text" *ngFor="let cat of cats | async">
        {{cat.name}}, {{cat.color}}, {{cat.favoriteFood}}
    </li>
</ul>
从'@angular/fire/auth'导入{AngularFireAuth};
从'@angular/fire/firestore'导入{AngularFirestore};
从“rxjs”导入{Observable};
// ...
猫:可见;
// ...
ngOnInit():void{
this.afAuth.authState.subscribe(用户=>{
this.cats=this.firestore.collection('cats').valueChanges();
});
}
myPage.component.html:

import { AngularFireAuth } from '@angular/fire/auth';
import { AngularFirestore } from '@angular/fire/firestore';
import { Observable } from 'rxjs';

// ...

cats: Observable<any[]>;

// ...

ngOnInit(): void {
    this.afAuth.authState.subscribe(user => {
        this.cats = this.firestore.collection('cats').valueChanges();
    });
}
<ul>
    <li class="text" *ngFor="let cat of cats | async">
        {{cat.name}}, {{cat.color}}, {{cat.favoriteFood}}
    </li>
</ul>
  • {{cat.name}、{{cat.color}、{{cat.favoriteFood}
你知道哪里出了问题吗?谢谢。

根据您的评论:


因此,我似乎试图将规则用作过滤器。有简单的方法吗 基于用户令牌授予对单个文档的读取权限的方法 数据,即他们的电子邮件地址

您只需使用该对象即可获取用户的电子邮件。我并不精通
angularfire
,但是下面的代码(来自angularfire on Authentication)展示了如何获取
displayName
属性。您应该对
电子邮件
属性执行相同的操作。然后,您应该直接查询文档,如前所述,因为您拥有整个文档

从'@angular/core'导入{Component};
从'@angular/fire/auth'导入{AngularFireAuth};
从“firebase/app”导入firebase;
@组成部分({
选择器:'应用程序根',
模板:`
你好{{user.displayName}}!
注销
请登录

使用谷歌登录 `, }) 导出类AppComponent{ 构造函数(公共授权:AngularFireAuth){ } 登录(){ this.auth.signInWithPopup(新的firebase.auth.GoogleAuthProvider()); } 注销(){ this.auth.signOut(); } }
明显的问题:您的用户登录了吗?另外,您可以分享您的查询吗?请务必注意,安全规则不是过滤器,请参见“是的,用户已登录”,我已对问题进行了编辑,以包含角度代码。谢谢,看来我是想用规则来过滤。有没有一种简单的方法可以根据用户的令牌数据(即他们的电子邮件地址)授予对单个文档的读取权限?谢谢Renaud。我可以毫无问题地查找用户的电子邮件。这是否意味着我应该将我的Cloud Firestore权限设置为“允许读取:if request.auth!=null;”(即:如果用户登录,则允许读取),然后从角度过滤结果?我只是想在Firebase数据库
allow read:if-request.auth.token.email.lower()==catDocId中尽可能地安全
是必需的(它阻止其他用户读取用户的文档),而且在IMO中也是足够的,因为如果用户未登录,
请求.auth
将为空。因此,要完成此操作,无需“从角度过滤结果”。只需获取一个且只有一个文档,即带有
/cats的文档/a@test.com
路径并像在问题中一样保护它。但问题仍然是
允许读取:if request.auth.token.email.lower()==catDocId
只在规则操场中工作,在实际的Angular应用程序中它返回零个文档:(你确定你正确地获取了一个文档。你不应该但是。