Restructure backend into modular API layers with admin/organizador/invitados routes, add role-based middleware, flatten module models, and update build scripts
This commit is contained in:
23
packages/server/src/core/middleware/auth.ts
Normal file
23
packages/server/src/core/middleware/auth.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import type { NextFunction, Request, Response } from "express";
|
||||
import jwt from "jsonwebtoken";
|
||||
|
||||
export interface AuthenticatedRequest extends Request {
|
||||
user?: { id: string; email: string; role?: string };
|
||||
}
|
||||
|
||||
export function requireAuth(req: AuthenticatedRequest, res: Response, next: NextFunction) {
|
||||
const auth = req.headers.authorization?.split(" ");
|
||||
const token = auth?.[1];
|
||||
|
||||
if (!token) {
|
||||
return res.status(401).json({ error: "Missing Authorization header" });
|
||||
}
|
||||
|
||||
try {
|
||||
const payload = jwt.verify(token, process.env.JWT_SECRET ?? "change-me") as { sub: string; email: string };
|
||||
req.user = { id: payload.sub, email: payload.email };
|
||||
next();
|
||||
} catch (err) {
|
||||
return res.status(401).json({ error: "Invalid token" });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user