我无法使用我的网络应用程序使用firebase对用户进行身份验证。每当我尝试登录firebase.auth()。signInWithEmailAndPassword(email,pass);调用函数,请求成功并触发authStateChanged侦听器。但是,在侦听器内部,firebaseUser始终返回null。
我尝试从个人服务器托管Web应用程序,并允许域访问我的firebase数据库。
我还有一个Android应用程序,可以与同一个DB完美地工作。
这是我的代码:
login.js:
(function() {
// Initialize Firebase
var config = {
//CONFIG INFORMATION HERE
};
firebase.initializeApp(config);
// Get elements
const txtEmail = document.getElementById('username');
const txtPassword = document.getElementById('password');
const loginButton = document.getElementById('loginButton');
loginButton.addEventListener('click', e => {
const email = txtEmail.value;
const pass = txtPassword.value;
const auth = firebase.auth();
const promise = auth.signInWithEmailAndPassword(email, pass);
promise.catch(e => console.log(e.message));
});
firebase.auth().onAuthStateChanged(firebaseUser => {
if(firebaseUser){
console.log(firebaseUser);
}
else{
console.log('not logged in');
}
})
}());
登录html:
<!DOCTYPE html>
<html>
<head>
<title>Athlete Buddy Login</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<link rel="stylesheet" type="text/css" href="allStyleSheet.css">
<link rel="stylesheet" type="text/css" href="login.css">
<link rel="icon" type="image/gif" href="pictures/ABLogo.png">
<script src="https://www.gstatic.com/firebasejs/live/3.1/firebase.js"></script>
</head>
<body>
<nav>
<a href="#">Home</a>
<a href="#">Login</a>
<a href="#">Features</a>
</nav>
<img src="pictures/loginArt.png" id="loginTitle">
<div id="loginFormDiv">
<form id="loginForm">
Username: <input id="username" class="field" type="text" name="Username"> <br><br>
Password: <input id="password" class="field" type="Password" name="Password"> <br><br>
<input id="loginButton" type="submit" value="Login">
<a id="createAccountButton" href="#">Create Account</a>
<a id="forgotPassword" href="http://www.youtube.com/watch?v=dQw4w9WgXcQ">forgot password?</a>
</form>
</div>
<script src="login.js"></script>
</body>
</html>
答案 0 :(得分:0)
您没有从按钮点击处理程序返回任何内容,也没有阻止它的默认行为。这意味着该按钮会提交表单,从而导致页面刷新。
解决方案很简单:
void scannerData (int reqId, int rank, ContractDetails contractDetails, string distance, string benchmark, string projection, string legsStr)
我还添加了一个额外的 loginButton.addEventListener('click', e => {
e.preventDefault();
const email = txtEmail.value;
const pass = txtPassword.value;
const auth = firebase.auth();
const promise = auth.signInWithEmailAndPassword(email, pass);
promise.then(u => console.log(u));
promise.catch(e => console.log(e.message));
return false;
});
处理程序,这样可以更容易地看到它正在运行。但你真的应该像{Q}一样处理then()
中的身份验证状态。
请参阅此jsbin:https://output.jsbin.com/waxixe/1