Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/noderaider/jwt-autorefresh
Extremely lightweight and tested universal refresh token scheduler.
https://github.com/noderaider/jwt-autorefresh
jwt jwt-authentication jwt-autorefresh oauth2 openid refresh-tokens scheduler synchronization
Last synced: about 1 month ago
JSON representation
Extremely lightweight and tested universal refresh token scheduler.
- Host: GitHub
- URL: https://github.com/noderaider/jwt-autorefresh
- Owner: noderaider
- License: mit
- Created: 2016-05-27T07:24:19.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2016-11-03T03:20:47.000Z (about 8 years ago)
- Last Synced: 2024-11-06T18:55:51.261Z (3 months ago)
- Topics: jwt, jwt-authentication, jwt-autorefresh, oauth2, openid, refresh-tokens, scheduler, synchronization
- Language: JavaScript
- Homepage: http://jwt-autorefresh.js.org
- Size: 204 KB
- Stars: 49
- Watchers: 6
- Forks: 7
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# jwt-autorefresh
**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.**
[![NPM](https://nodei.co/npm/jwt-autorefresh.png?stars=true&downloads=true)](https://nodei.co/npm/jwt-autorefresh/)
## Install
`npm i -S jwt-autorefresh`
## How to use
```js
import autorefresh from 'jwt-autorefresh'
import { onAuthorize, onDeauthorize } from './events'/** 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=${localStorage.refresh_token}&grant_type=refresh_token`
}
return fetch('/oauth/token', init)
.then(res => res.json())
.then(({ token_type, access_token, expires_in, refresh_token }) => {
localStorage.access_token = access_token
localStorage.refresh_token = refresh_token
return access_token
})
}/** 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.floor(Math.random() * 30)/** Schedule autorefresh to occur 60 to 90 seconds prior to token expiration */
return 60 + jitter
}let start = autorefresh({ refresh, leadSeconds })
let cancel = () => {}
onAuthorize(access_token => {
cancel()
cancel = start(access_token)
})onDeauthorize(() => cancel())
```