@tool-developer/egg-jwt

JWT for egg plugin that supports server - and client-side custom authorization failure (return JSON data, or jump to the specified page).
中文
Install
$ npm i @tool-developer/egg-jwt --save
Usage
exports.jwt = {
enable: true,
package: '@tool-developer/egg-jwt',
};
generate token, by payload include user id info, like this:
const payload = {
uid
}
const token = this.app.jwt.sign(payload);
validate token, get user id info
const valid = this.app.jwt.verify(token);
const {uid} = valid;
Configuration
exports.jwt = {
secret:'',
enableMiddleware:false,
cookieTokenSet:'auth-token',
headerAuthorization:'authorization',
headerAuthorizationScheme:'Bearer',
headerPassthroughSet:'x-custom-passthrough',
signPageRoutePath:'',
authOptions:{
},
contextStateSecret:'secret',
contextStateUser:'user',
signOptions:{
expiresIn:'2d'
},
verifyOptions:{},
ignore:[]
};
see config/config.default.js for more detail.
authOptions.passthrough
server authorization options
true:
return json data, like{code:401,info}
false:
when set signPageRoutePath, to redirect signPageRoutePath.
no set signPageRoutePath, to throw.
/a/b/c:
to redirect the url.
client authorization options, by headerPassthroughSet, same as the authOptions.
signOptions
signOptions, to see jsonwebtoken jwt.sign
signOptions.expiresIn
token expired time, default 2h, to see zeit/ms
verifyOptions
verifyOptions, to see jsonwebtoken jwt.verify
ignore/match
to see egg math and ignore
Example
Questions & Suggestions
Please open an issue here.
License
MIT