58 lines
1.4 KiB
TypeScript
58 lines
1.4 KiB
TypeScript
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();
|
|
}
|