import { withAuth } from "next-auth/middleware"; import { NextResponse } from "next/server"; export default withAuth( function middleware(req) { // Allow access to the root path and signin page if (req.nextUrl.pathname === "/" || req.nextUrl.pathname === "/signin") { return NextResponse.next(); } // For all other routes, check authentication return NextResponse.next(); }, { callbacks: { authorized: ({ token, req }) => { // Allow access to the root path and signin page if (req.nextUrl.pathname === "/" || req.nextUrl.pathname === "/signin") { return true; } // Check if the request is for an API route if (req.nextUrl.pathname.startsWith('/api/')) { // For API routes, require a valid token return !!token; } // For all other routes, require a valid token return !!token; }, }, pages: { signIn: "/signin", }, } ); export const config = { matcher: [ /* * Match all request paths except for the ones starting with: * - api/auth (auth endpoints) * - _next/static (static files) * - _next/image (image optimization files) * - favicon.ico (favicon file) * - public folder */ "/((?!api/auth|_next/static|_next/image|favicon.ico|public).*)", ], };