本地http服务器未连接到Firebase用户

时间:2018-07-25 04:05:45

标签: html node.js firebase firebase-authentication simplehttpserver

我正在使用Visual Studio代码编辑器和节点js HTTP服务器。我创建了一个简单的登录页面,并在firebase中创建了一个用户。但我无法使用用户名登录。我用HTML和引导程序为网页编写了代码。

index.html

<!DOCTYPE html>
<html>
<head>       
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous" />
    <script src="http://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha256-3edrmyuQ0w65f8gfBsqowzjJe2iM6n0nKciPUp8y+7E="  crossorigin="anonymous"></script>
    <link rel="stylesheet" href="css/style.css" />
    <script src="https://www.gstatic.com/firebasejs/5.3.0/firebase.js"></script>
</head>
<body class="bg-dark">
    <div id="login-card" class="card">                
        <div class="card-body">
            <h1>Wallpaper App Admin</h1>
            <form id="login-form">
                <div class="form-group">
                    <label for="email">email</label>
                    <input type="email" id="email" class="form-control"/>
                </div>
                <div class="form-group">
                        <label for="password">password</label>
                        <input type="password" id="password" class="form-control"/>
                </div>
                <div class="form-group">
                    <button type="button" id="btn-login" class="btn btn-primary">Login</button>
                </div>
            </form>
        </div>
    </div> 
    <script src="js/app.js"></script>
    <script>
        firebase.auth().onAuthStateChanged(function(user){
            if(user){
                window.location.href="admin.html";
            }
        });
    </script>
</body>
</html>

style.css文件

#login-card{
width:450px;
margin:150px auto;
}

app.js文件

var config = {
    apiKey: "AIzaSyBF2wl4WHBbLHg90M3lAk_dNKZ7SAo0iE8",
    authDomain: "wallpaper-app-b7c7b.firebaseapp.com",
    databaseURL: "https://wallpaper-app-b7c7b.firebaseio.com",
    projectId: "wallpaper-app-b7c7b",
    storageBucket: "wallpaper-app-b7c7b.appspot.com",
    messagingSenderId: "302819877773"
  };
  firebase.initializeApp(config);
firebase.auth.Auth.persistence.LOCAL;

$("#btn-login").click(function(){

    var email = $("#email").val();
    var password = $("#password").val();

    var result = firebase.auth().signInWithEmailAndPassword(email, password);

    result.catch(function(error){
        var errorCode = error.code;
        var errorMessage = error.message;
        console.log(errorCode);
        console.log(errorMessage);
    });
});

admin.html

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous" />
    <script src="http://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha256-3edrmyuQ0w65f8gfBsqowzjJe2iM6n0nKciPUp8y+7E="  crossorigin="anonymous"></script>
    <link rel="stylesheet" href="css/style.css" />
    <script src="https://www.gstatic.com/firebasejs/5.3.0/firebase.js"></script>
</head>
<body>
    <h1>This is the Admin Page</h1>
    <script src="js/app.js"></script>
    <script>
        firebase.auth().onAuthStateChanged(function(user){
            if(!user){
                window.location.href="index.html";
            }
        });
    </script>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

firebase.auth.Auth.Persistence.LOCAL行引起错误,有两个问题。

  1. 默认值已经为LOCAL,因此没有必要对其进行定义。
  2. 定义持久性的正确方法是 firebase.auth().setPersistence(firebase.auth.Auth.Persistence.LOCAL)