This commit is contained in:
Jack Mechem 2026-05-01 16:21:50 -07:00
parent c991fe7b6d
commit e6b5fed399
8 changed files with 6647 additions and 6623 deletions

37
middleware.ts Normal file
View file

@ -0,0 +1,37 @@
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
const ENROLLMENT_OPEN = process.env.ENROLLMENT_OPEN === "true";
export function middleware(req: NextRequest) {
const { pathname } = req.nextUrl;
// Enrollment routes — only accessible when enrollment is open
if (
pathname.startsWith("/enroll") ||
pathname.startsWith("/api/auth/register")
) {
return ENROLLMENT_OPEN
? NextResponse.next()
: new NextResponse(null, { status: 404 });
}
// Always allow login page and auth api routes
if (pathname.startsWith("/auth") || pathname.startsWith("/api/auth")) {
return NextResponse.next();
}
// No token — redirect to login
const token = req.cookies.get("token")?.value;
if (!token) {
const loginUrl = new URL("/auth", req.url);
loginUrl.searchParams.set("callbackUrl", pathname);
return NextResponse.redirect(loginUrl);
}
return NextResponse.next();
}
export const config = {
matcher: ["/((?!_next/static|_next/image|favicon.ico).*)"],
};