在本章中,我們將向您介紹如何使用Firebase電子郵件/密碼身份驗證。在開始之前,需要設置登錄方法,參考以下圖所示 -
點擊設置登錄方法,然后選擇電子郵件地址/密碼,打開啟用并保存,如下圖所示 -
要驗證用戶,可以使用createUserWithEmailAndPassword(email,password)方法。
示例
讓我們來看看下面的一個例子。參考代碼 -
var email = "test@yiibai.com";
var password = "mypassword";
firebase.auth().createUserWithEmailAndPassword(email, password).catch(function(error) {
console.log(error.code);
console.log(error.message);
});
接下來,您可以檢查Firebase儀表板中的數(shù)據(jù)庫,并查看用戶是否已創(chuàng)建成功。如下所示,已經創(chuàng)建了一個用戶賬號 -
登錄過程與我們常見的登錄方式幾乎相同。 使用signInWithEmailAndPassword(email, password)登錄用戶。
示例
讓我們看看下面的一個例子。演示如何登錄 -
var email = "test@yiibai.com";
var password = "mypassword";
firebase.auth().signInWithEmailAndPassword(email, password).catch(function(error) {
console.log(error.code);
console.log(error.message);
});
最后,可以使用signOut()方法注銷已登錄的用戶。
示例
讓我們看看下面的一個例子。演示如何注銷已登錄的用戶 -
firebase.auth().signOut().then(function() {
console.log("Logged out!")
}, function(error) {
console.log(error.code);
console.log(error.message);
});