Firebase:如果身份验证正确,如何转发到新网页?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Firebase:如果身份验证正确,如何转发到新网页?相关的知识,希望对你有一定的参考价值。
在我的Web应用程序中,身份验证过程通过firebase
处理。我在这个过程中使用firebase
javascript api。在我的html中,我通过username
和password
登录。这些信息由Javascript捕获并发送到firebase。
</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
,它将为登录用户提供下一个UI。
我怎样才能做到这一点?
答案
在then
有signInWithEmailAndPassword
。请看下面。
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;
}
以上是关于Firebase:如果身份验证正确,如何转发到新网页?的主要内容,如果未能解决你的问题,请参考以下文章
如果用户已经使用 Vue 和 Firebase 身份验证登录,如何自动重定向用户?
读取引发的 Firebase 身份验证错误(Firebase 3.x 和 Swift)
Unity/Firebase 如何使用 Google 进行身份验证?