jwt-koa
Simple middleware for secured APIs and servers with Koa.
Setup
Install it:
npm install jwt-koa jsonwebtoken --save
or
yarn add jwt-koa jsonwebtoken
Usage
Import
const jwtKoa = require('jwt-koa');
Set middleware to secured Router
process.env.secret = 'SECRET';
securedRouter.use(jwtKoa);
securedRouter.get('/secured', async ctx => {
ctx.body = { data: 'Secured Data' };
});
Send token to client
notSecuredRouter.post('/send', async ctx => {
const token = jwt.sign({ data: 'DATA' }, process.env.secret, {
expiresIn: 3000
});
ctx.response.body = token;
});
Client-side example
fetch('/secured', { method: 'GET', headers: { Authorization: } })
.then(j => j.json())
.then(data => );