gpt4 book ai didi

javascript - 用户使用 Firebase 身份验证登录后如何重定向?

转载 作者:数据小太阳 更新时间:2023-10-29 04:36:15 24 4
gpt4 key购买 nike

如何在用户登录后重定向到其他网页?

目前,当用户登录时,会检索数据,但不会将用户重定向到其他网站。

我知道我应该使用“getRedirectResult”,但有人可以告诉我如何使用它以及它如何将用户重定向到不同的网页,同时维护检索到的用户数据。

我的 JavaScript 工作:

function toggleSignIn() {
if (!firebase.auth().currentUser) {
// [START createprovider]
var provider = new firebase.auth.GoogleAuthProvider();
// [END createprovider]
// [START addscopes]
provider.addScope('https://www.googleapis.com/auth/plus.login');
// [END addscopes]
// [START signin]
firebase.auth().signInWithPopup(provider).then(function(result) {
// This gives you a Google Access Token. You can use it to access the Google API.
var token = result.credential.accessToken;
// The signed-in user info.
var user = result.user;
// [START_EXCLUDE]
document.getElementById('quickstart-oauthtoken').textContent = token;
// [END_EXCLUDE]
}).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// The email of the user's account used.
var email = error.email;
// The firebase.auth.AuthCredential type that was used.
var credential = error.credential;
// [START_EXCLUDE]
if (errorCode === 'auth/account-exists-with-different-credential') {
alert("You have already signed up with a different auth provider for that email.");
// If you are using multiple auth providers on your app you should handle linking
// the user's accounts here.
}
else if (errorCode === 'auth/auth-domain-config-required') {
alert("An auth domain configuration is required");
}
else if (errorCode === 'auth/cancelled-popup-request') {
alert("Popup Google sign in was canceled");
}
else if (errorCode === 'auth/operation-not-allowed') {
alert("Operation is not allowed");
}
else if (errorCode === 'auth/operation-not-supported-in-this-environment') {
alert("Operation is not supported in this environment");
}
else if (errorCode === 'auth/popup-blocked') {
alert("Sign in popup got blocked");
}
else if (errorCode === 'auth/popup-closed-by-user') {
alert("Google sign in popup got cancelled");
}
else if (errorCode === 'auth/unauthorized-domain') {
alert("Unauthorized domain");
}
else {
console.error(error);
}
// [END_EXCLUDE]
});
// [END signin]
} else {
// [START signout]
firebase.auth().signOut();
// [END signout]
}
// [START_EXCLUDE]
document.getElementById('quickstart-sign-ing').disabled = false;
// [END_EXCLUDE]
}

最佳答案

从用户那里获取电子邮件和密码,然后将值传递给 signInWithEmailAndPassword,如果发生错误,它将打印消息,否则 Firebase 将成功登录用户。

firebase.auth().signInWithEmailAndPassword(email, password).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
console.log(error.Message);

});

您还需要一个处理登录和注销状态的监听器。如果用户已成功登录,您可以在此处重定向用户。

要处理登录和注销,请始终使用 onAuthStateChanged()

//Handle Account Status
firebase.auth().onAuthStateChanged(user => {
if(user) {
window.location = 'home.html'; //After successful login, user will be redirected to home.html
}
});

当有人登录时,user 将填充用户详细信息,您可以使用它重定向到另一个页面。

关于javascript - 用户使用 Firebase 身份验证登录后如何重定向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38987695/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com