think-session-jwt-multi
v1.1.2
Published
JsonWebToken for ThinkJS 3.x.增加token获取方式,支持多个。
Downloads
12
Maintainers
Readme
think-session-jwt-multi
仅对 think-session-jwt 做了几行改动。
let token;
const { tokenType = 'cookie', tokenName = 'jwt' } = this.options;
//支持多个类型
if (tokenType instanceof Array) {
for (let ttype of tokenType){
switch (ttype) {
case 'header':
token = this.ctx.headers[tokenName];
break;
case 'body':
token = this.ctx.post(tokenName);
break;
case 'query':
token = this.ctx.query[tokenName];
break;
default:
token = this.ctx.cookie(tokenName, undefined, this.options);
break;
}
if (token) {
break;
}
}
} else {
switch (tokenType) {
case 'header':
token = this.ctx.headers[tokenName];
break;
case 'body':
token = this.ctx.post(tokenName);
break;
case 'query':
token = this.ctx.query[tokenName];
break;
default:
token = this.ctx.cookie(tokenName, undefined, this.options);
break;
}
}
JsonWebToken to store session for ThinkJS 3.x base on node-jsonwebtoken
Install
npm install think-session-jwt-multi --save
Quick Start
const JWTSession = require('think-session-jwt-multi');
exports.session = {
type: 'jwt',
common: {
cookie: {
name: 'thinkjs',
}
},
jwt: {
handle: JWTSession,
secret: 'secret', // secret is reqired
//此处变更,项目中可能用到多个场景。支持header+cookie
tokenType: ['header','cookie'], // ['query', 'body', 'header', 'cookie'], 'cookie' is default
tokenName: 'jwt', // if tokenType not 'cookie', this will be token name, 'jwt' is default
sign: {
// sign options is not required
},
verify: {
// verify options is not required
},
verifyCallback: any => any, // default verify fail callback
}
}
- session数据从token中获取,通过配置tokenType指定token来源;
- 设置session数据后会返回token字符串;
- 配置
verifyCallback
函数,验证失败时返回该函数运行的结果;
Sign and verify options
使用node-jsonwebtoken的配置。