From 7a8a124dcb6d4f9e35e05111f07b19cd7a4d8ec7 Mon Sep 17 00:00:00 2001 From: Agent Date: Tue, 14 Jul 2026 17:27:43 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20auth.js=20=E6=94=B9=E7=94=A8MySQL=20pool?= =?UTF-8?q?=EF=BC=8C=E4=BF=AE=E5=A4=8Ddocker=E5=81=A5=E5=BA=B7=E6=A3=80?= =?UTF-8?q?=E6=9F=A5=E8=B6=85=E6=97=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修复 JWT 认证中间件 default import 错误 - auth.js 同步改成 pool.query MySQL 语法 --- backend/src/middleware/auth.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/backend/src/middleware/auth.js b/backend/src/middleware/auth.js index a83d6e2..7ae24af 100644 --- a/backend/src/middleware/auth.js +++ b/backend/src/middleware/auth.js @@ -1,10 +1,10 @@ import jwt from 'jsonwebtoken'; -import db from '../database.js'; +import { pool } from '../database.js'; const SECRET = process.env.JWT_SECRET || 'yx-nav-secret-2026'; /** JWT 认证中间件 */ -export function authenticate(req, res, next) { +export async function authenticate(req, res, next) { const authHeader = req.headers.authorization; if (!authHeader || !authHeader.startsWith('Bearer ')) { return res.status(401).json({ status: 401, message: '未登录' }); @@ -12,7 +12,8 @@ export function authenticate(req, res, next) { try { const token = authHeader.slice(7); const payload = jwt.verify(token, SECRET); - const user = db.prepare('SELECT id, username, nickname, role FROM users WHERE id = ?').get(payload.userId); + const [rows] = await pool.query('SELECT id, username, nickname, role FROM users WHERE id = ?', [payload.userId]); + const user = rows[0]; if (!user) return res.status(401).json({ status: 401, message: '用户不存在' }); req.user = user; next();