无法创建用户PERMISSION_DENIED:权限被拒绝

时间:2016-10-29 10:02:57

标签: firebase firebase-authentication firebase-security

我尝试使用facebook / google登录后创建新用户,但收到错误:

  

error_handler.js:46 EXCEPTION:未捕获(在承诺中):错误:   PERMISSION_DENIED:Permission deniedErrorHandler.handleError @   error_handler.js:46next @ application_ref.js:291schedulerFn @   async.js:89SafeSubscriber .__ tryOrUnsub @   PERMISSION_DENIED:权限被拒绝

我的规则是:

 "users": {
      "$uid": {
        // grants write access to the owner of this user account whose uid must exactly match the key ($uid)
        ".write": "auth !== null && auth.uid === $uid",
        // grants read access to any user who is logged in with Facebook
        ".read": "auth !== null && (auth.provider === 'facebook' || auth.provider === 'google')" 
      }
    }

当我删除我的规则时,一切正常。

我的代码是:

  private createNewUser(user: any): any {
    const newUser = this.af.database.object('users/' + user.uid);
    newUser.set({
      username: user.name,
      email: user.email,
      profile_picture: user.avatar,
      provider : user.provider,
      create_date: new Date().toISOString().slice(0, 10).replace(/-/g, "")})

  }

我需要查看哪些文件?

2 个答案:

答案 0 :(得分:1)

你的规则有误。
您只允许已在uid节点下具有users/uid的现有用户进行写访问,但您不允许users节点上的写访问权。

您需要在users节点下允许写访问权限。但这将级联,这意味着如果用户节点下的.write规则将评估为true,则users节点下的.write规则都不会被检查。因此,您需要将现有的.write更改为.validate

"users": {
  // Grant every logged in user to write here
  ".write": "auth !== null",
  "$uid": {
    // .validate rule will restrict .write if current user id does not match the key ($uid)
    ".validate": "auth !== null && auth.uid === $uid",
    // grants read access to any user who is logged in with Facebook
    ".read": "auth !== null && (auth.provider === 'facebook' || auth.provider === 'google')" 
  }
}

有关如何设置安全规则的详细信息,请阅读以下链接并观看此链接上的视频。

https://firebase.google.com/docs/database/security/

答案 1 :(得分:0)

最后,我将此规则用于用户。

X