Skip to content
This repository was archived by the owner on Feb 10, 2025. It is now read-only.

Commit c5002f7

Browse files
committed
fix: build signin url properly
1 parent ada90ae commit c5002f7

2 files changed

Lines changed: 10 additions & 10 deletions

File tree

apps/ui/middleware.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,23 @@
11
import { NextResponse } from "next/server";
22
import type { NextRequest } from "next/server";
33
import { getSignInUrl } from "@/modules/const";
4+
import { env } from "@/env.mjs";
45

56
function redirectProtectedRoute(req: NextRequest) {
67
// TODO check that the cookie is valid not just present
78
const { cookies, headers } = req;
89
const hasSession = cookies.has("sessionid");
9-
const apiBaseUrl = process.env.NEXT_PUBLIC_API_BASE_URL ?? "";
10+
const apiBaseUrl = env.TILAVARAUS_API_URL ?? "";
1011

1112
if (!hasSession) {
1213
// on the server we are behind a gateway so get the forwarded headers
1314
// localhost has no headers
1415
const currentUrl = req.url;
16+
const url = new URL(currentUrl);
1517
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);
2321
}
2422
return undefined;
2523
}

packages/common/src/urlBuilder.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@ export function getSignInUrl(
3030
originOverride?: string
3131
): string {
3232
const authUrl = buildAuthUrl(apiBaseUrl);
33-
// TODO why is originOveride only used when on logout?
3433
if (callBackUrl.includes(`/logout`)) {
34+
// TODO this is unsound if the callback url is not a full url but this at least redirects to an error page
3535
const baseUrl =
3636
originOverride != null ? originOverride : new URL(callBackUrl).origin;
3737
return `${authUrl}login?next=${baseUrl}`;
3838
}
39-
return `${authUrl}login?next=${callBackUrl}`;
39+
const next =
40+
originOverride != null ? `${originOverride}/${callBackUrl}` : callBackUrl;
41+
return `${authUrl}login?next=${next}`;
4042
}
4143

4244
/// @param apiBaseUrl - base url for api (hostname typically)

0 commit comments

Comments
 (0)