Files
Transcendence/srcs/backend/middleware/auth.js
T
2026-01-21 16:11:28 +01:00

23 lines
449 B
JavaScript

import jwt from 'jsonwebtoken';
// Check if the webtoken is valid
export default function authMiddleware(req, res, next)
{
const header = req.headers.authorization;
if (!header)
return (res.status(401).json({error: 'Missing token'}));
const token = header.split(' ')[1];
try
{
const payload = jwt.verify(token, process.env.JWT_SECRET);
req.user = payload;
next();
}
catch
{
res.status(401).json({error: 'Invalid token'});
}
};