module.exports = (options, app) => { return async function jwtAuth(ctx, next) { const token = ctx.request.header.authorization; if (!token) { ctx.throw(401, '未登录,无法访问'); } try { // 验证 token const decoded = app.jwt.verify(token, options.secret); ctx.state.user = decoded; await next(); } catch (err) { ctx.throw(401, '无效的token'); } }; };