Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/forbeslindesay/jwt-cache
Cache an async function for generating a JSON Web Token
https://github.com/forbeslindesay/jwt-cache
javascript jsonwebtoken jwt nodejs
Last synced: 16 days ago
JSON representation
Cache an async function for generating a JSON Web Token
- Host: GitHub
- URL: https://github.com/forbeslindesay/jwt-cache
- Owner: ForbesLindesay
- License: mit
- Created: 2021-10-18T12:41:44.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2023-03-16T16:47:23.000Z (over 1 year ago)
- Last Synced: 2024-10-20T04:41:38.963Z (24 days ago)
- Topics: javascript, jsonwebtoken, jwt, nodejs
- Language: TypeScript
- Homepage:
- Size: 37.1 KB
- Stars: 7
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
Awesome Lists containing this project
README
# jwt-cache
Cache an async function for generating a JSON Web Token
[![Rolling Versions](https://img.shields.io/badge/Rolling%20Versions-Enabled-brightgreen)](https://rollingversions.com/ForbesLindesay/jwt-cache)
## Installation
```
yarn add jwt-cache
```## Usage
```ts
import jwtCache from 'jwt-cache';const cache = jwtCache({
getToken: async () => {
// this could be slow code for generating a JSON Web Token
// with an expiry represented via the standard "exp" value
return sign({hello: 'world'}, `my_secret`, {expiresIn: 3});
},
// If the token will not be valid for this many ms, get
// a new token rather than returning the cached token
// Defaults to 1 second
minimumValidityMilliseconds: 100,
// Optionally eagerly fetch a token before it is needed.
// This will trigger fetching a new token this many ms
// before it would reach the minimum validity threshold
eagerRefreshMilliseconds: 100,
});// When you need a token:
cache.getToken().then((token) => {
// use the token here
});
```