Skip to content

Instantly share code, notes, and snippets.

@tientp-floware
Last active October 6, 2020 03:14
Show Gist options
  • Save tientp-floware/bec90db359f2d2fda9c38595ff42862a to your computer and use it in GitHub Desktop.
Save tientp-floware/bec90db359f2d2fda9c38595ff42862a to your computer and use it in GitHub Desktop.
Auto refresh token in session on google api
function refreshSession() {
if (!gapi.auth2.getAuthInstance().currentUser.get().isSignedIn()) {
return;
}
var authResponse = gapi.auth2.getAuthInstance().currentUser.get().getAuthResponse();
if (authResponse.expires_at - Date.now() < 60000) {
gapi.auth2.getAuthInstance().currentUser.get().reloadAuthResponse()
.then(() => {
setTimeout(refreshSession, 5000); // 5s
})
.catch(error => {
setTimeout(refreshSession, 3000); // 3s
});
} else {
setTimeout(refreshSession, 1000); // 1s
}
}
### Credit-don't remember.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment