jwt在node中实现的库是jsonwebtoken
const jwt = require('jsonwebtoken')
const {promisify} = require('util')
const tojwt = promisify(jwt.sign)
const verify = promisify(jwt.verify)
const {uuid} = require('../config/config.default')
module.exports.createToken = async userinfo=>{
const token = await tojwt({userinfo},uuid,{expiresIn:60 * 60})
return token
}
module.exports.verfiyToken = async(req,res,next)=>{
let token = req.headers.authorization
token = token?token.split("Bearer ")[1]:null
if(!token){
res.status(402).json({error:'缺少token'})
}
try{
let userinfo = await verify(token,uuid)
console.log(userinfo);
next()
}catch (error){
res.status(402).json({error:'token无效'})
}
}