【问题标题】:Firebase Rule - Match document to user via email addressFirebase 规则 - 通过电子邮件地址将文档与用户匹配
【发布时间】:2018-11-12 19:28:00
【问题描述】:

我试图让一个简单的规则起作用:如果我的 records 集合中的文档包含与登录用户的电子邮件匹配的电子邮件 (fatherEmail || motherEmail),则允许阅读该文档。不工作 - Missing or insufficient permissions

这是我的规则:

每个 Firebase 技术支持推荐的方法来检索经过身份验证的用户的电子邮件地址:

function getUserEmail(){ return request.auth.token.email; }

这是 Stackblitz: https://stackblitz.com/edit/login-and-match-record-mvce?file=src%2Fapp%2Fauth.service.ts

使用经过验证的电子邮件登录: 用户:devcore2911@gmail.com 通过:foobar 如果您查看控制台,您会看到我上面提到的错误。

我的records 集合包含一个文档,其中包含devcore2911@gmail.com 作为fatherEmail 属性的值,因此文档应该拉起:

【问题讨论】:

    标签: firebase google-cloud-firestore firebase-security


    【解决方案1】:

    解决方案:我必须执行查询以过滤掉与用户电子邮件匹配的文档。

    查看此堆栈闪电战:https://stackblitz.com/edit/login-and-match-record-mvce?file=src%2Fapp%2Frecord-list%2Frecord-list.component.ts

    这是解决问题的代码。请注意使用 combineLatest,因为 Firebase 查询不支持 || 运算符。

    const matchFatherEmail = this.afs.collection("records", ref => ref.where("fatherEmail","==", this.authService.user.email));
    const matchMotherEmail = this.afs.collection("records", ref => ref.where("motherEmail","==", this.authService.user.email));
    
    this.records$ = combineLatest(matchFatherEmail.valueChanges(), matchMotherEmail.valueChanges())
      .pipe(switchMap(docs => {
        const [docsFatherEmail, docsMotherEmail] = docs;
        const combined = docsFatherEmail.concat(docsMotherEmail);
        return of(combined);
      }))
    

    【讨论】:

      猜你喜欢
      • 2016-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-23
      • 2016-11-07
      • 2016-09-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多