代码之家  ›  专栏  ›  技术社区  ›  ajayv

当用户在多个google帐户中登录时显示多个登录弹出窗口,如果用户只在一个帐户中登录,则绕过登录弹出窗口

  •  1
  • ajayv  · 技术社区  · 7 年前

    我正致力于在web应用程序上添加google登录流。想知道我们是否能实现这个功能,

    1. 如果用户登录了多个google帐户,则显示多登录弹出窗口,用户可以在其中选择帐户。

    我想 auth2.signIn ,它检查有多少帐户存在,并在此基础上实现上述用例。

    作为进一步的上下文,登录页代码的javascript当前为:

    <script src="https://apis.google.com/js/platform.js?onload=init_google_signin" async defer></script>
    <script>
    function init_google_signin() {
            var auhtInstance,
                clientID = 'client_id';
    
            auhtInstance = gapi.load('auth2', function () {
              /**
               * Retrieve the singleton for the GoogleAuth library and set up the
               * client.
               */
              auth2 = gapi.auth2.init({
                client_id: clientID,
                scope: 'email profile'
              });
            });
          }
    </script>
    

    function handleGoogleSignIn() {
        var GoogleAuth,
            currentUser,
            googleAuthPromise;
    
        GoogleAuth = gapi.auth2.getAuthInstance();
        currentUser = GoogleAuth.currentUser.get();
    
        if (currentUser && currentUser.isSignedIn()) {
           // submit form with access_token and user info, service will redirect user to logged-in page
           setGoogleFormValues(currentUser, currentUser.getBasicProfile());
        } else {
           googleAuthPromise = GoogleAuth.signIn();
           googleAuthPromise.then(function (user) {
              // submit form with access_token and user info, service will redirect user to logged-in page
              setGoogleFormValues(currentUser, currentUser.getBasicProfile());
           });
        }
    } 
    

    1. 当用户从app注销时,从google注销 GoogleAuth.signOut();

    2. GoogleAuth.disconnect();

    3. 使用 prompt: 'select_account' 签名时,手摇签名看起来像,

       function handleGoogleSignIn() {
          var GoogleAuth,
              currentUser,
              googleAuthPromise;
      
          GoogleAuth = gapi.auth2.getAuthInstance();
          currentUser = GoogleAuth.currentUser.get();
      
          googleAuthPromise = GoogleAuth.signIn(prompt: 'select_account');
          googleAuthPromise.then(function (user) {
             // submit form with access_token and user info, service will redirect user to logged-in page
             setGoogleFormValues(currentUser, currentUser.getBasicProfile());
             });
          }
      } 
      

    这三个修复总是打开弹出窗口,因此它修复了第一个用例,但在第二个用例中失败了。

    0 回复  |  直到 7 年前
    推荐文章