这与关于google plus:防止使用Google +自动登录的问题有关。
不同之处在于,我使用的是google登录平台,而不是google,后者有不同的api。
背景:
我有一个价格页面,有一个免费试用注册表格。表单中有google登录按钮。我希望一个签名的用户仍然能够看到定价页面,而没有谷歌的标志-在导致重定向。
我的代码
我的页面顶部有一个元标记来标识我的应用程序。<meta name="google-signin-client_id" content="MY_CLIENT_ID">
我在我的页面上包含了这个脚本:<script src="https://apis.google.com/js/platform.js"></script>
我有一个呈现按钮的div:<div class="g-signin2" data-onsuccess="onSignIn"></div>
我的onSignIn函数如下所示:
function onSignIn(googleUser) {
var id_token = googleUser.getAuthResponse().id_token;
$('#google_token').val(id_token); //hidden form value
$('#google-oauth').submit(); //hidden form
}隐藏表单被提交到后端,其中令牌用于检索用户的电子邮件地址和名称,然后创建一个用户帐户并将其登录。
我的问题是,如果用户已经登录,谷歌将自动调用onSignIn函数,从而在页面加载时提交表单。有什么方法可以防止自动调用onSignIn函数吗?
参考资料:https://developers.google.com/identity/sign-in/web/sign-in
发布于 2017-11-21 13:29:46
试着在你得到用户信息后退出,我试着退出,但是断开了。
function onSignIn(googleUser) {
var profile = googleUser.getBasicProfile();
var idToken = googleUser.getAuthResponse().id_token;
gapi.auth2.getAuthInstance().disconnect().then(function () {
//Do stuff here after the user has been signed out, you can still authenticate the token with Google on the server side
}
}发布于 2017-11-15 09:28:09
试试这个:
function onSignIn(googleUser) {
var id_token = googleUser.getAuthResponse().id_token;
var auth2 = gapi.auth2.getAuthInstance();
auth2.disconnect();
//if this did not had time to sign out put below lines in setTimeout to make a delay
$('#google_token').val(id_token); //hidden form value
$('#google-oauth').submit(); //hidden form
}https://stackoverflow.com/questions/40983266
复制相似问题