【问题标题】:Are this firebase rules & the PUT and GET request correct? (Angular + Firebase)这个 Firebase 规则和 PUT 和 GET 请求是否正确? (角度 + Firebase)
【发布时间】:2020-10-15 12:27:38
【问题描述】:

我希望用户只能看到他自己的帖子,并且没有人应该访问他的帖子。 Auth + 发布已经可以并且正在工作。但没有 Firebase 限制。

对于 firebase,我计划使用此规则。可以吗?

{
  "rules": {
    "notes": {
      "$uid": {
        ".read": "auth != null && auth.uid == $uid",
        ".write": "auth != null"
      }
    }
  }
}

将请求放入角度:

this.http
      .put(
        'https://_____.firebaseio.com/notes.json?auth=<ID_TOKEN>',
        notes
      )

以角度获取请求(此处为完整函数)

loadNotes() {
    return this.http
      .get<Note[]>(
        'https://______.firebaseio.com/notes.json?auth=<ID_TOKEN>'
      )
      .pipe(
        map(notes => {
          return notes.map(note => {
            return {
              ...note
            };
          });
        }),
        tap(notes => {
          this.noteService.setNotes(notes);
        })
      );
  }
}

我认为这里最关键的点是指向 firebase 数据库和 firebase 规则的链接。

我真的很感激一些帮助!谢谢。

【问题讨论】:

    标签: angular typescript firebase


    【解决方案1】:

    您的put 调用尝试写入/notes,但您的安全规则不向任何人提供对该节点的写入权限。所以写入正确地被拒绝了。

    要允许写操作,你需要把数据放到/notes/$uid

      this.http
          .put(
            'https://_____.firebaseio.com/notes/uidOfUserInAuthToken.json?auth=<ID_TOKEN>',
            notes
          )
    

    这里的uidOfUserInAuthToken 应该与您通过请求发送ID_TOKEN 的用户的UID 匹配。

    【讨论】:

    • 看起来已经很棒了!但是仍然存在一个问题:即使在控制台中 被用户 ID 替换,我仍然收到错误:无法解析身份验证令牌。现在有什么问题?我真的不知道可能缺少什么。
    猜你喜欢
    • 2023-02-07
    • 2020-04-23
    • 2020-04-10
    • 2021-01-29
    • 2020-01-28
    • 2016-07-16
    • 1970-01-01
    • 2016-10-31
    • 2011-11-11
    相关资源
    最近更新 更多