Google OAuth 2.0. The javascript API does not remain “logged in” after page refresh

I am developing an AngularJS application with Google+ user authentication. I took the angular -google-plus package (the Angular module that handles login with the Google+ API).

In this case, the login will use the gapi object:

gapi.auth.authorize(...)

Everything works until the page is refreshed. For example, I can get the current user from the API using:, gapi.client.oauth2.userinfo.get().execute(function() { ... })but again, the state will not be saved when I refresh the page.

What do I need to do to keep the “Recorded” state after refreshing the page? It looks like the google api is "forgetting" the state.

+4
source share
1 answer

I am not familiar with your code ... Is it really OAuth 2.0? I think you are talking about the Google Sign-In JavaScript client because you are mentioning a page refresh.

I believe that you cannot immediately get any information, such as gapi.client.oauth2.userinfobecause there is no code to wait for the instance to be initialized.

then do something like the following:

gapi.auth2.init({client_id: "...", ...}).then(function(googleAuth) { // onInit
  console.log(googleAuth.isSignedIn.get());
}, function() { // onError
});
0
source

Source: https://habr.com/ru/post/1615102/


All Articles