7

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

Community
  • 1
  • 1
Andy Hansen
  • 411
  • 2
  • 5
  • 6

4 Answers4

6

Try signing out after you get the user information, I tried sign out, but disconnect did it

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

    }
}
Yaman
  • 1,030
  • 17
  • 33
5

try this:

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
}
Alireza Rinan
  • 480
  • 6
  • 9
0

See my answer here

I ended up using the custom integration which does not attempt to auto sign in (also allowed me to change the appearance in the same time)

Community
  • 1
  • 1
Florin D
  • 1,610
  • 18
  • 17
0

Here is my way to do: - when page load, if user loged by google, we _counter + 1 thus if _counter != 1 we can do anything because _counter==1 is pageload if loged

<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>
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135