2026-07-25 01:24:08 +08:00

16 lines
426 B
JavaScript

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');
}
};
};