16

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();
Nob Venoda
  • 543
  • 1
  • 6
  • 14
  • 1
    I have the same circunstances you do have, (A site with codeigniter,Angular and Google login) but i have no problem on removing the access. when you say your usser session is still there, do you mean the custom session of your site or the auth instance of google? this is my code and is working fine `function googleSignOut() { var auth2 = gapi.auth2.getAuthInstance(); auth2.signOut().then(function () { myPostToDeleteMySiteSession() ;}); auth2.disconnect(); }` – UrielUVD Mar 09 '16 at 06:42

3 Answers3

11

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>
Udara Seneviratne
  • 2,303
  • 1
  • 33
  • 49
3

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();
    });
 }
Paul V
  • 351
  • 3
  • 9
0

for me the problem was only in chrome because of the one profile mode. profile mode means that you only connected to one account, in order to see the list of accounts you need to be connected to multiple accounts, but with the profile mode ON, you only connected to one account, so there is no list to show - there is only that one account.

do a test on firefox whene you connected to at list 2 google accounts, and you will see the list or exist of profile mode in chrome(but i have no idea how to do that, i stack in profile mode in chrome for the moment)

Yehuda Zvi
  • 239
  • 3
  • 9