1.1.3 • Published 6 years ago

jwt-koa v1.1.3

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

jwt-koa

Simple mini-lib for secured APIs and servers with Koa and JWT.

NPM version Open Source Love

What includes

  • Middleware for checking request headers for token
  • CreateToken function for creating token

Setup

Install it:

npm install jwt-koa --save

or

yarn add jwt-koa

Usage

Import

const jwtKoa = require('jwt-koa');

Set secret key for JWT

process.env.SECRET = 'secret';

Set middleware to secured Router

securedRouter.use(jwtKoa.middleware);
securedRouter.get('/secured', async ctx => {
    ctx.body = { data: 'Secured Data' };
});

Send token to client

notSecuredRouter.post('/send', async ctx => {
    const token = jwtKoa.createToken({ tokenData: 'tokenData' });
    // You can set expire time (3000 by default)
    const tokenWithExpireTime = jwtKoa.createToken(
        { tokenData: 'tokenData' },
        5000
    );
    ctx.response.body = token;
});

Client-side example

fetch('/secured', { method: 'GET', headers: { Authorization: /* There is token from backend*/ } })
    .then(j => j.json())
    .then(data => /* There is secured data*/);

Usage example

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.9

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago