【问题标题】:Firebase Realtime Database - Client doesn't have permission to access the desired dataFirebase 实时数据库 - 客户端无权访问所需数据
【发布时间】:2021-08-10 19:01:24
【问题描述】:

我有一个应用程序(Ionic Angular Capacitor 2)调用 Firebase 实时数据库中的某个对象。它在访问规则和匿名身份验证方面运行良好。但是在一些模块更新后(不知道这是否是问题)我在调用数据库时被拒绝权限。 这发生在 web 和 ios/android 上。 我已经阅读了谷歌和这里的所有页面。但主要问题是 Realtime db 上的 firestore 规则或通过将 .read/.write 规则设置为 true 来解决。

这是在创建或登录用户时在 OnInit() 上调用的 auth,调用 continueLoad():

async loginAndLoad() {
  // create anonymous user if not exists and/or log in
 await this.fireauth.signInAnonymously().catch((error) => {
     console.log('Login error: ' + error.code);
     console.log('Login error message: ' + error.message);
  });
 // trigger state change, when user is logged in
  this.fireauth.onAuthStateChanged((user) => {
 
    if (user.isAnonymous) {
      var isAnonymous = user.isAnonymous;
      var uid = user.uid;
      console.log('User added: ' + isAnonymous);
      console.log('User uid: ' + uid);
      // We have a signed in user, go on.. Else throw error
         this.continueLoad();
    } else {
          this.loginAndLoad();
        }
  });
}

数据库调用示例,我从 this.continueLoad() 调用:

async getElements() {
     const elements = this.db.list('settings/elements/da/');
     const subscribe = await elements.snapshotChanges().subscribe(res => {
           subscribe.unsubscribe();
       let array = [];
       res.forEach(child => {
         let a: any;
          a = child.payload.toJSON();
           if (a.status == "published") {
            array.push({key: child.key,title: a.title});
          }
       })
        this.storage.set('elements', JSON.stringify(array));
     })
}

这是实时数据库规则:

{
  "rules": {
    ".write": "!data.exists()",
    "products": {
        ".read": "auth.uid != null",
        ".write": "auth.uid != null",
        ".indexOn": ["stats", "status", "user_warn", "image_approved", "image_added", "note", "description","title","date_published","date_edited","date_added"]
    },
    "searchwords": {
        ".read": "auth.uid != null",
        ".write": "auth.uid != null"  
    },
    "users": {
        ".read": "auth.uid != null",
        ".write": "auth.uid != null && auth.provider != 'anonymous'"
    },
    "settings": {
        ".read": "auth.uid != null",
        ".write": "auth.uid != null && auth.provider != 'anonymous'"
    },
    "routes": {
        ".read": "auth.uid != null",
        ".write": "auth.uid != null && auth.provider != 'anonymous'"
    },
    "campaigns": {
        ".read": "auth.uid != null",
        ".write": "auth.uid != null && auth.provider != 'anonymous'"
    }  
  }
}

这是离子信息:

Ionic:

   Ionic CLI                     : 6.16.1 (/usr/local/lib/node_modules/@ionic/cli)
   Ionic Framework               : @ionic/angular 5.6.7
   @angular-devkit/build-angular : 0.1100.7
   @angular-devkit/schematics    : 11.0.7
   @angular/cli                  : 11.0.7
   @ionic/angular-toolkit        : 3.1.1

Capacitor:

   Capacitor CLI      : 2.4.6
   @capacitor/android : 2.4.7
   @capacitor/core    : 2.4.6
   @capacitor/ios     : 2.4.7

Utility:

   cordova-res : 0.15.3
   native-run  : 1.3.0

System:

   NodeJS : v14.17.0 (/usr/local/bin/node)
   npm    : 6.14.13
   OS     : macOS Big Sur

这太令人沮丧了......希望有人对这个问题有逻辑想法:)

【问题讨论】:

    标签: javascript angular ionic-framework firebase-realtime-database firebase-security


    【解决方案1】:

    我终于找到了问题所在。如开头所述,som 插件已更新。 (尝试新插件后)。 因此,经过数小时的调试。我降级到 Firebase 8.1.2,@angular/fire 降级到 6.1.5,一切都恢复正常了..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-15
      • 2020-11-03
      • 2017-02-12
      • 1970-01-01
      • 2018-07-14
      • 2019-03-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多