1

我正在构建vuejs应用程序,并且正在尝试将Firebase验证添加到应用程序。我使用云端firestore来建立新闻系统。Firebase云防火墙+验证:只写登录用户

现在的“添加新闻”页上的我叫

firebase.auth().signInWithEmailAndPassword("[email protected]", "mypassword").catch(function (error) { 
    console.log(error.code); 
    console.log(error.message); 

    if (errorCode === 'auth/wrong-password') { 
    alert('Wrong password.'); 
    } else { 
    alert(errorMessage); 
    } 
}); 

登录用户,或给他的反馈,如果出事了。

后来我喜欢这个

irestore.collection("news").doc().set({ 
    date: today, 
    title: "Hello", 
    text: "A think I am a news!" 
}); 

写数据到云公司的FireStore在公司的FireStore规则我设置

allow read, write: if request.auth != null; 

的新闻收集 - 所以应该只授予登录用户的写权限, 对?

现在事情: 如果我有一个错误的密码登录,火力点还给,该密码是不正确的(所以我们不都是我们登录?),但该数据被写入到反正公司的FireStore。我做错了什么?

enter image description here

回答

1

您的规则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. 
    } 
}); 

如果你得到一个user那么你可以指望request.auth到不null