This commit is contained in:
Baobhan Sith
2025-04-14 22:51:05 +08:00
parent 286492fc63
commit a974b8b1d9
49 changed files with 13954 additions and 0 deletions
@@ -0,0 +1,17 @@
import { Request, Response, NextFunction } from 'express';
/**
* 认证中间件:检查用户是否已登录 (通过 session 中的 userId 判断)
*/
export const isAuthenticated = (req: Request, res: Response, next: NextFunction): void => {
if (req.session && req.session.userId) {
// 用户已登录,继续处理请求
next();
} else {
// 用户未登录,返回 401 未授权错误
res.status(401).json({ message: '未授权:请先登录。' });
}
};
// 未来可以添加基于角色的授权中间件等
// export const isAdmin = ...