- Local auth: registration/login with email+password (bcrypt, MariaDB) - Landing page: marketing page at / with hero, categories, advantages, CTA - Tool categories: Images (4), Code (3), Web (3), Utilities (2) - Sidebar: category dividers, user avatar with logout - Protected routes: all tools require auth, landing/login/register public - New files: lib/db.js, routes/auth.js, login.html, register.html, landing.html - Dependencies: bcrypt, mysql2
34 lines
1.1 KiB
JavaScript
34 lines
1.1 KiB
JavaScript
/**
|
|
* Auth middleware — requires user session for protected routes.
|
|
* Public: landing page, auth routes, static assets, health, placeholder API.
|
|
*/
|
|
|
|
const PUBLIC_PATHS = ['/', '/health', '/favicon.ico'];
|
|
const PUBLIC_PREFIXES = ['/auth/', '/vendor/', '/placeholder-img/'];
|
|
const PUBLIC_FILES = ['/shared.css', '/shared.js', '/landing.html'];
|
|
|
|
function authMiddleware(req, res, next) {
|
|
// Public paths
|
|
if (PUBLIC_PATHS.includes(req.path)) return next();
|
|
if (PUBLIC_FILES.includes(req.path)) return next();
|
|
if (PUBLIC_PREFIXES.some(p => req.path.startsWith(p))) return next();
|
|
|
|
// Authenticated user
|
|
if (req.session && req.session.user) return next();
|
|
|
|
// API requests — 401 JSON
|
|
if (req.path.startsWith('/api/') || req.path.startsWith('/download/')) {
|
|
return res.status(401).json({ error: 'Unauthorized' });
|
|
}
|
|
|
|
// Page requests — save return URL and redirect to login
|
|
if (req.method === 'GET') {
|
|
req.session.returnTo = req.originalUrl;
|
|
return res.redirect('/auth/login');
|
|
}
|
|
|
|
return res.status(401).json({ error: 'Unauthorized' });
|
|
}
|
|
|
|
module.exports = authMiddleware;
|