const jwt = require('jsonwebtoken'); module.exports = (req, res, next) => { const token = req.headers['authorization']; if (!token) { return res.unauthorized('未提供 token'); } try { const decoded = jwt.verify(token, process.env.JWT_SECRET); req.userId = decoded.userId; next(); } catch (err) { res.unauthorized('无效或已过期的 token'); } };