https://github.com/bdfoster/nomatic-jwt
An opinionated library for encoding, decoding, and verifying JSON Web Tokens (JWTs)
https://github.com/bdfoster/nomatic-jwt
Last synced: about 1 year ago
JSON representation
An opinionated library for encoding, decoding, and verifying JSON Web Tokens (JWTs)
- Host: GitHub
- URL: https://github.com/bdfoster/nomatic-jwt
- Owner: bdfoster
- License: mit
- Created: 2017-09-29T01:38:47.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2023-10-26T00:26:58.000Z (over 2 years ago)
- Last Synced: 2025-03-18T10:11:53.658Z (over 1 year ago)
- Language: TypeScript
- Homepage: https://bdfoster.github.io/nomatic-jwt
- Size: 166 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# nomatic-jwt
[](https://greenkeeper.io/)
[](https://github.com/semantic-release/semantic-release)
[](https://github.com/bdfoster/nomatic-jwt/releases)
[](https://www.npmjs.com/package/nomatic-jwt)
[](https://travis-ci.org/bdfoster/nomatic-jwt)
[](https://coveralls.io/github/bdfoster/nomatic-jwt)
[](https://snyk.io/test/github/bdfoster/nomatic-jwt)
[](https://david-dm.org/bdfoster/nomatic-jwt)
[](https://david-dm.org/bdfoster/nomatic-jwt?type=dev)
[](https://github.com/bdfoster/nomatic-jwt/blob/master/LICENSE)
An opinionated library for encoding, decoding, and verifying JSON Web Tokens (JWTs), heavily inspired by
[node-jwt-simple](https://github.com/hokaccha/node-jwt-simple).
### Installation
You can install from [npm](https://npmjs.com/nomatic-jwt) by doing:
```bash
npm i --save nomatic-jwt
```
### Basic Usage
```javascript
const JWT = require('nomatic-jwt').JWT;
const jwt = new JWT({
algorithm: 'HS256', // "HS256" | "HS384" | "HS512" | "RS256" | "RS384" | "RS512"
expiresIn: 60 * 60, // 1hr
key: 'somethingMoreSecureThanThis', // Valid only with HMAC-SHA algorithms
timeOffset: 60, // allowable wiggle room for expiration (`exp`) and not valid before (`nbf`) claims
validate: true // If false, won't validate when decoding
});
// Encode
const token = jwt.encode({
sub: 'user/12345678',
roles: ['manager']
});
/* eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9
* .eyJzdWIiOiJ1c2VyLzEyMzQ1Njc4Iiwicm9sZXMiOlsibWFuYWdlciJdLCJleHAiOjE1MDY4Nzk1ODAsIm5iZiI6MTUwNjg3NTk4MCwiaWF0IjoxNTA
* 2ODc1OTgwfQ
* .FjHYltcA1Natf6Iu72HyGxkk4GX2phMRG3yNW65_IsQ'
*/
// Decode
const decoded = jwt.decode(token);
```