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'