import { NextRequest, NextResponse } from "next/server"; import { getIronSession } from "iron-session"; import type { SessionData } from "@/lib/auth"; const sessionOptions = { password: process.env.SESSION_SECRET || "dev-session-secret-change-in-production-must-be-32-chars", cookieName: "study-app-session", }; // Routes that don't require authentication const publicPaths = ["/login", "/api/auth", "/shared"]; export async function middleware(request: NextRequest) { const { pathname } = request.nextUrl; // Allow public routes if (publicPaths.some((path) => pathname.startsWith(path))) { return NextResponse.next(); } // Allow static assets and Next.js internals if ( pathname.startsWith("/_next") || pathname.startsWith("/favicon") || pathname.includes(".") ) { return NextResponse.next(); } // Check session const response = NextResponse.next(); const session = await getIronSession( request, response, sessionOptions ); if (!session.isAuthenticated) { return NextResponse.redirect(new URL("/login", request.url)); } return response; } export const config = { matcher: ["/((?!_next/static|_next/image|favicon.ico).*)"], };