Spaces:
Build error
Build error
| import { NextResponse } from "next/server" | |
| import type { NextRequest } from "next/server" | |
| export function middleware(request: NextRequest) { | |
| const { pathname } = request.nextUrl | |
| // Public routes that don't require authentication | |
| const publicRoutes = [ | |
| "/", | |
| "/auth/signin", | |
| "/auth/signup", | |
| "/auth/forgot-password", | |
| "/auth/reset-password", | |
| ] | |
| // Check if route is public | |
| const isPublicRoute = publicRoutes.some((route) => pathname === route || pathname.startsWith(route)) | |
| // Get session token from cookie or header | |
| const sessionToken = request.cookies.get("session_token")?.value || | |
| request.headers.get("x-session-token") | |
| // If accessing public route, allow | |
| if (isPublicRoute) { | |
| return NextResponse.next() | |
| } | |
| // If no session token and trying to access protected route, redirect to signin | |
| if (!sessionToken && !isPublicRoute) { | |
| const signInUrl = new URL("/auth/signin", request.url) | |
| signInUrl.searchParams.set("redirect", pathname) | |
| return NextResponse.redirect(signInUrl) | |
| } | |
| return NextResponse.next() | |
| } | |
| // Matcher configuration for Next.js middleware | |
| export const config = { | |
| matcher: [ | |
| /* | |
| * Match all request paths except for the ones starting with: | |
| * - api (API routes) | |
| * - _next/static (static files) | |
| * - _next/image (image optimization files) | |
| * - favicon.ico (favicon file) | |
| * - public files (images, etc.) | |
| */ | |
| "/((?!api|_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp|ico)).*)", | |
| ], | |
| } | |