Added files of the Todo app

This commit is contained in:
Diven2510
2025-12-30 19:37:10 +05:30
parent 387812c33e
commit bab99095d7
31 changed files with 6803 additions and 0 deletions

View File

@@ -0,0 +1,25 @@
import jwt from 'jsonwebtoken';
import User from '../models/User.js';
export const authenticateToken = async (req, res, next) => {
const authHeader = req.headers['authorization'];
const token = authHeader && authHeader.split(' ')[1];
if (!token) {
return res.status(401).json({ message: 'Access token required' });
}
try {
const decoded = jwt.verify(token, process.env.JWT_SECRET);
const user = await User.findById(decoded.userId).select('-password');
if (!user) {
return res.status(401).json({ message: 'Invalid token' });
}
req.user = user;
next();
} catch (error) {
return res.status(403).json({ message: 'Invalid or expired token' });
}
};