登录后,页面刷新而不是转到下一页

时间:2018-01-23 23:30:16

标签: javascript html firebase firebase-authentication

我正在开发一个使用Firebase作为我的后端的网站,用户已登录但登录后我无法转到下一页我需要它来取用户,它只是刷新登录页面。我有点教这个JavaScript,所以任何帮助都会很棒。

提前致谢。

登录页面:

<div class="container">
        <div class="row">
            <div class="col-md-3"></div>

            <div class="col-md-6">


                <p>Enter an email and password below and either sign in to an existing account or sign up</p>

                <input class="mdl-textfield__input" style="display: inline; width: auto;" type="text" id="email" name="email" placeholder="Email" />
                &nbsp;&nbsp;&nbsp;
                <input class="mdl-textfield__input" style="display: inline; width: auto;" type="password" id="password" name="password" placeholder="Password" />
                <br />
                <br />
                <button class="mdl-button mdl-js-button mdl-button--raised" id="quickstart-sign-in" name="signin">Sign In</button>
                &nbsp;&nbsp;&nbsp;


            </div>

            <div class="col-md-3"></div>
        </div>

    </div>



    <script src="https://www.gstatic.com/firebasejs/4.9.0/firebase-app.js"></script>
    <script src="https://www.gstatic.com/firebasejs/4.9.0/firebase-auth.js"></script>
    <script src="https://www.gstatic.com/firebasejs/4.9.0/firebase-database.js"></script>
    <script src="https://www.gstatic.com/firebasejs/4.9.0/firebase.js"></script>

    <script>
        // Initialize Firebase
        var config = {
            //
        };
        firebase.initializeApp(config);
    </script>

    <script type="text/javascript">
        /**
         * Handles the sign in button press.
         */
        function toggleSignIn() {

                var email = document.getElementById('email').value;
                var password = document.getElementById('password').value;

                if (email.length < 4) {
                    alert('Please enter an email address.');
                    return;
                }
                if (password.length < 4) {
                    alert('Please enter a password.');
                    return;
                }
                // Sign in with email and pass.
                // [START authwithemail]
                firebase.auth().signInWithEmailAndPassword(email, password).catch(function (error) {
                    // Handle Errors here.
                    var errorCode = error.code;
                    var errorMessage = error.message;
                    // [START_EXCLUDE]
                    if (errorCode === 'auth/wrong-password') {
                        alert('Wrong password.');
                    } else {
                        alert(errorMessage);
                    }
                    console.log(error);


                });


        }


        function initApp() {
            firebase.auth().onAuthStateChanged(function (user) {
                if (user) {

                    alert("welcome");
                    window.location = '../UserPages/UserIndex.aspx'; 

                }
            });

            document.getElementById('quickstart-sign-in').addEventListener('click', toggleSignIn, false);
        }

        window.onload = function () {
            initApp();
        };
  </script>

1 个答案:

答案 0 :(得分:0)

为什么不呢。 document.location.href =&#39; Gray pop up box&#39;而是在widows.location上。 &#34;警报(&#34;欢迎&#34);                     window.location =&#39; ../ UserPages / UserIndex.aspx&#39 ;;

            }  "