如何避免谷歌登录时弹出用户已经登录?

问题描述 投票:1回答:1

我使用下面的代码,使用户通过谷歌登录。

    <link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet" type="text/css">
  <script src="https://apis.google.com/js/api:client.js"></script>
  <script>
      var googleUser = {};
      var startApp = function () {
          gapi.load('auth2', function () {
              // Retrieve the singleton for the GoogleAuth library and set up the client.
              auth2 = gapi.auth2.init({
                  client_id: [my client id],
                  cookiepolicy: 'single_host_origin',
                  // Request scopes in addition to 'profile' and 'email'
                  scope: 'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/plus.me'
              });
          });
      };

      function checkUser() {
          if (auth2.isSignedIn.get()) {
              onSignIn(auth2.currentUser.po.po);
          }
          else {
              gapi.auth.authorize({ client_id: [my client id], scope: 'https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/plus.me', immediate: false }, onSignIn);
          }
      }

      function onSignIn(googleUser) {
          alert('Access Token' + googleUser.access_token);
          //location.href = "ServerSide.aspx?acccessToken=" + googleUser.access_token;
      }
  </script>
  <style type="text/css">
    #customBtn {
      display: inline-block;
      background: #4285f4;
      color: white;
      width: 190px;
      border-radius: 5px;
      white-space: nowrap;
    }
    #customBtn:hover {
      cursor: pointer;
    }
    span.label {
      font-weight: bold;
    }
    span.icon {
      /*background: url('/identity/sign-in/g-normal.png') transparent 5px 50% no-repeat;*/
      display: inline-block;
      vertical-align: middle;
      width: 42px;
      height: 42px;
      border-right: #2265d4 1px solid;
    }
    span.buttonText {
      display: inline-block;
      vertical-align: middle;
      padding-left: 42px;
      padding-right: 42px;
      font-size: 14px;
      font-weight: bold;
      /* Use the Roboto font that is loaded in the <head> */
      font-family: 'Roboto', sans-serif;
    }
  </style>
  <!-- In the callback, you would hide the gSignInWrapper element on a
  successful sign in -->
  <div id="gSignInWrapper" onclick="checkUser()">
    <span class="label">Sign in with:</span>
    <div id="customBtn" class="customGPlusSignIn">
      <span class="icon"></span>`enter code here`
      <span class="buttonText">Google</span>
    </div>
  </div>
  <div id="name"></div>
  <script>startApp();</script>

有了这个代码,如果用户登录,甚至在谷歌的流行来得快,去得也快,看起来weird.So我想如果用户登录到谷歌弹出不该来,直接让用户进入系统。为此,我曾用“auth2.isSignedIn.get()”按钮点击,但这一功能总是给我假,即使用户是在谷歌记录。我被困在这point.Please如果你有任何方式实现这一目标就告诉我。

javascript oauth google-api google-signin google-plus-signin
1个回答
0
投票

isSignedIn方法可以用谷歌身份验证实例GoogleAuth.isSignedIn.get(),你应该使用以下

gapi.auth2.getAuthInstance()。isSignedIn.get()

© www.soinside.com 2019 - 2024. All rights reserved.