Last active
October 6, 2020 03:14
-
-
Save tientp-floware/bec90db359f2d2fda9c38595ff42862a to your computer and use it in GitHub Desktop.
Auto refresh token in session on google api
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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