egg-jwt
Egg's JWT(JSON Web Token Authentication Plugin)
Important
egg-jwt@3 use koa-jwt2
Install
$ npm i egg-jwt --save
or
yarn add egg-jwt
Usage
// {app_root}/config/plugin.jsexportsjwt = enable: true package: "egg-jwt";
Configuration
// {app_root}/config/config.default.jsexportsjwt = secret: "123456";
see config/config.default.js for more detail.
Example
// app/router.js"use strict"; module { app; // use old api app.jwt app; app; // is setting in config.jwt.match}; // app/controller/render.js"use strict"; module { Controller * { thisctxbody = "hello World"; } return RenderController;}; // app/controller/login.js"use strict"; module { Controller * { thisctxbody = "hello admin"; } return LoginController;}; // app/controller/success.js"use strict"; module { Controller * { thisctxbody = thisctxstateuser; } return SuccessController;};
Then
curl 127.0.0.1:7001
// response 401
curl 127.0.0.1:7001/login
// response hello admin
curl -H "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJmb28iOiJiYXIiLCJpYXQiOjE0OTAwMTU0MTN9.ehQ38YsRlM8hDpUMKYq1rHt-YjBPSU11dFm0NOroPEg" 127.0.0.1:7001/success
// response {foo: bar}
How To Create A Token
const token = app.jwt.sign({ foo: 'bar' }, app.config.jwt.secret);
For more options, check here
Questions & Suggestions
Please open an issue here.