generated-bill / backend /src /middleware /auth.middleware.ts
narasimha01tm's picture
Deploy SmartSpend backend
4bf0aa2
Raw History Blame Contribute Delete
512 Bytes
import { Response, NextFunction } from 'express';
import { verifyToken } from '../config/jwt';
export const protect = (req: any, res: any, next: NextFunction) => {
const token = req.headers.authorization?.split(' ')[1];
if (!token) {
return res.status(401).json({ message: 'Auth token missing' });
}
try {
const decoded = verifyToken(token);
req.user = decoded;
next();
} catch (error) {
res.status(401).json({ message: 'Invalid token' });
}
};