我在使用Firebase與我的Web應用程序驗證用戶身份時遇到問題。每當我嘗試登錄firebase.auth()。signInWithEmailAndPassword(email,pass);函數被調用,請求成功並觸發authStateChanged偵聽器。但是,在偵聽器內部firebaseUser總是返回null。Firebase身份驗證成功,但返回空用戶(Web)
我已經嘗試從個人服務器託管Web應用程序,並允許域訪問我的Firebase數據庫。
我也有一個android應用程序,完美地運行相同的數據庫。
這裏是我的代碼:
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>
這似乎不大可能,權威性會失敗,而不觸發'趕上()'。你可以在jsbin中設置它的複製,所以我可以看看嗎? –
https://jsbin.com/sonenup/1 –
我以前從來沒有用過jsbin,很可能是我做錯了登錄憑證:[email protected]密碼:testing123 –