UNPKG

2.99 kBMarkdownView Raw
1# jwt-common
2
3[![Build Status](https://travis-ci.org/lykmapipo/jwt-common.svg?branch=master)](https://travis-ci.org/lykmapipo/jwt-common)
4[![Dependencies Status](https://david-dm.org/lykmapipo/jwt-common.svg?style=flat-square)](https://david-dm.org/lykmapipo/jwt-common)
5[![Coverage Status](https://coveralls.io/repos/github/lykmapipo/jwt-common/badge.svg?branch=master)](https://coveralls.io/github/lykmapipo/jwt-common?branch=master)
6
7Helper utilities for day to day [jwt](https://jwt.io/) usage.
8
9
10## Requirements
11
12- [NodeJS v8.11.1+](https://nodejs.org)
13- [npm v5.6.0+](https://www.npmjs.com/)
14
15## Installation
16
17```sh
18npm install --save @lykmapipo/jwt-common
19```
20
21## Usage
22
23```js
24const { encode, decode, refresh, isExpired } = require('@lykmapipo/jwt-common');
25
26// plain
27encode(payload, (error, jwt) => { ... });
28decode(token, (error, jwt) => { ... });
29refresh(token, payload, (error, jwt) => { ... });
30isExpired(token); //=> false
31
32// express
33const { jwtAuth, jwtPermit } = require('@lykmapipo/jwt-common');
34const secret = process.env.JWT_SECRET || 'secret';
35
36const user = (token, next) => fetchUser(token._id, next);
37app.get('/users', jwtAuth({ secret, user }), (req, res, next) => { ... });
38app.get('/users', jwtAuth({ secret, user }), jwtPermit('user:read'), (req, res, next) => { ... });
39
40```
41
42### Environment
43If below options are available in `process.env` will be used as default.
44```js
45process.env.JWT_SECRET
46process.env.JWT_ALGORITHM
47process.env.JWT_AUDIENCE
48process.env.JWT_ISSUER
49process.env.JWT_SUBJECT
50process.env.JWT_EXPIRES_IN
51```
52
53## Test
54
55- Clone this repository
56
57- Install all dependencies
58
59```sh
60npm install
61```
62
63- Then run test
64
65```sh
66npm test
67```
68
69## Contribute
70
71It will be nice, if you open an issue first so that we can know what is going on, then, fork this repo and push in your ideas. Do not forget to add a bit of test(s) of what value you adding.
72
73## Licence
74
75The MIT License (MIT)
76
77Copyright (c) 2018 lykmapipo & Contributors
78
79Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
80
81The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
82
83THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.