This is related to this question about google plus: Prevent auto sign in with Google plus
The difference is that I'm using the google sign in platform instead of google plus, which has a different api.
Background:
I have a pricing page that has a free trial signup form. The form has the google sign in button. I would like a signed in user to be able to still see the pricing page without the google sign-in causing a redirect.
My Code
I have the meta tag at the top of my page that identifies my application. <meta name="google-signin-client_id" content="MY_CLIENT_ID">
I include this script on my page:<script src="https://apis.google.com/js/platform.js"></script>
I have this div that renders the button: <div class="g-signin2" data-onsuccess="onSignIn"></div>
My onSignIn function looks like this:
function onSignIn(googleUser) {
var id_token = googleUser.getAuthResponse().id_token;
$('#google_token').val(id_token); //hidden form value
$('#google-oauth').submit(); //hidden form
}
The hidden form is submitted to the backend, where the token is used to retrieve the user's email address and name, and then creates a user account and logs them in.
My problem is that if the user is already signed in, google will automatically call the onSignIn function causing the form to be submitted when the page is loaded. Is there a way for me to prevent the onSignIn function being automatically called?
Reference: https://developers.google.com/identity/sign-in/web/sign-in