tududi/backend/middleware/auth.js
Antonis Anastasiadis e594d1075b
Linting cleanup (#99)
* Add eslint and prettier dependencies and configs

* Lint project.
2025-07-01 11:40:09 +03:00

31 lines
954 B
JavaScript

const { User } = require('../models');
const requireAuth = async (req, res, next) => {
try {
// Skip authentication for health check, login routes, and current_user
const skipPaths = ['/api/health', '/api/login', '/api/current_user'];
if (skipPaths.includes(req.path) || req.originalUrl === '/api/health') {
return next();
}
if (!req.session || !req.session.userId) {
return res.status(401).json({ error: 'Authentication required' });
}
const user = await User.findByPk(req.session.userId);
if (!user) {
req.session.destroy();
return res.status(401).json({ error: 'User not found' });
}
req.currentUser = user;
next();
} catch (error) {
console.error('Authentication error:', error);
res.status(500).json({ error: 'Authentication error' });
}
};
module.exports = {
requireAuth,
};