Javascript AngularFirestore缺少或权限不足(Firebase自定义令牌身份验证)
我一直在尝试从firestore读取数据,但无法执行此操作Javascript AngularFirestore缺少或权限不足(Firebase自定义令牌身份验证),javascript,angular,google-cloud-firestore,firebase-security,angularfire,Javascript,Angular,Google Cloud Firestore,Firebase Security,Angularfire,我一直在尝试从firestore读取数据,但无法执行此操作 async authUser() { let token = this._tokman.getUserToken(); await this._afa.signInWithCustomToken(token); } 从服务器获取令牌并将其传递给firebase以对用户进行身份验证。 然后调用firestore获取数据 async data() { await this._afa.onAuthStateCha
async authUser() {
let token = this._tokman.getUserToken();
await this._afa.signInWithCustomToken(token);
}
从服务器获取令牌并将其传递给firebase以对用户进行身份验证。
然后调用firestore获取数据
async data() {
await this._afa.onAuthStateChanged(user => {
if (user) {
this._afs.collection('gps-data').doc(user.uid)
.valueChanges()
.subscribe(res => {
console.log(res);
}, err => {
console.log(err);
})
}
})
}
但是从firestore获取此错误
FirebaseError: Missing or insufficient permissions.
at new e (http://localhost:4200/vendor.js:51950:23)
at http://localhost:4200/vendor.js:62177:28
at http://localhost:4200/vendor.js:62178:18
at e.onMessage (http://localhost:4200/vendor.js:62200:10)
at http://localhost:4200/vendor.js:62117:26
at http://localhost:4200/vendor.js:62148:37
at http://localhost:4200/vendor.js:66953:31
at ZoneDelegate.invoke (http://localhost:4200/polyfills.js:460:30)
at Zone.run (http://localhost:4200/polyfills.js:219:47)
at http://localhost:4200/polyfills.js:953:40
firestore的安全规则是
service cloud.firestore {
match /databases/{database}/documents {
match /gps-data/{userid} {
allow read: if request.auth.token.sub == userid;
}
}
}
还将jwt令牌添加到http头中。
请告诉我哪里做错了
更新
解决方法:更改firebase版本
从
到
在不更改安全规则的情况下问题在于此行中的
令牌.sub
:
request.auth.token.sub == userid;
令牌的sub
是为其生成该令牌的项目ID。所以你要比较一个项目ID和一个用户ID,这两个ID中的任何一个都不一样
您希望在此处比较两个用户ID,即:
request.auth.uid == userid;
我强烈建议您花些时间阅读有关安全规则的文档,特别是中的部分。如果您使用的是自定义令牌,您可以从两个位置获取uid,一个来自auth,另一个来自idToken。uid:。无论如何,谢谢你的帮助。
request.auth.token.sub == userid;
request.auth.uid == userid;