Firebase: как перейти на новую веб-страницу, если аутентификация правильная? - PullRequest
0 голосов
/ 14 апреля 2019

В моем веб-приложении процесс аутентификации обрабатывается с помощью firebase.Я использую firebase API Java для этого процесса.В моем HTML я передаю username и password для входа.Эта информация захвачена Javascript и отправлена ​​в базу данных.

</head>
<!-- Firebase App (the core Firebase SDK) is always required and must be listed first -->
        <script src="https://www.gstatic.com/firebasejs/5.9.4/firebase-app.js"></script>

        <!-- Add Firebase products that you want to use -->
        <script src="https://www.gstatic.com/firebasejs/5.9.4/firebase-auth.js"></script>
        <script src="https://www.gstatic.com/firebasejs/5.9.4/firebase-database.js"></script>

        <script>
            // Initialize Firebase
            var config = {
            apiKey: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
            authDomain: "xxxxxxxxxxxxxx-xxxxxxx.firebaseapp.com",
            databaseURL: "https://xxxxxxx-xxxxxxxxxx.firebaseio.com",
            projectId: "xxxxxxxx-xxxxxxxxx",
            storageBucket: "xxxxxxxxxx-xxxxxxxxxx.appspot.com",
            messagingSenderId: "xxxxxxxxxxxxxxxxxxx"
            };
            firebase.initializeApp(config);
        </script>

        <script type="text/javascript">

            function toggleSignIn() 
            {
                if (firebase.auth().currentUser) {
                // [START signout]
                alert('same user');
                //firebase.auth().signOut();
                // [END signout]
                } else {
                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);
                document.getElementById('quickstart-sign-in').disabled = false;
                // [END_EXCLUDE]
                });
                alert('hi');
                // [END authwithemail]
                }
                document.getElementById('quickstart-sign-in').disabled = true;

            }
        </script>
</head>

Здесь я фиксирую ошибки, и это нормально.Но если имя пользователя и пароль верны, мне нужно переслать servlet с именем LoadUsers, который предоставит следующий пользовательский интерфейс для вошедшего в систему пользователя.

Как я могу это сделать?

1 Ответ

0 голосов
/ 14 апреля 2019

В signInWithEmailAndPassword есть then. Просто проверьте ниже.

function toggleSignIn() 
            {
                if (firebase.auth().currentUser) 
                {
                    // [START signout]
                    alert('same user');
                    //firebase.auth().signOut();
                    // [END signout]
                } else {
                    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).then(function(firebaseUser) 
                    {
                        window.location.href = 'LoadSellPendingApprovals'
                    })

                    .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);
                        document.getElementById('quickstart-sign-in').disabled = false;
                    // [END_EXCLUDE]
                    });
                alert('hi');
                // [END authwithemail]
                }
                document.getElementById('quickstart-sign-in').disabled = true;

            }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...