这与关于google plus的问题有关:Prevent auto sign in with Google plus
不同之处在于我使用google登录平台而不是google plus,后者有不同的api。
背景
我有一个定价页面,其中包含免费试用注册表单。该表单有谷歌登录按钮。我希望已登录的用户能够在没有谷歌登录的情况下仍然看到定价页面,从而导致重定向。
我的代码
我的页面顶部有元标记,用于标识我的应用程序。 <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
}
隐藏表单将提交给后端,其中令牌用于检索用户的电子邮件地址和名称,然后创建用户帐户并将其登录。
我的问题是,如果用户已经登录,Google会自动调用onSignIn函数,导致在加载页面时提交表单。有没有办法阻止自动调用onSignIn函数?
参考:https://developers.google.com/identity/sign-in/web/sign-in
答案 0 :(得分:1)
试试这个:
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
}
答案 1 :(得分:1)
尝试在获取用户信息后退出,我尝试退出,但断开连接
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
}
}
答案 2 :(得分:0)
我最终使用的custom integration不会尝试自动登录(也允许我在同一时间更改外观)
答案 3 :(得分:0)
这是我的方法:
- 当页面加载时,如果谷歌用户,我们_counter + 1
因此,如果_counter != 1
我们可以做任何事情,因为_counter==1
是页面加载,如果隐藏
<script>
var GoogleOAuth = {
_counter: 0,
_gauth: null,
init:function() {
gapi.auth2.init({
client_id:'xxxxidclientxxx.apps.googleusercontent.com',
scope: 'email profile openid'
}).then(function() {
GoogleOAuth._gauth = gapi.auth2.getAuthInstance();
var isSigned = GoogleOAuth._gauth.isSignedIn.get();
if (isSigned) {
GoogleOAuth._counter++;
}
gapi.signin2.render('btnGooglelogin', {
'scope': 'profile email',
'width': 240,
'height': 50,
'longtitle': true,
'theme': 'dark',
'onsuccess': GoogleOAuth.onSignIn,
'onfailure': GoogleOAuth.onFail
});
});
},
onSignIn: function (googleUser) {
var profile = googleUser.getBasicProfile();
var id = profile.getId();
var name = profile.getName();
var avatarUrl = profile.getImageUrl();
var email = profile.getEmail();
var idToken = googleUser.getAuthResponse().id_token;
if (email == '' || idToken == '') {
alert('Your email will become username, please public your email');
return;
}
if (GoogleOAuth._counter == 1) {
GoogleOAuth._counter++;
return;
}
$.post('/User/Googlelogin', { idToken: idToken, googleId:id,name:name,avatarUrl:avatarUrl,email:email})
.done(function(data) {
if (data.Ok) {
window.location='/';
} else {
alert(data.Message);
}
}).fail(function() {
alert("Error. can not process");
});
},
signOut: function () {
var auth2 = gapi.auth2.getAuthInstance();
auth2.signOut();
},
onFail:function(error){}
}
</script>
<script src="https://apis.google.com/js/platform.js?onload=googleOnload" async defer></script>
<script>
function googleOnload() {
gapi.load('auth2', function () {
GoogleOAuth.init();
});
}
</script>