kot-tem-router
v1.0.0
Published
```'user strict' module.exports = ({ whiteList }) => { return async (ctx, next) => { if (whiteList.includes(ctx.path)) { await next(); return; } try { const token = ctx.get('token')
Downloads
1
Readme
module.exports = ({ whiteList }) => {
return async (ctx, next) => {
if (whiteList.includes(ctx.path)) {
await next();
return;
}
try {
const token = ctx.get('token')
if (token) {
const data = await ctx.helper.token.verify(token)
// console.log('data----', data);
ctx.userInfl = data;
await next();
}
else {
ctx.status = 401;
ctx.body = {
code: 'FATL',
status: '401',
msg: "您没有传递toKen 没办法确认身份"
}
}
}
catch(error) {
//token 解密
console.log('error---', error)
ctx.status = 403;
ctx.body = {
code: 'FATL',
status: '403',
msg: "解析失败 你没有权限"
}
}
}
}
```'user strict';
const jwt=require('jsonwebtoken')
const KEY='token_key';//设置token
module.exports={
token:{
sign(payload){
return jwt.sign(payload,KEY,{
expiresIn:'1 days'
})
},
//解析token
verify(token){
return new Promise((reslove,reject)=>{
jwt.verify(token,KEY,(error,data)=>{
if(error){
reject(error)
return
}
reslove(data)
})
})
}
}
}