How to write out a user after refreshing a page?

I follow the google guide to get the user out.

Given that gapi.auth2 will be undefined after page refresh, I do:

 if (gapi.auth2) { var auth2 = gapi.auth2.getAuthInstance(); auth2.signOut(); } else { gapi.load('auth2', function () { gapi.auth2.init({ client_id: 'myAppID', cookiepolicy: 'single_host_origin' }).signOut(); }); } 

But I get uncaught exception: This method can only be invoked after the token manager is started in the else block.

I also tried to save the auth instance in local storage, but this led to some cyclic errors in the value of the object when it was substituted.

One possible solution is to make

 document.location.href = "https://www.google.com/accounts/Logout?continue=https://appengine.google.com/_ah/logout?continue=myUrl"; 

but instead of registering the user only from my application, this will affect all Google services in which he is logged, in addition to unwanted redirection.

Is there a different approach?

+6
source share
2 answers

Instead of retrieving the singleton for the GoogleAuth library and setting up the client in my login page controller, I had to initialize it in the index.html file:

 <script src="https://apis.google.com/js/api:client.js?onload=start" async defer></script> <script> function start() { gapi.load('auth2', function() { auth2 = gapi.auth2.init({ client_id: 'myAppID', cookiepolicy: 'single_host_origin' }); }); } </script> 

This solved the problem of logging out. However, if the login page has been updated, its controller logic will be executed before gapi.auth2 is determined, and it would be impractical to successfully attach the click handler to the login button.

To avoid this - although this was not an elegant solution, I used $ interval until gapi.auth2 initialized:

 waitForAuth2Initialization = $interval(function () { console.log("Checking..."); if (!gapi.auth2) return; console.log("Ok, gapi.auth2 is not undefined anymore"); var auth2 = gapi.auth2.getAuthInstance(); // Attach signin auth2.attachClickHandler... $interval.cancel(waitForAuth2Initialization); }, 50); 

EDIT: another possible solution is to use the promise callback for the controller logic to wait until the promise is resolved, until the Google API is fully loaded and gapi.auth2 ready to use. This can be done if:

 <script src="https://apis.google.com/js/api:client.js?onload=start" async defer></script> <script> gapiPromise = (function () { var deferred = $.Deferred(); window.start = function () { deferred.resolve(gapi); }; return deferred.promise(); }()); auth2Promise = gapiPromise.then(function () { var deferred = $.Deferred(); gapi.load('auth2', function () { auth2 = gapi.auth2.init({ client_id: 'myAppID', cookiepolicy: 'single_host_origin' }).then(function () { deferred.resolve(gapi.auth2); }); }); return deferred.promise(); }); </script> 

And then in the controller:

 auth2Promise.then(function () { console.log("Ok, gapi.auth2 is not undefined anymore"); var auth2 = gapi.auth2.getAuthInstance(); // Attach signin auth2.attachClickHandler... }); 

But the disadvantage of this approach is that it is slower (it takes twice as long to attach a click handler) than the first, using $interval .

+5
source

There is an easier way, you just need to call. then after calling gapi.auth2.init

 gapi.load('auth2', function () { var auth2 = gapi.auth2.init({ client_id: 'myAppID', cookiepolicy: 'single_host_origin' }); auth2.then(function(){ // this get called right after token manager is started auth2.signOut(); }); }); 
+7
source

All Articles