1 year ago

#375862

test-img

ajayv

How to handle access_token expiry and google signin status in Google Token Model?

For google sign in, as google deprecated the platform.js library so we are going to use the google identity service library, and get the access_token using the Token Model.

const client = google.accounts.oauth2.initTokenClient({
  client_id: 'YOUR_GOOGLE_CLIENT_ID',
  scope: 'https://www.googleapis.com/auth/calendar.readonly',
  callback: (response) => {
    ...
  },
});

To trigger the UX flow, the client.requestAccessToken(); is called on each click of the Google signin button.

One thing I didn't understand is how to handle the access_token expiry -

As per the doc -

By design, access tokens have a short lifetime. If the access token expires prior to the end of the user's session, obtain a new token by calling requestAccessToken() from a user-driven event such as a button press.

Q.1 Does the above mean we need to call this function explicitly from the callback function? Or how would we know the access_token is expired?

Also with platform.js, we had an option to check if the user is already logged in google -

GoogleAuth = gapi.auth2.getAuthInstance();
currentUser = GoogleAuth.currentUser.get();

if (currentUser && currentUser.isSignedIn()) {
   // user is already loggedin in google
} else {
   googleAuthPromise = GoogleAuth.signIn();
   googleAuthPromise.then(function (user) {
     // now user is loggedin in google
   });
}

Q.2 But with the new library, these methods are not available, how can we achieve that if a user is already logged in to google (by not involving any custom session or cookie variables)?

google-signin

google-identity

googlesigninaccount

0 Answers

Your Answer

Accepted video resources