mul*_*sie 7 javascript firebase firebase-authentication
用户登录后如何重定向到其他网页?
目前,当用户登录时,会检索数据,但不会将用户重定向到其他网站。
我知道我应该使用“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]
}
Run Code Online (Sandbox Code Playgroud)
您只需要在 initApp() 中添加两个重定向即可实现此目的。我指的是快速入门 git repo https://github.com/firebase/quickstart-js/blob/master/auth/email.html,因为我是通过重复问题Redirecting to a page after Firebase login - Javascript来到这里的
1) 在第 163 行,添加您希望登录用户重定向到的页面:
162 //User is signed in.
163 window.location = ‘loggedIn.html’;
164 var displayName = user.displayName;
Run Code Online (Sandbox Code Playgroud)
2) 在第 180 行(现在是 181,因为上面添加了),添加重定向回登录页面:
180 // User is signed out.
181 window.location = ‘index.html’;
182 // [START_EXLUDE]
Run Code Online (Sandbox Code Playgroud)
您需要将整个脚本添加到索引和登录页面中 - 因此原始 git 存储库中第 37 行和第 201 行之间的所有内容(包括脚本标签)(但添加了上面的两个重定向)。
现在,如果您尝试直接进入loggedIn.html 而不登录,initApp 函数将检查您是否登录,查看您是否登录,并将您重定向到登录页面。
唯一的问题是您在重定向之前会快速浏览记录的内容,因此要解决此问题,您可以将此页面的正文设置为隐藏,并在用户登录时运行脚本以删除隐藏的内容班级。
最后,您需要在 toggleSignIn 函数中添加重定向,位于原始存储库的第 46 行:
45 firebase.auth().signOut();
46 window.location = ‘index.html’;
47 // [END signout]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18425 次 |
| 最近记录: |