| 1 | import { auth } from "@/server/auth"; |
| 2 | import { NextResponse, type NextRequest } from "next/server"; |
| 3 | |
| 4 | export async function proxy(request: NextRequest) { |
| 5 | const session = await auth(); |
| 6 | const isAuthPage = request.nextUrl.pathname.startsWith("/auth"); |
| 7 | |
| 8 | // Always redirect from root to /presentation |
| 9 | if (request.nextUrl.pathname === "/") { |
| 10 | return NextResponse.redirect(new URL("/presentation", request.url)); |
| 11 | } |
| 12 | |
| 13 | // If user is on auth page but already signed in, redirect to home page |
| 14 | if (isAuthPage && session) { |
| 15 | return NextResponse.redirect(new URL("/presentation", request.url)); |
| 16 | } |
| 17 | |
| 18 | // If user is not authenticated and trying to access a protected route, redirect to sign-in |
| 19 | if (!session && !isAuthPage && !request.nextUrl.pathname.startsWith("/api")) { |
| 20 | return NextResponse.redirect( |
| 21 | new URL( |
| 22 | `/auth/signin?callbackUrl=${encodeURIComponent(request.url)}`, |
| 23 | request.url, |
| 24 | ), |
| 25 | ); |
| 26 | } |
| 27 | |
| 28 | return NextResponse.next(); |
| 29 | } |
| 30 | |
| 31 | // Add routes that should be protected by authentication |
| 32 | export const config = { |
| 33 | matcher: ["/((?!api|_next/static|_next/image|favicon.ico).*)"], |
| 34 | }; |
| 35 |