简体   繁体   English

Google登录按钮电子邮件字段

[英]Google Sign-In Button Email Field

I have set up my simple Google Sign-in button from the tutorial https://developers.google.com/+/web/signin/ and I was wondering if how I can do the following: 我已经从教程https://developers.google.com/+/web/signin/设置了我的简单Google登录按钮,我想知道如何执行以下操作:

  1. Restrict all domains in the email field except for something like @domain.com So johndoe@gmail.com will not work, but johndoe@twitter.com would be accepted. 限制电子邮件字段中的所有域名除@ domain.com之外的其他域名因此johndoe@gmail.com将无效,但johndoe@twitter.com将被接受。 Makes sense? 说得通?

  2. Is it possible to "force" the user to sign in again if the the user is already logged into a Google account? 如果用户已登录Google帐户,是否可以“强制”用户再次登录?

Here is my Javascript / jQuery code: 这是我的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>

Ad 1) You would have to check the email after login manually and act accordingly if the email doesn't match whatever domain you are looking for. 广告1)您必须在手动登录后检查电子邮件,并在电子邮件与您要查找的任何域名不匹配时采取相应措施。

To do this you would have to request an additional scope https://www.googleapis.com/auth/userinfo.email which you can define in the data-scope parameter of your Sign-in button markup, and after authentication do an authenticated request for https://www.googleapis.com/oauth2/v2/userinfo which will return the email address in the response. 要执行此操作,您必须请求一个额外的范围https://www.googleapis.com/auth/userinfo.email ,您可以在登录按钮标记的data-scope参数中定义,并在身份验证后进行身份验证请求https://www.googleapis.com/oauth2/v2/userinfo ,它将在回复中返回电子邮件地址。

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

Ad 2) The sign-in button accepts a parameter data-approvalprompt="force" that will show the authentication dialog (and let a user switch to a different account) on each sign-in attempt. 广告2)登录按钮接受参数data-approvalprompt="force" ,该参数将在每次登录尝试时显示身份验证对话框(并让用户切换到其他帐户)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM