Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't logout with google-signin

I am trying to learn how to use Google Signin for my website. I am following the guide I found on this page: https://developers.google.com/identity/sign-in/web/

So I created a new document and I copy/past the code they propose on the above page. I added my client ID in the tag, and tested the code. Everything worked well, I could login. The login button changed and said Signed in instead of Sign in. In addition, in I look into my console I can see the data from the console.log() like id, email, name,...

I continued reading the guide and I found how to add a Sign out button on this page: https://developers.google.com/identity/sign-in/web/sign-in I implemented this code, and here is the full code I have (without the client ID)

<html lang="en">
  <head>
    <meta name="google-signin-scope" content="profile email">
    <meta name="google-signin-client_id" content="YOUR_CLIENT_ID.apps.googleusercontent.com">
    <script src="https://apis.google.com/js/platform.js" async defer></script>
  </head>
  <body>
    <div class="g-signin2" data-onsuccess="onSignIn" data-theme="dark"></div>
    <a href="#" onclick="signOut();">Sign out</a>
    <script>
      function onSignIn(googleUser) {
        // Useful data for your client-side scripts:
        var profile = googleUser.getBasicProfile();
        console.log("ID: " + profile.getId()); // Don't send this directly to your server!
        console.log('Full Name: ' + profile.getName());
        console.log('Given Name: ' + profile.getGivenName());
        console.log('Family Name: ' + profile.getFamilyName());
        console.log("Image URL: " + profile.getImageUrl());
        console.log("Email: " + profile.getEmail());

        // The ID token you need to pass to your backend:
        var id_token = googleUser.getAuthResponse().id_token;
        console.log("ID Token: " + id_token);
      };
    </script>
<script>
  function signOut() {
    var auth2 = gapi.auth2.getAuthInstance();
    auth2.signOut().then(function () {
      console.log('User signed out.');
    });
  }
</script>
  </body>
</html>

When I click on it, I get the 'User signed out.' in my console. But if I refresh the page, the google button still says that I am Signed in, and the profile information are coming back in my console - even if I manually clear the console. The Sign out button clearly doesn't work, and I have no idea why.

Anybody has some experience with that?

Thanks for the help!


1 Answers

In order to call signOut you need to call call authorize() to get an id_token that is used to mark a user as signed out.

See this issue on the google-api-javascript-client GitHub.

A code sample is posted there explaining that you need to call gapi.auth.authorize first with a callback function that calls gapi.auth.signOut().

In essence, something like this:

 gapi.auth.authorize(
    { 
        'client_id': CLIENT_ID, 
        'scope': SCOPES, 
        'immediate': false,
        cookie_policy: 'single_host_origin',
        response_type: 'token id_token'
    },
    function (authResult) {   gapi.auth.signOut();}
);
like image 180
Alexander Higgins Avatar answered Nov 02 '25 04:11

Alexander Higgins



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!