feat : Init Project

This commit is contained in:
Thibault Pouch
2026-02-28 14:20:09 +01:00
commit f700d7fd86
20 changed files with 4066 additions and 0 deletions

View File

@@ -0,0 +1,57 @@
import { Request, Response, NextFunction } from 'express';
import jwt from 'jsonwebtoken';
export interface JwtPayload {
userId: string;
role: string;
isAdmin: boolean;
}
declare global {
namespace Express {
interface Request {
user?: JwtPayload;
}
}
}
export function authenticate(req: Request, res: Response, next: NextFunction): void {
const header = req.headers.authorization;
if (!header?.startsWith('Bearer ')) {
res.status(401).json({ error: 'Missing or invalid Authorization header' });
return;
}
const token = header.slice(7);
try {
const payload = jwt.verify(token, process.env.JWT_SECRET!) as JwtPayload;
req.user = payload;
next();
} catch {
res.status(401).json({ error: 'Token expired or invalid' });
}
}
export function requireStaff(req: Request, res: Response, next: NextFunction): void {
if (!req.user) {
res.status(401).json({ error: 'Unauthorized' });
return;
}
if (req.user.role !== 'dev' && req.user.role !== 'com') {
res.status(403).json({ error: 'Staff access required' });
return;
}
next();
}
export function requireAdmin(req: Request, res: Response, next: NextFunction): void {
if (!req.user) {
res.status(401).json({ error: 'Unauthorized' });
return;
}
if (!req.user.isAdmin) {
res.status(403).json({ error: 'Admin access required' });
return;
}
next();
}