jwt-autorefresh-fix
Extremely lightweight and tested universal refresh token scheduler. Ensures access tokens are always refreshed in advance of their expiration automatically and integrates with any third party api / persistence architecture.
Install
npm i -S jwt-autorefresh-fix
How to use
/** Function that returns a promise which will resolve to a simple jwt access_token (you handle the persistence mechanism) */const refresh = { const init = method: 'POST' headers: 'Content-Type': `application/x-www-form-urlencoded` body: `refresh_token=&grant_type=refresh_token` return } /** You supply a leadSeconds number or function that generates a number of seconds that the refresh should occur prior to the access token expiring */const leadSeconds = { /** Generate random additional seconds (up to 30 in this case) to append to the lead time to ensure multiple clients dont schedule simultaneous refresh */ const jitter = Math /** Schedule autorefresh to occur 60 to 90 seconds prior to token expiration */ return 60 + jitter} let start = let {}
let 'delayFunc' to undefined
will use inner default function:
const delayFunc = ({exp, iat, nbf, lead}) => {
return exp - iat - lead;
}
or you can use delayFunc
to calculate the final delay time yourself
// delayFunc return milliSeconds
const delayFunc = ({exp, iat, nbf, lead}) => {
return (exp - iat) / 2 - lead;
}
let start = autorefresh({ refresh, leadSeconds, delayFunc })
...