我正在创建一个没有任何理由让用户进行身份验证的移动应用程序。但是,我不希望其他人编写可以访问我在 Firestore 中的数据或调用我的任何云函数的应用程序或网站。
这是否意味着我需要实现匿名身份验证,然后编写要求请求来自经过身份验证的用户的访问规则?
或者有没有办法编写规则来说明它们必须来自我的应用程序?
最佳答案
我们遇到了同样的问题:一个部署给许多人的应用程序,它只需要能够读取 Firestore 文档,但另一个不会分发的管理应用程序(实际上只是一个网页),需要能够编写到那些文件。
我们的解决方案是:
allow read;
allow write: if request.auth.uid == 'notshowingyououridhere-sorry';
我们使用 ionic 和 typescript,所以进行用户/密码登录的代码相对简单:
firebase.initializeApp(credentials);
firebase.auth()
.signInWithEmailAndPassword('obfuscated@sorry.com', 'sorrynotshowingyouourpassword')
.catch(err => {
console.log('Something went wrong:', err.message);
});
firebase.auth().onAuthStateChanged((user) => {
if (user) {
// User is signed in.
const isAnonymous = user.isAnonymous;
const uid = user.uid;
console.log('onAuthStatChanged: isAnon', isAnonymous);
console.log('userid', user.uid);
} else {
console.log('onAuthStateChanged: else part...');
}
});
希望这可以帮助。
关于firebase - 使用 Firebase 匿名身份验证是否允许我确保我的 Firestore 数据和云函数只能由我的应用程序访问?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51414771/