deafmissoula-website/server/src/middleware/auth.ts
friday-bot ef8d281b2b v2: MongoDB backend + /manage dashboard
- Replace Express 4 backend with Express 5 + Mongoose 8 + TypeScript 5.8
- Add MongoDB 8.0 container via docker-compose
- Add JWT auth (jose), argon2 password hashing, zod validation, multer uploads
- Models: BoardMember, Event, GalleryItem, Sponsor, Minute, Member, User
- Full CRUD REST API for all content types
- Seed script to populate MongoDB from hardcoded data
- Wire all frontend components to fetch from API (no JSX changes)
- Add /manage dashboard: login + tabs for Board, Events, Gallery, Sponsors, Minutes, Members
- Uploads volume for persistent file storage across deploys
2026-05-27 16:23:12 -06:00

27 lines
821 B
TypeScript

import { Request, Response, NextFunction } from 'express';
import { jwtVerify } from 'jose';
export interface AuthRequest extends Request {
userId?: string;
userRole?: string;
}
const secret = () => new TextEncoder().encode(process.env.JWT_SECRET ?? 'fallback-dev-secret');
export async function requireAuth(req: AuthRequest, res: Response, next: NextFunction): Promise<void> {
const header = req.headers.authorization;
if (!header?.startsWith('Bearer ')) {
res.status(401).json({ error: 'Unauthorized' });
return;
}
try {
const token = header.slice(7);
const { payload } = await jwtVerify(token, secret());
req.userId = payload.userId as string;
req.userRole = payload.role as string;
next();
} catch {
res.status(401).json({ error: 'Invalid or expired token' });
}
}