Files
gymflow/server/src/middleware/auth.ts
2025-11-29 12:13:12 +02:00

22 lines
600 B
TypeScript

import { Request, Response, NextFunction } from 'express';
import jwt from 'jsonwebtoken';
const JWT_SECRET = process.env.JWT_SECRET || 'secret';
export const authenticateToken = (req: Request, res: Response, next: NextFunction) => {
const authHeader = req.headers['authorization'];
const token = authHeader && authHeader.split(' ')[1];
if (!token) {
return res.sendStatus(401);
}
jwt.verify(token, JWT_SECRET, (err: any, user: any) => {
if (err) {
return res.sendStatus(403);
}
(req as any).user = user;
next();
});
};