我正在构建一个 vuejs 应用程序,我正在尝试向该应用程序添加 firebase 身份验证。我将 Cloud Firestore 用于新闻系统。
现在在我调用的“添加新闻”页面上
firebase.auth().signInWithEmailAndPassword("john@doe.com", "mypassword").catch(function (error) {
console.log(error.code);
console.log(error.message);
if (errorCode === 'auth/wrong-password') {
alert('Wrong password.');
} else {
alert(errorMessage);
}
});
让用户登录,或者在出现问题时向他提供反馈。
以后我是这样写数据到cloud firestore
irestore.collection("news").doc().set({
date: today,
title: "Hello",
text: "A think I am a news!"
});
在我设置的firestore规则中
allow read, write: if request.auth != null;
新闻集 - 所以它应该只向登录用户授予写入权限,对吧?
现在的事情: 如果我使用错误的密码登录,firebase 会返回密码不正确(所以我们没有登录,是吗?)但数据无论如何都会写入 firestore。我做错了什么?
最佳答案
您的规则 request.auth != null
将检查用户是否通过任何方法登录。您可以通过两种方式在客户端检查签名:
同步:
// Synchronously check for current user
var user = firebase.auth().currentUser;
if (user) {
// User is signed in.
} else {
// No user is signed in.
}
异步:
// Listen for current user status
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
// User is signed in.
} else {
// No user is signed in.
}
});
如果您得到一个用户
,那么您可以预期request.auth
不会是null
。
关于javascript - Firebase 云 Firestore + 身份验证 : write only for signed in users,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46851217/