node-tvdb
node-tvdb copied to clipboard
Refresh JWT when required to stop it expiring
I've noticed that a long running app's token will expire. The API docs state that the token lasts for 24h so this PR should allow a refresh every 12 hours thus stopping it expiring.
Hey Ian! Thanks for taking the time to put together this PR. I think this will make a great addition to the library! 😄
Could we implement this a slightly different way?
- Offer a public option to enable/disable refreshing automatically — I think having this enabled by default would be sensible :)
- Instead of generating our own expiration timestamp, can we just read the expiration date from the JWT? (see below)
- Instead of refreshing every 12 hours, only refresh when needed, based off the expiration date on the JWT.
You can get the expiration date from the JWT by:
- Splitting the token into three parts by the
"."character. The 2nd part contains our JWT payload. - Base64 decode the 2nd part and JSON.parse to get the payload object
- The
"exp"key contains the token expiration, which can be stored and checked against.
Cheers, and let me know if there's anything I can help with!
Ok, good idea. I'll make those changes and update the PR
Ok, I've updated the PR so that the token will refresh using the exp property of the JWT payload.