egg-authorization
Egg's authorization(JSON Web Token Authentication Plug)
Install
$ npm i egg-authorization --save
Usage
exports.auth = {
enable: true,
package: "egg-authorization"
};
Configuration
'use strict';
exports.auth = {
enable: true,
type: 'jwt',
jwt: {
common: {
enableParse: true,
enableSignature: false,
enableOnError: true,
},
generate: {
secret: '123456',
exp: 3600,
},
parse: {
tokenPos: 'headers.authorization',
secret: '123456',
},
onerror: {
strategy: 'returnFormat',
returnType: 'json',
json(ctx) {
ctx.body = { msg: 'Unauthorized' };
ctx.status = 401;
},
html(ctx) {
ctx.body = '<h3>Unauthorized</h3>';
ctx.status = 410;
},
},
},
};
see config/config.default.js for more detail.
Example
"use strict";
module.exports = app => {
app.get('/login',app.controller.login.index);
};
'use strict';
module.exports = app => {
class LoginController extends app.Controller {
* index() {
console.log('login userInfo body : ', this.ctx.query.user);
this.ctx.body = this.ctx.query.user;
}
}
return LoginController;
};
you can use this way to get the token context
("use strict");
module.exports = app => {
class SuccessController extends app.Controller {
index() {
const authorization = this.ctx.headers.authorization;
const data = this.ctx.auth.decode(authorization);
console.log(data);
this.ctx.body = data;
}
}
return SuccessController;
};
Questions & Suggestions
Please open an issue here.
License
MIT