- Replaced static hosts.json and staticConfig.ts with SQLite database (Prisma) - Implemented JWT authentication and Login UI - Added dynamic API routes for hosts, topology, and settings - Updated UI components to fetch and manage state dynamically - Added Settings interface for managing hosts and topology nodes
28 lines
892 B
TypeScript
28 lines
892 B
TypeScript
import { Request, Response, NextFunction } from 'express';
|
|
import jwt from 'jsonwebtoken';
|
|
|
|
export interface AuthRequest extends Request {
|
|
user?: {
|
|
id: string;
|
|
username: string;
|
|
};
|
|
}
|
|
|
|
const JWT_SECRET = process.env.JWT_SECRET || 'fallback-secret-for-development-only';
|
|
|
|
export const requireAuth = (req: AuthRequest, res: Response, next: NextFunction) => {
|
|
const authHeader = req.headers.authorization;
|
|
if (!authHeader?.startsWith('Bearer ')) {
|
|
return res.status(401).json({ status: 'error', message: 'Authentication required' });
|
|
}
|
|
|
|
const token = authHeader.split(' ')[1];
|
|
try {
|
|
const decoded = jwt.verify(token, JWT_SECRET) as { id: string; username: string };
|
|
req.user = decoded;
|
|
next();
|
|
} catch (error) {
|
|
return res.status(401).json({ status: 'error', message: 'Invalid or expired token' });
|
|
}
|
|
};
|