简体   繁体   中英

Local http-server is not connecting to the firebase user

I am using Visual Studio Code editor and node js HTTP server. I have created the a simple login page and created a user in firebase . but i didn't able to login using the username. I wrote code for web page in HTML and bootstrap.

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 file

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

app.js File

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>

The line firebase.auth.Auth.Persistence.LOCAL is causing error, there are two problems.

  1. The default value is already LOCAL , so it is not necessary to define it.
  2. The proper way to define persistence is firebase.auth().setPersistence(firebase.auth.Auth.Persistence.LOCAL)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM