Javascript Firebase Google登录(Web)流
大家好。这是我的代码,我有两个问题 首先,我想知道我的登录流是否正确 我的逻辑是当用户进入我的索引页时 我将首先检查用户登录状态 若用户未登录,则要求用户登录Javascript Firebase Google登录(Web)流,javascript,web,firebase,firebase-authentication,Javascript,Web,Firebase,Firebase Authentication,大家好。这是我的代码,我有两个问题 首先,我想知道我的登录流是否正确 我的逻辑是当用户进入我的索引页时 我将首先检查用户登录状态 若用户未登录,则要求用户登录 function google_login_in(){ var provider = new firebase.auth.GoogleAuthProvider(); provider.addScope('https://www.googleapis.com/auth/plus.login');
function google_login_in(){
var provider = new firebase.auth.GoogleAuthProvider();
provider.addScope('https://www.googleapis.com/auth/plus.login');
firebase.auth().signInWithPopup(provider).then(function(result) {
var token = result.credential.accessToken;
var user = result.user;
}).catch(function(error) {
var errorCode = error.code;
var errorMessage = error.message;
var email = error.email;
var credential = error.credential;
});
}
function print_user(user) {
user.providerData.forEach(function (profile) {
console.log("Sign-in provider: "+profile.providerId);
console.log(" Provider-specific UID: "+profile.uid);
console.log(" Name: "+profile.displayName);
console.log(" Email: "+profile.email);
console.log(" Photo URL: "+profile.photoURL);
});
}
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
print_user(user);
} else {
google_login_in();
}
});
第二个问题是如何使用“带重定向的signInWithRedirect
”
该链接显示“要通过重定向到登录页面进行登录,请调用signInWithRedirect
”
所以,如果用户已经登录,我想重定向到“B.html
”页面
如何使用此功能
firebase.auth().signInWithRedirect("B.html");
虽然您可能不想从
onAuthStateChanged
触发google\u login\u in()
,但您的流量通常是正常的,原因有两个:
null
。它是登录状态的积极指示器,只有用户在场(而不是缺席)才可靠signInWithRedirect
signInWithRedirect()
——这并不意味着你的应用程序将重定向,而是Firebase身份验证过程将通过重定向而不是在弹出窗口中进行。这可以在没有用户交互的情况下触发,但是它会触发整个页面重新加载,将用户从当前页面带走,然后将他们带回来
如果要在对用户进行身份验证时重定向到其他URL,只需等待onAuthStateChanged
指示用户已登录,然后使用例如window.location='/B.html'