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