Firebase`onAuthStateChanged`总是返回空用户

时间:2019-12-19 22:57:02

标签: javascript firebase firebase-authentication

这是我的checkout

index.html

这是我的<!DOCTYPE html> <html lang="en-US"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <head> <link rel='icon' href='favicon.ico' type='image/x-icon'/ > <link rel="stylesheet" href="./utilities/css/stylesTopNav.css"> <link rel="stylesheet" href="./utilities/css/stylesLoader.css"> <link rel="stylesheet" href="./utilities/css/stylesLogin.css"> <title>Login</title> <script src="https://www.gstatic.com/firebasejs/7.2.0/firebase-app.js"></script> <script src="https://www.gstatic.com/firebasejs/7.2.0/firebase-firestore.js"></script> <script src="https://www.gstatic.com/firebasejs/7.2.0/firebase-storage.js"></script> <script src="https://www.gstatic.com/firebasejs/7.2.0/firebase-auth.js"></script> </head> <body style="background:url(./utilities/images/waterBGGif.gif) fixed; background-size: cover;"> <div class="container" id="containerID"> <label for="Email"><b>Email</b></label> <input id="emailID" type="text" placeholder="Enter Email" required> <label for="psw"><b>Password</b></label> <input id="passwordID" type="password" placeholder="Enter Password" required> <button type="submit" id="loginButtonID" onclick="loginButtonAction('')">Log In</button> </div> <script src="./utilities/js/firebase.js"> </script> </body> </html>

firebase.js

我已经在Firebase身份验证中添加了电子邮件:

enter image description here

但是我的var firebaseConfig = { apiKey: "*******************", authDomain: "*******************", databaseURL: "*******************", projectId: "*******************", storageBucket: "*******************", messagingSenderId: "*******************", appId: "*******************" }; firebase.initializeApp(firebaseConfig); var firestore = firebase.firestore(); var firestorage = firebase.storage().ref(); function logOutButtonAction() { const logOutBtn = document.getElementById('logoutButtonID'); firebase.auth().signOut(); firebaseStateChange() } function loginButtonAction() { const txtEmail = document.getElementById('emailID'); const txtPass = document.getElementById('passwordID'); const logInBtn = document.getElementById('loginButtonID'); const email = txtEmail.value; const password = txtPass.value; const auth = firebase.auth(); const promise = auth.signInWithEmailAndPassword(email, password); promise.catch(e => console.log(e.message)); firebaseStateChange() } function firebaseStateChange() { firebase.auth().onAuthStateChanged(function(user) { if (user) { window.location.href = "product.html"; } else { window.location.href = "index.html"; } }); } 总是返回null。我在这里想念的是什么?提前致谢。

0 个答案:

没有答案