Firebase安全规则无法识别request.auth.token.name
我想使用用户的显示名称来应用安全规则。 我使用了signInAnonymously和updateProfile。
这是我的客户代码
var firebase = require('firebase/app')
require('firebase/auth')
require('firebase/firestore')
firebase.initializeApp(~~~)
firebase.auth().signInAnonymously().then(({user}) => {
user.updateProfile({
displayName: 'ABC',
}).then(() => {
// {..., uid: '~~~', displayName: 'ABC', photoURL: null}
console.log(user.toJSON())
var db = firebase.firestore()
db.collection('HelloCollection').doc('WorldDoc').get()
.then((doc) => { console.log('Success') })
.then((err) => { console.log('Failed', err) })
})
})
这是我的Firestore安全规则
service cloud.firestore {
match /databases/{database}/documents {
match /HelloCollection/{docId} {
// allow read, create, update: if true; // This evaluated true.
// This evaluated false.
allow read, create, update: if request.auth.token.name == "ABC";
}
}
}
我期望“成功”, 但实际输出是
Failed FirebaseError: Missing or insufficient permissions.
at new FirestoreError (webpack-internal:///3120:352:28)
at JsonProtoSerializer.fromRpcStatus (webpack-internal:///3120:14802:16)
at JsonProtoSerializer.fromWatchChange (webpack-internal:///3120:15293:44)
at PersistentListenStream.onMessage (webpack-internal:///3120:11280:43)
at eval (webpack-internal:///3120:11209:30)
at eval (webpack-internal:///3120:11249:28)
at eval (webpack-internal:///3120:1630:20)
at run (webpack-internal:///1421:66:22)
at eval (webpack-internal:///1421:79:30)
at MutationObserver.flush (webpack-internal:///358:18:9)
答案 0 :(得分:0)
我无法直接解决此问题,但通过使其通过UserID而不是User name进行身份验证来绕过该问题,如下所示:
request.resource.data.uid == request.auth.token.sub
请注意,也应更改查询HelloCollection,以便存储带有文本的用户ID。
答案 1 :(得分:0)
这是一个工作示例..
match /stuff/{candy} {
allow read, write: if request.auth.token.name == candy
}
@fre bern 是对的。您可以通过“request.auth.token.name”访问用户的显示名称..如果你不能让它工作.. mb sth你的代码有问题