- 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.
24 lines
829 B
JavaScript
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." });
|
|
}
|
|
}
|