dxr
v1.0.0
Published
- need require ```js const url = require('url'); const {verify} = require('jsonwebtoken'); ```
Downloads
5
Readme
token
- need require
const url = require('url');
const {verify} = require('jsonwebtoken');
- module
module.exports = () => {
return async (ctx,next) => {
let whiteList = ['/login','/registry'];
let pathname = url.parse(ctx.request.url).pathname ;
if(whiteList.includes(pathname)){ //是否在白名单
return await next()
}
if(!ctx.request.header.authorization){ //没传token
return (ctx.body={
code:0,
msg:'去登录'
})
}
let nothing = '' ; //错的token
try {
nothing = verify(ctx.request.header.authorization,'token')
} catch (error) {
nothing = false ;
}
if(!nothing){
return (ctx.body={
code:0,
msg:'token被篡改了'
})
}
let time = (new Date().getTime() - nothing) /1000/60/60 //超时token过期
if(time>2){
return (ctx.body={
code:0,
msg:'token过期,请重新登录'
})
}
return await next();
}
}