Files
chuanqi-qycq-web/module/server/koa/auth.js
艾贤凌 6d4a72161f inint
2026-03-16 12:05:55 +08:00

39 lines
1.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import jwt from 'jsonwebtoken'
import * as log4js from '../log4js.js'
const whiteList = [
'/',
'/api/login',
'/api/register',
'/api/send_code',
'/api/reset_password',
'/api/check', // 旧版 token 验证,无需 JWT
'/api/server/list',
'/api/misc/agree',
'/api/config',
'/api/linuxdo/authorize',
'/api/linuxdo/callback',
'/api/linuxdo/bind',
'/api/bind_account', // 游戏服务端内部:绑定第三方账号
'/api/link', // 游戏服务端内部:按 connect_id 反查账号
]
async function auth(ctx, next) {
try {
log4js.koa.debug(`鉴权: ${ctx.method} ${ctx.path}`)
if (whiteList.includes(ctx.path)) {
await next()
return
}
const token = ctx.request.headers.authorization?.split(' ')[1]
if (!token) throw new Error('无token')
ctx.user = jwt.verify(token, process.env.SECRET_KEY || 'chuanqi_secret')
await next()
} catch (err) {
ctx.status = 401
ctx.body = { code: 401, message: 'token无效或过期请重新登录' }
}
}
export default auth