requireAuth.ts
Ricky/backend/src/middleware/requireAuth.ts
import { Request, Response, NextFunction } from 'express';
import { verifyJwt } from '../lib/jwt.js';
export function requireAuth(req: Request, res: Response, next: NextFunction) {
const header = req.headers.authorization;
if (!header || !header.startsWith('Bearer ')) {
return res.status(401).json({ error: 'UNAUTHORIZED', message: 'Missing bearer token' });
}
const token = header.slice('Bearer '.length);
try {
const payload = verifyJwt<{ userId: string }>(token);
(req as any).userId = payload.userId;
next();
} catch {
return res.status(401).json({ error: 'UNAUTHORIZED', message: 'Invalid token' });
}
}
Artículos relacionados
__init__.py
__init__.py — python source code from the Ricky learning materials (Ricky/Cat Project Final/back_web-4/app/__init__.py).
Leer artículo →auth.py
auth.py — python source code from the Ricky learning materials (Ricky/Cat Project Final/back_web-4/app/admin/auth.py).
Leer artículo →routes.py
routes.py — python source code from the Ricky learning materials (Ricky/Cat Project Final/back_web-4/app/admin/routes.py).
Leer artículo →config.py
config.py — python source code from the Ricky learning materials (Ricky/Cat Project Final/back_web-4/app/config.py).
Leer artículo →admin_ingest.py
admin_ingest.py — python source code from the Ricky learning materials (Ricky/Cat Project Final/back_web-4/app/routes/admin_ingest.py).
Leer artículo →admin_stats.py
admin_stats.py — python source code from the Ricky learning materials (Ricky/Cat Project Final/back_web-4/app/routes/admin_stats.py).
Leer artículo →