|
1 | 1 | import { NextResponse } from "next/server"; |
2 | 2 | import type { NextRequest } from "next/server"; |
3 | 3 | import { getSignInUrl } from "@/modules/const"; |
| 4 | +import { env } from "@/env.mjs"; |
4 | 5 |
|
5 | 6 | function redirectProtectedRoute(req: NextRequest) { |
6 | 7 | // TODO check that the cookie is valid not just present |
7 | 8 | const { cookies, headers } = req; |
8 | 9 | const hasSession = cookies.has("sessionid"); |
9 | | - const apiBaseUrl = process.env.NEXT_PUBLIC_API_BASE_URL ?? ""; |
| 10 | + const apiBaseUrl = env.TILAVARAUS_API_URL ?? ""; |
10 | 11 |
|
11 | 12 | if (!hasSession) { |
12 | 13 | // on the server we are behind a gateway so get the forwarded headers |
13 | 14 | // localhost has no headers |
14 | 15 | const currentUrl = req.url; |
| 16 | + const url = new URL(currentUrl); |
15 | 17 | const protocol = headers.get("x-forwarded-proto") ?? "http"; |
16 | | - const host = headers.get("x-forwarded-host"); |
17 | | - const originalUrl = headers.get("x-original-url"); |
18 | | - if (host && originalUrl) { |
19 | | - const origin = `${protocol}://${host}`; |
20 | | - return getSignInUrl(apiBaseUrl, originalUrl, origin); |
21 | | - } |
22 | | - return getSignInUrl(apiBaseUrl, currentUrl); |
| 18 | + const host = headers.get("x-forwarded-host") ?? url.host; |
| 19 | + const origin = `${protocol}://${host}`; |
| 20 | + return getSignInUrl(apiBaseUrl, url.pathname, origin); |
23 | 21 | } |
24 | 22 | return undefined; |
25 | 23 | } |
|
0 commit comments