| @@ -1,14 +1,27 @@ |
| 1 | import { authkitMiddleware } from "@workos-inc/authkit-nextjs"; | | |
| 2 | import type { NextFetchEvent, NextRequest } from "next/server"; | 1 | import type { NextFetchEvent, NextRequest } from "next/server"; |
| | 2 | import { NextResponse } from "next/server"; |
| 3 | | 3 | |
| 4 | const handler = authkitMiddleware({ | 4 | function getRedirectUri(request: NextRequest): string { |
| 5 | middlewareAuth: { | 5 | const host = request.headers.get("x-forwarded-host") ?? request.headers.get("host"); |
| 6 | enabled: false, | 6 | const proto = request.headers.get("x-forwarded-proto") ?? request.nextUrl.protocol.replace(":", ""); |
| 7 | unauthenticatedPaths: ["/", "/sign-in", "/sign-up", "/callback", "/sign-out"], | 7 | const origin = host && proto ? `${proto}://${host}` : request.nextUrl.origin; |
| 8 | }, | 8 | return `${origin}/callback`; |
| 9 | }); | 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 | return handler(request, event); | 25 | return handler(request, event); |
| 13 | } | 26 | } |
| 14 | | 27 | |