当用户已经登录时,如何避免出现谷歌登录弹出窗口

How to avoid the google Sign In Pop up when the user is already logged in?

本文关键字:登录 谷歌 窗口 何避免 用户      更新时间:2023-09-26

我使用以下代码让用户通过谷歌登录。

    <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>

有了这段代码,如果用户登录谷歌,弹出窗口就会来来去去,这看起来很奇怪。所以我想如果用户登录谷歌弹出不应该来,直接让用户进入系统。为此,我在点击按钮时使用了"auth2.isSignedIn.get()",但即使用户登录到谷歌,这个函数也总是给我false。我陷入了困境。如果你有任何方法可以实现这一点,请告诉我。

isSignedIn方法可在google身份验证实例GoogleAuth.isSignedIn.get()上使用,您应该使用以下

gap.auth2.getAuthInstance().isSignedIn.get()