Google api auth2 signOut not working

Nob Venoda picture Nob Venoda · Dec 29, 2015 · Viewed 9.2k times · Source

First off, I am following this guide https://developers.google.com/identity/sign-in/web/ and this reference https://developers.google.com/identity/sign-in/web/reference.

But instead of having a callback declared in window, I used gapi.signin2.render function to render the button and attach a handler to it in my Angular controller.

Logging in works just fine, problem is, when I try to log out by calling gapi.auth2.getAuthInstance().signOut(), it just doesn't do it.

I noticed that sessionStorage for accounts.google.com is still there, and because of that Google automatically signs me back in when I render the button again on the login screen.

I tried seeing what happens after signOut() is complete:

gapi.auth2.getAuthInstance().signOut().then(function() {
    console.log(gapi.auth2.getAuthInstance().isSignedIn.get());
    // prints 'true'
});

I also tried calling disconnect() instead of signOut(), knowing that it will revoke access, and it does remove token from the sessionStorage, but user session is still there. It is only gone once I reload the page.

Here is my complete code:

$scope.logout = function() {
    //...
    gapi.auth2.getAuthInstance().signOut().then(function() {
      console.log(gapi.auth2.getAuthInstance().isSignedIn);
    });
};

$scope.processAuth = function(authResult) {
  console.log("success");
  if(authResult.getAuthResponse().id_token) {
    // backend calls
  }
};

$scope.renderSignInButton = function() {
  console.log(gapi.auth2);
  gapi.signin2.render('signInButton',
    {
      'onsuccess': $scope.processAuth, // Function handling the callback.
      'onfailure': $scope.signinFailed, // Function handling the callback.
      'clientid': 'asdasdasd.apps.googleusercontent.com',
      'scope': 'https://www.googleapis.com/auth/userinfo.email',
      'cookiepolicy': 'single_host_origin'
    }
  );
};

$scope.renderSignInButton();

Answer

Udara Seneviratne picture Udara Seneviratne · Aug 2, 2018

I tried as following and it worked. It is required to call auth2.disconnect() when the auth2.signOut() success.

<script type="text/javascript">
    function signOut() {
        var auth2 = gapi.auth2.getAuthInstance();
        auth2.signOut().then(function () {
            auth2.disconnect();
        });

    }

    function onLoad() {
        gapi.load('auth2', function() {
            gapi.auth2.init();
        });
    }
</script>



<script src="https://apis.google.com/js/platform.js?onload=onLoad" async defer></script>