我是新用於Web的Firebase身份驗證。我設法獲得了註冊表單的工作方式,但我遇到了發送電子郵件以驗證用戶電子郵件地址的問題。用戶創建爲正常的,出現在我的控制檯,但沒有確認電子郵件的發送和 我碰到下面的錯誤在Chrome:Firebase身份驗證網站:如何驗證電子郵件地址
Uncaught TypeError: Cannot read property 'sendEmailVerification' of null
這裏是我的代碼至今:
"use strict";
(function() {
// initialize firebase
var config = {
apiKey: "//api key",
authDomain: "// etc",
databaseURL: "// etc",
projectId: "// etc",
storageBucket: "// etc",
messagingSenderId: "// etc"
};
firebase.initializeApp(config);
// get the login form elements from HTML
var txtEmail = document.getElementById('txtEmail');
var txtPassword = document.getElementById('txtPassword');
var btnLogin = document.getElementById('btnLogin');
var btnSignUp = document.getElementById('btnSignUp');
var btnLogout = document.getElementById('btnLogout');
// add login event
btnLogin.addEventListener('click', function (e) {
// get email and pass
var email = txtEmail.value;
var pass = txtPassword.value;
var auth = firebase.auth();
// sign in
var promise = auth.signInWithEmailAndPassword(email, pass);
promise.catch(function (e) {
return console.log(e.message);
});
});
// add signup event. the signup button sends user email and pass to firebase
btnSignUp.addEventListener('click', function (e) {
// get email and pass
// TODO: validate email - check it is real
var email = txtEmail.value;
var pass = txtPassword.value;
var auth = firebase.auth();
var user = firebase.auth().currentUser;
// sign in
var promise = auth.createUserWithEmailAndPassword(email, pass);
promise.catch(function (e) {
return console.log(e.message);
}).then(function(){user.sendEmailVerification().then(function() {
// Email sent.
}, function(error) {
// An error happened.
})
}); // end verification
}); // end sign up button event listener
// show logout button when user is logged in
// TODO: make sure the login form clears the credentials on logout
btnLogout.addEventListener('click', function (e) {
firebase.auth().signOut();
});
// realtime authentication listener
firebase.auth().onAuthStateChanged(function (firebaseUser) {
if (firebaseUser) {
console.log(firebaseUser);
btnLogout.classList.remove('hide');
btnSignUp.classList.add('hide');
btnLogin.classList.add('hide');
} else {
console.log('not logged in');
btnLogout.classList.add('hide');
btnSignUp.classList.remove('hide');
btnLogin.classList.remove('hide');
} // end if statement
}); // end function
})();
我還沒有找到一個簡單的例子來說明如何做到這一點,文檔只是提供了代碼,但沒有提供信息。我感謝您的幫助。謝謝。
已解決: 謝謝你的幫助。現在正在工作。以下是完整的工作代碼:
"use strict";
(function() {
// initialize firebase
var config = {
apiKey: "",
authDomain: "",
databaseURL: "",
projectId: "",
storageBucket: "",
messagingSenderId: ""
};
firebase.initializeApp(config);
// get the login form elements from HTML
var txtEmail = document.getElementById('txtEmail');
var txtPassword = document.getElementById('txtPassword');
var btnLogin = document.getElementById('btnLogin');
var btnSignUp = document.getElementById('btnSignUp');
var btnLogout = document.getElementById('btnLogout');
// login event
btnLogin.addEventListener('click', function (e) {
// get email and pass
var email = txtEmail.value;
var pass = txtPassword.value;
var auth = firebase.auth();
// sign in
var promise = auth.signInWithEmailAndPassword(email, pass);
promise.catch(function (e) {
return console.log(e.message);
});
});
// add signup event. the signup button sends user email and pass to firebase
btnSignUp.addEventListener('click', function (e) {
// get email and pass
// TODO: validate email - check it is real
var email = txtEmail.value;
var pass = txtPassword.value;
var auth = firebase.auth();
var user = firebase.auth().currentUser;
// create user and sign in
var promise = auth.createUserWithEmailAndPassword(email, pass);
promise.then(function(user) {
user.sendEmailVerification().then(function() {
// Email sent.
}, function(error) {
// An error happened.
});
});
}); // end sign up button event listener
// show logout button when user is logged in
btnLogout.addEventListener('click', function (e) {
firebase.auth().signOut();
});
// realtime authentication listener
firebase.auth().onAuthStateChanged(function (firebaseUser) {
if (firebaseUser) {
console.log(firebaseUser);
btnLogout.classList.remove('hide');
btnSignUp.classList.add('hide');
btnLogin.classList.add('hide');
} else {
console.log('not logged in');
btnLogout.classList.add('hide');
btnSignUp.classList.remove('hide');
btnLogin.classList.remove('hide');
} // end else statement
}); // end function
})();
謝謝,該代碼有效。這是我更新的代碼正在工作: //創建用戶並登錄 var promise = auth.createUserWithEmailAndPassword(email,pass); promise.then(功能(用戶){ user.sendEmailVerification(),然後(函數(){// 電子郵件發送 },函數(誤差){// 錯誤發生 });。 } ); // end promise }); //結束註冊按鈕事件監聽器 – greynolds