init commit

This commit is contained in:
huanglinhuan
2025-12-03 22:20:43 +08:00
commit aaee847593
9 changed files with 495 additions and 0 deletions

27
middleware/auth.js Executable file
View File

@@ -0,0 +1,27 @@
const jwt = require('jsonwebtoken');
const JWT_SECRET = process.env.JWT_SECRET || 'your-secret-key-change-in-production';
// 验证JWT token的中间件
const authenticateToken = (req, res, next) => {
const authHeader = req.headers['authorization'];
const token = authHeader && authHeader.split(' ')[1];
if (!token) {
return res.status(401).json({ error: '未提供访问令牌' });
}
jwt.verify(token, JWT_SECRET, (err, user) => {
if (err) {
return res.status(403).json({ error: '无效的访问令牌' });
}
req.user = user;
next();
});
};
module.exports = {
authenticateToken,
JWT_SECRET
};