Google登录 - 检查用户是否属于特定组织

时间:2017-08-03 23:56:21

标签: javascript oauth google-signin

我正在尝试检查登录用户是否属于特定组织。

通过组织,我的意思是一个GSuite组织。例如,我工作的公司是XYZ,我想看看用户X是否在该组织内。

我已按照Google Sign In上的文档进行操作,这些文档可用于检索基本用户详细信息。

这是我要去的地方:

<head>
  <title>Pricing</title>
  <meta name="google-signin-client_id" content="<Client ID>">
</head>

<body>
  <div class="g-signin2" data-onsuccess="onSignIn"></div>
  <a href="#" onclick="signOut();">Sign out</a>
  <script src="https://apis.google.com/js/platform.js" async defer></script>
  <script>
    function onSignIn(googleUser) {
      var profile = googleUser.getBasicProfile();
      console.log('Name: ' + profile.getName());

      /*
        At this point, I want to check if the user belongs to a specific GSuite organisation
      */
    }

    function signOut() {
      var auth2 = gapi.auth2.getAuthInstance();
      auth2.signOut().then(function() {
        console.log('User signed out.');
      });
    }
  </script>
</body>

1 个答案:

答案 0 :(得分:1)

我建议只查询用户电子邮件,然后检查该电子邮件地址的域名。

function onSignIn(googleUser) {
  console.log('Email: ' + profile.getEmail()); // This is null if the 'email' scope is not present.
}