Files
siti-plugin-repo/server/middleware/auth.js
Roberto Guagliardo 7b0ca40c4f feat: implement user authentication and license management system
- Added schema for users, licenses, and license hostnames in the database.
- Created storage utility for reading and writing JSON files.
- Developed user service for user registration, authentication, and retrieval.
- Implemented authentication middleware to protect routes.
- Built LicenseCard component to display license details.
- Created SiteNav component for navigation with user authentication status.
- Established AuthContext for managing authentication state and actions.
- Developed Home page to display available plugins.
- Created LicenseManager page for managing licenses with forms for creation and verification.
- Implemented PluginDetail page to show detailed information about a specific plugin.
- Added utility functions for date formatting.
2026-02-01 02:20:28 +00:00

24 lines
829 B
JavaScript

import jwt from "jsonwebtoken";
import { JWT_SECRET } from "../lib/config.js";
import { getUserById } from "../lib/userService.js";
export async function requireAuth(req, res, next) {
const authHeader = req.headers.authorization || "";
const token = authHeader.startsWith("Bearer ") ? authHeader.slice(7).trim() : null;
if (!token) {
return res.status(401).json({ error: "Inloggen vereist." });
}
try {
const payload = jwt.verify(token, JWT_SECRET);
const user = await getUserById(payload.sub);
if (!user) {
return res.status(401).json({ error: "Gebruiker niet gevonden." });
}
req.user = user;
req.token = token;
next();
} catch (error) {
return res.status(401).json({ error: "Ongeldige of verlopen token." });
}
}