Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google OAuth 2.0. javascript API doesn't stay "logged in" after page refresh

I'm developing an AngularJS application with google+ user authentication. I've taken the package angular-google-plus (an Angular module which handles the login with the Google+ API).

At it's heart, the login will use the gapi object:

gapi.auth.authorize(...)

Everything is working while the page doesn't refresh. For example, I can get the current user from the API with: gapi.client.oauth2.userinfo.get().execute(function() { ... }) but again, the state wont be kept when I refresh the page.

What do I need to do in order to maintain the "Logged-In" state after the page is refreshed ? it seems the google api "forgets" the state.

like image 879
orberkov Avatar asked Nov 09 '15 18:11

orberkov


1 Answers

I feel unfamiliar with your code... Is it really OAuth 2.0? I think you are talking about Google Sign-In JavaScript client because you are mentioning page refresh.

I believe that you cannot immediately get any information like gapi.client.oauth2.userinfo because there is no code to wait for an instance to be initialized.

Do something like the following with then:

gapi.auth2.init({client_id: "...", ...}).then(function(googleAuth) { // onInit
  console.log(googleAuth.isSignedIn.get());
}, function() { // onError
});
like image 196
ghchoi Avatar answered Oct 14 '22 08:10

ghchoi