2013-09-25 50 views
1

我从教程https://developers.google.com/+/web/signin/设置我的简单的谷歌登录按钮,我想知道,如果我怎么能做到以下几点:谷歌登录按钮电子邮件字段

  1. 限制所有域的电子邮件字段除了像@ domain.com 所以[email protected]不会工作,但[email protected]将被接受。说得通?

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

这里是我的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> 

回答

4

广告1)你必须手动登录后,检查电子邮件,并采取相应的行动,如果邮件不匹配任何域您正在寻找对于。

要做到这一点,你将不得不要求额外的范围https://www.googleapis.com/auth/userinfo.email,您可以在您的登录按钮标记的data-scope参数定义和验证之后做https://www.googleapis.com/oauth2/v2/userinfo验证的请求将在响应中返回的电子邮件地址。

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

广告2)登录按钮接受参数data-approvalprompt="force",该参数将在每次登录尝试时显示身份验证对话框(并让用户切换到其他帐户)。

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

+0

非常感谢! – Nick