Google api auth2 signOut not working

First, I follow this guide https://developers.google.com/identity/sign-in/web/ and this link https://developers.google.com/identity/sign-in/web/reference .

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

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

I noticed that sessionStorage for accounts.google.com still exists, and because of this, Google automatically signs me up when I click the button on the login screen again.

I tried to see what happens after signOut() :

 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 would deny access and it would remove the token from sessionStorage, but the user session still exists. This only happens after a page reload.

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(); 
+10
source share
2 answers

I tried as follows and it worked. It is required to call auth2.disconnect() when auth2.signOut() completes auth2.signOut() .

 <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> 
+2
source

It may happen that the auth2 object is not loaded. (which will cause both problems presented in the question).

This next snippet fixes this particular problem.

  if(!gapi.auth2){ gapi.load('auth2', function() { gapi.auth2.init(); }); } 
+1
source

All Articles