Col*_*lin 12 html javascript authentication web firebase
我正在尝试建立一个为用户使用电子邮件身份验证的网站.但是,每次我尝试注册或登录用户时,firebase.auth().onAuthStateChanged函数都会触发,但无法识别用户已登录或注册.这是我目前的代码.我知道它有效,因为它会提醒我,"没有用户!" 每次登录或注册后,因为我可以进入我的Firebase控制台,看到用户已注册.如果有人知道如何解决这个问题,我将不胜感激!
谢谢!
码:
function initApp() {
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
alert("Signed in user!")
} else {
alert("No user!")
}
});
}
window.onload = function() {
initApp();
};
Run Code Online (Sandbox Code Playgroud)
登录和注册代码:
function toggleSignIn() {
if (firebase.auth().currentUser) {
alert("Sign out")
firebase.auth().signOut();
// [END signout]
} else {
var email = document.getElementById('email').value;
var password = document.getElementById('pass').value;
if (email.length < 4) {
alert('Please enter an email address.');
return;
}
if (password.length < 4) {
alert('Please enter a password.');
return;
}
// Sign in with email and pass.
// [START authwithemail]
firebase.auth().signInWithEmailAndPassword(email, password).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// [START_EXCLUDE]
if (errorCode === 'auth/wrong-password') {
alert('Wrong password.');
} else {
console.error(error);
}
// [END_EXCLUDE]
});
// [END authwithemail]
}
}
function handleSignUp() {
var email = document.getElementById('semail').value;
var password = document.getElementById('spass').value;
if (password.length < 6) {
alert('Password must be 6 characters or more!');
return;
}
// Sign in with email and pass.
// [START createwithemail]
firebase.auth().createUserWithEmailAndPassword(email, password).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// [START_EXCLUDE]
if (errorCode == 'auth/weak-password') {
alert('The password is too weak.');
} else {
console.error(error);
}
// [END_EXCLUDE]
});
// [END createwithemail]
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9889 次 |
| 最近记录: |