firebase.auth().onAuthStateChanged 登录后没有执行?
firebase.auth().onAuthStateChanged not executed after sign-in?
我使用以下方法在 firebase 上创建了用户:
Firebase Admin SDK(在 python 中)。用户出现在 firebase 控制台中。
现在我正尝试在登录页面上使用 javascript 登录用户。但是,当我 运行 下面显示的代码总是在我的脚本中的 else
循环中结束时,我认为这表明用户未登录。
期望的行为是:
用户输入 email/password,他们提交表单,然后登录,然后将他们重定向到索引页面。相反,它什么都不做。
<!doctype html>
<html lang="en">
<head>
<script src="https://www.gstatic.com/firebasejs/ui/4.5.1/firebase-ui-auth.js"></script>
<link type="text/css" rel="stylesheet" href="https://www.gstatic.com/firebasejs/ui/4.5.1/firebase-ui-auth.css" />
</head>
<body>
<script src="https://www.gstatic.com/firebasejs/7.15.1/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.15.1/firebase-analytics.js"></script>
<script>
const firebaseConfig = {
//my stuff here
};
firebase.initializeApp(firebaseConfig);
firebase.analytics();
</script>
<script src="https://www.gstatic.com/firebasejs/7.15.1/firebase-auth.js"></script>
<div class="container">
<h2>Sign In</h2>
<form id="loginform" action="" method="post">
<div class="form-group">
<label for="email">Email</label> <input class="form-control" id="email" name="email" required="required" type="text" value="cameron.snow@gmail.com">
</div>
<div class="form-group">
<label for="password">Password</label> <input class="form-control" id="password" name="password" required="required" type="password" value="">
</div>
<div class="form-group">
<input class="btn btn-primary" id="submit" name="submit" type="submit" value="Login">
</div>
</form>
<script>
document.getElementById('submit').addEventListener('click', loginUser);
function loginUser(){
let email = document.getElementById('email').value;
let password = document.getElementById('password').value;
firebase.auth().signInWithEmailAndPassword(email, password).catch(e=>{
alert(e)
});
};
firebase.auth().onAuthStateChanged(user=>{
if(user){
console.log(user)
user.getIdToken().then(function (token) {
//save the token in a cookie
document.cookie = "token=" + token;
window.location.href = "/";
});
} else{
console.log("What up dude?");
}
});
</script>
</div>
</body>
</html>
*编辑:
我已经在脚本中检查了我的 email
和 password
变量是否正常工作。我还检查了:
firebase.auth().signInWithEmailAndPassword(email, password).catch(e=>{
alert(e)});
piece 也在工作,return 是我所期望的...所以问题必须在 onAuthStateChanged()
部分。
您不应在 onAuthStateChanged()
中添加 window.location.href = "/";
,因为这会在索引页面中创建一个重新呈现的无限循环。
相反,您应该在成功登录后直接重定向到索引页面
firebase.auth().signInWithEmailAndPassword(email, password).then(()=> {
window.location.href = "/";
}).catch(e=>{
alert(e)});
我使用以下方法在 firebase 上创建了用户: Firebase Admin SDK(在 python 中)。用户出现在 firebase 控制台中。
现在我正尝试在登录页面上使用 javascript 登录用户。但是,当我 运行 下面显示的代码总是在我的脚本中的 else
循环中结束时,我认为这表明用户未登录。
期望的行为是: 用户输入 email/password,他们提交表单,然后登录,然后将他们重定向到索引页面。相反,它什么都不做。
<!doctype html>
<html lang="en">
<head>
<script src="https://www.gstatic.com/firebasejs/ui/4.5.1/firebase-ui-auth.js"></script>
<link type="text/css" rel="stylesheet" href="https://www.gstatic.com/firebasejs/ui/4.5.1/firebase-ui-auth.css" />
</head>
<body>
<script src="https://www.gstatic.com/firebasejs/7.15.1/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.15.1/firebase-analytics.js"></script>
<script>
const firebaseConfig = {
//my stuff here
};
firebase.initializeApp(firebaseConfig);
firebase.analytics();
</script>
<script src="https://www.gstatic.com/firebasejs/7.15.1/firebase-auth.js"></script>
<div class="container">
<h2>Sign In</h2>
<form id="loginform" action="" method="post">
<div class="form-group">
<label for="email">Email</label> <input class="form-control" id="email" name="email" required="required" type="text" value="cameron.snow@gmail.com">
</div>
<div class="form-group">
<label for="password">Password</label> <input class="form-control" id="password" name="password" required="required" type="password" value="">
</div>
<div class="form-group">
<input class="btn btn-primary" id="submit" name="submit" type="submit" value="Login">
</div>
</form>
<script>
document.getElementById('submit').addEventListener('click', loginUser);
function loginUser(){
let email = document.getElementById('email').value;
let password = document.getElementById('password').value;
firebase.auth().signInWithEmailAndPassword(email, password).catch(e=>{
alert(e)
});
};
firebase.auth().onAuthStateChanged(user=>{
if(user){
console.log(user)
user.getIdToken().then(function (token) {
//save the token in a cookie
document.cookie = "token=" + token;
window.location.href = "/";
});
} else{
console.log("What up dude?");
}
});
</script>
</div>
</body>
</html>
*编辑:
我已经在脚本中检查了我的 email
和 password
变量是否正常工作。我还检查了:
firebase.auth().signInWithEmailAndPassword(email, password).catch(e=>{
alert(e)});
piece 也在工作,return 是我所期望的...所以问题必须在 onAuthStateChanged()
部分。
您不应在 onAuthStateChanged()
中添加 window.location.href = "/";
,因为这会在索引页面中创建一个重新呈现的无限循环。
相反,您应该在成功登录后直接重定向到索引页面
firebase.auth().signInWithEmailAndPassword(email, password).then(()=> {
window.location.href = "/";
}).catch(e=>{
alert(e)});