我正在尝试制作一个网站,在那里我单击一个按钮与Google登录,然后转到一个新的HTML页面。
然而,当新的Google登录窗口弹出时,它是 闭合的,而HTML页面更改。
我确实希望HTML页面更改,但是:我需要Google登录
首先工作?有没有办法等到用户登录之前登录到新页面?
我在想是否有一种布尔方法来看看登录成功,但我找不到任何方法。
你能帮我吗?
<html>
<head>
//connect to css and javascript file
<link rel="stylesheet" href="hackathon.css">
<script src=scripts.js></script>
</head>
<script src="https://www.gstatic.com/firebasejs/4.1.3/firebase.js"></script>
<script>
// Initialize Firebase
var config = {
apiKey: "AIzaSyAOWTFIhpydI27RMa_JzOW0Ffyajb9sTqg",
authDomain: "teach-and-learn-cd50e.firebaseapp.com",
databaseURL: "https://teach-and-learn-cd50e.firebaseio.com",
projectId: "teach-and-learn-cd50e",
storageBucket: "",
messagingSenderId: "412721016471"
};
firebase.initializeApp(config);
</script>
<br><br><br><br><br><br><br><br><br>
//create the header
<p><center><font face="Verdana" size="7" color="white">Teach and Learn</font></p>
//login button
<button class="button1">Log In</button>
//google sign in
<script type="text/javascript">
button=document.querySelector('button')
database=firebase.database()
reference=database.ref()
button.onclick=function(){
var provider = new firebase.auth.GoogleAuthProvider();
firebase.auth().signInWithPopup(provider).then(function(result) {
var token = result.credential.accessToken;
var user = result.user;
}).catch(function(error) {
var errorCode = error.code;
var errorMessage = error.message;
var email = error.email;
var credential = error.credential;
})
//immediately goes to this page without giving the use
//opportunity to sign in
//main.html is loaded and the sign in window is closed
window.location.href="./main.html";
</script>
</html>
如果登录成功,则function(result)
内部的代码将运行。如果登录失败,则function(error)
内部的代码将运行。then
中的任何内容都将等到您尝试登录。将重定向代码放在当时的内部。
我在此编码器上修改了您的代码,因此您可以自己尝试一下。摆脱代码中的第二个window.location.href = "./main.html";
非常重要。
https://codepen.io/anon/pen/mooeyv?editors=1000
将window.location.href移动到回调?
firebase.auth().signInWithPopup(provider).then(function(result) {
//..
window.location.href="./main.html";
}).catch(function(error) {
//..
});