【发布时间】:2019-06-03 21:27:31
【问题描述】:
我设置了 Firebase 函数来调用我的 Firestore。我正在使用 admin.auth() 并返回数据。我在 Firestore 规则部分设置了自定义规则,但函数没有遵循规则,即当我在 Postman 中使用 URL 时,我不应该获取数据,因为它不满足“如果读取,写入:如果请求” .auth != null”。我该如何解决这个问题?
这是我的 Firebase 功能代码:
const admin = require('firebase-admin');
module.exports = function(req, res) {
const uid = req.body.uid;
admin
.auth()
.getUser(uid)
.then(user => {
admin
.firestore()
.collection('discover')
.get()
.then(snapshot => {
res.send(
snapshot.docs.map(doc => {
const data = Object.assign({ doc_id: doc.id }, doc.data());
return data;
})
);
})
.catch(err => {
res.send({ message: 'Something went wrong!', success: false });
});
})
.catch(err => {
res.send({ error: 'Something went wrong!', success: false });
});
};
Firestore 规则:
service cloud.firestore {
match /databases/{database}/documents {
match /users/{users} {
allow read: if request.auth != null;
}
match /discover/{discover} {
allow read: if request.auth != null;
}
match /favorites/{favorite} {
allow read, write: if request.auth != null;
}
}
}
我应该无法从 Postman 那里获取这些数据(因为我没有经过身份验证),但我仍然在获取这些数据。如果用户未登录,我不希望数据可以访问。
【问题讨论】:
标签: firebase react-native google-cloud-firestore firebase-security