| @@ -1,14 +1,27 @@ |
| 1 | | −import { authkitMiddleware } from "@workos-inc/authkit-nextjs"; |
| 2 | 1 | import type { NextFetchEvent, NextRequest } from "next/server"; |
| 2 | +import { NextResponse } from "next/server"; |
| 3 | 3 | |
| 4 | | −const handler = authkitMiddleware({ |
| 5 | | − middlewareAuth: { |
| 6 | | − enabled: false, |
| 7 | | − unauthenticatedPaths: ["/", "/sign-in", "/sign-up", "/callback", "/sign-out"], |
| 8 | | − }, |
| 9 | | −}); |
| 4 | +function getRedirectUri(request: NextRequest): string { |
| 5 | + const host = request.headers.get("x-forwarded-host") ?? request.headers.get("host"); |
| 6 | + const proto = request.headers.get("x-forwarded-proto") ?? request.nextUrl.protocol.replace(":", ""); |
| 7 | + const origin = host && proto ? `${proto}://${host}` : request.nextUrl.origin; |
| 8 | + return `${origin}/callback`; |
| 9 | +} |
| 10 | + |
| 11 | +export async function proxy(request: NextRequest, event: NextFetchEvent) { |
| 12 | + if (!process.env.WORKOS_CLIENT_ID || !process.env.WORKOS_API_KEY || !process.env.WORKOS_COOKIE_PASSWORD) { |
| 13 | + return NextResponse.next(); |
| 14 | + } |
| 15 | + |
| 16 | + const { authkitMiddleware } = await import("@workos-inc/authkit-nextjs"); |
| 17 | + const handler = authkitMiddleware({ |
| 18 | + redirectUri: getRedirectUri(request), |
| 19 | + middlewareAuth: { |
| 20 | + enabled: false, |
| 21 | + unauthenticatedPaths: ["/", "/sign-in", "/sign-up", "/callback", "/sign-out"], |
| 22 | + }, |
| 23 | + }); |
| 10 | 24 | |
| 11 | | −export function proxy(request: NextRequest, event: NextFetchEvent) { |
| 12 | 25 | return handler(request, event); |
| 13 | 26 | } |
| 14 | 27 | |