Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google api auth2 signOut not working

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();
like image 710
Nob Venoda Avatar asked Dec 29 '15 16:12

Nob Venoda


2 Answers

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>
like image 159
Udara Seneviratne Avatar answered Oct 14 '22 05:10

Udara Seneviratne


It can happen that the auth2 object is not loaded. ( which would cause both issues presented in the question ).

This following snippet resolves that particular issue.

 if(!gapi.auth2){
    gapi.load('auth2', function() {
        gapi.auth2.init();
    });
 }
like image 3
Paul V Avatar answered Oct 14 '22 06:10

Paul V