Skip to content

Commit c6d1d45

Browse files
author
Rajat
committed
Login redirection fix
1 parent 82ff26c commit c6d1d45

5 files changed

Lines changed: 39 additions & 4 deletions

File tree

apps/web/app/login/page.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { redirect } from "next/navigation";
2-
import { API_URL, WEB_CLIENT } from "@/lib/config";
2+
import { API_PUBLIC_URL, WEB_CLIENT } from "@/lib/config";
3+
import { getDashboardLoginUrl } from "@/lib/login-url";
4+
5+
export const dynamic = "force-dynamic";
36

47
/**
58
* `apps/api` hosts the only login UI (see the "Unified Login Screen"
@@ -8,6 +11,10 @@ import { API_URL, WEB_CLIENT } from "@/lib/config";
811
* bounces the browser there and back.
912
*/
1013
export default function LoginPage() {
11-
const target = `${API_URL}/login?redirect=${encodeURIComponent(`${WEB_CLIENT}/dashboard`)}`;
12-
redirect(target);
14+
redirect(
15+
getDashboardLoginUrl({
16+
apiPublicUrl: API_PUBLIC_URL,
17+
webClient: WEB_CLIENT,
18+
}),
19+
);
1320
}

apps/web/lib/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { DEFAULT_MAX_UPLOAD_SIZE_BYTES } from "@/lib/media-limits";
22

33
export const API_URL = process.env.API_URL || "http://localhost:4000";
4+
export const API_PUBLIC_URL =
5+
process.env.API_PUBLIC_URL || "http://localhost:4000";
46

57
/** This app's own public origin — sent to `apps/api`'s hosted login page
68
* (`GET /login?redirect=...`) as where to bounce the browser back to after

apps/web/lib/login-url.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { describe, expect, it } from "vitest";
2+
import { getDashboardLoginUrl } from "./login-url";
3+
4+
describe("dashboard login redirect", () => {
5+
it("uses the public API URL for browser redirects", () => {
6+
expect(
7+
getDashboardLoginUrl({
8+
apiPublicUrl: "https://api.sendlit.com",
9+
webClient: "https://app.sendlit.com",
10+
}),
11+
).toBe(
12+
"https://api.sendlit.com/login?redirect=https%3A%2F%2Fapp.sendlit.com%2Fdashboard",
13+
);
14+
});
15+
});

apps/web/lib/login-url.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
type LoginUrlInput = {
2+
apiPublicUrl: string;
3+
webClient: string;
4+
};
5+
6+
export function getDashboardLoginUrl({
7+
apiPublicUrl,
8+
webClient,
9+
}: LoginUrlInput): string {
10+
return `${apiPublicUrl}/login?redirect=${encodeURIComponent(`${webClient}/dashboard`)}`;
11+
}

apps/web/tsconfig.tsbuildinfo

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)