Google登录按钮电子邮件字段

时间:2013-09-25 00:04:10

标签: javascript jquery google-plus

我已经从教程https://developers.google.com/+/web/signin/设置了我的简单Google登录按钮,我想知道如何执行以下操作:

  1. 限制电子邮件字段中的所有域名,但@ domain.com除外 所以johndoe@gmail.com将无效,但johndoe@twitter.com将被接受。有道理吗?

  2. 如果用户已登录Google帐户,是否可以“强制”用户再次登录?

  3. 这是我的Javascript / jQuery代码:

    <script type="text/javascript">
    
          (function() {
           var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
           po.src = 'https://apis.google.com/js/client:plusone.js';
           var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
         })();
    
    
      function signinCallback(authResult) {
      if (authResult['access_token']) {
        // Successfully authorized
        // Hide the sign-in button now that the user is authorized, for example:
        document.getElementById('signinButton').setAttribute('style', 'display: none');
    
        $('.inline-field-title').hide();
    
    
      } else if (authResult['error']) {
        // There was an error.
        // Possible error codes:
        //   "access_denied" - User denied access to your app
        //   "immediate_failed" - Could not automatically log in the user
        // console.log('There was an error: ' + authResult['error']);
      }
    }
    
    
      function disconnectUser(access_token) {
      var revokeUrl = 'https://accounts.google.com/o/oauth2/revoke?token=' +
          access_token;
    
      // Perform an asynchronous GET request.
      $.ajax({
        type: 'GET',
        url: revokeUrl,
        async: false,
        contentType: "application/json",
        dataType: 'jsonp',
        success: function(nullResponse) {
          // Do something now that user is disconnected
          // The response is always undefined.
        },
        error: function(e) {
          // Handle the error
          // console.log(e);
          // You could point users to manually disconnect if unsuccessful
          // https://plus.google.com/apps
        }
      });
    
    }
    // Could trigger the disconnect on a button click
    
    $('#revokeButton').click(disconnectUser);
    
    
    </script>
    

1 个答案:

答案 0 :(得分:4)

广告1)您必须在手动登录后查看电子邮件,并在电子邮件与您要查找的任何域名不匹配时采取相应措施。

要执行此操作,您必须请求一个额外的范围https://www.googleapis.com/auth/userinfo.email,您可以在登录按钮标记的data-scope参数中定义该范围,并在身份验证后对{{1}执行经过身份验证的请求这将返回响应中的电子邮件地址。

有关示例代码,请参阅https://developers.google.com/+/web/people/#retrieve_an_authenticated_users_email_address

广告2)登录按钮接受参数https://www.googleapis.com/oauth2/v2/userinfo,该参数将在每次登录尝试时显示身份验证对话框(并让用户切换到其他帐户)。

请参阅https://developers.google.com/+/web/signin/#sign-in_button_attributes