node-tvdb icon indicating copy to clipboard operation
node-tvdb copied to clipboard

Refresh JWT when required to stop it expiring

Open ianbarker opened this issue 6 years ago • 3 comments

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.

ianbarker avatar Dec 21 '18 14:12 ianbarker

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!

edwellbrook avatar Dec 22 '18 15:12 edwellbrook

Ok, good idea. I'll make those changes and update the PR

ianbarker avatar Dec 24 '18 07:12 ianbarker

Ok, I've updated the PR so that the token will refresh using the exp property of the JWT payload.

ianbarker avatar Jan 02 '19 14:01 ianbarker