Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/hungry-bars-pick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@polar-sh/better-auth": minor
---

Adds theme to the portal hook
116 changes: 116 additions & 0 deletions packages/polar-betterauth/src/__tests__/plugins/portal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,122 @@ describe("portal plugin", () => {
expect.stringContaining("Polar customer portal creation failed"),
);
});

it("should apply theme to portal URL when provided", async () => {
const plugin = portal({ theme: "dark" });
const endpoints = plugin(mockClient);
const themeHandler = endpoints.portal.handler;

const mockSession = {
token: "session-token-123",
customerPortalUrl: "https://polar.sh/portal/session-123",
};

vi.mocked(mockClient.customerSessions.create).mockResolvedValue(
mockSession,
);

const ctx = {
context: {
session: { user: { id: "user-123" } },
},
json: vi.fn(),
};

await themeHandler(ctx);

expect(ctx.json).toHaveBeenCalledWith({
url: "https://polar.sh/portal/session-123?theme=dark",
redirect: true,
});
});

it("should support light theme", async () => {
const plugin = portal({ theme: "light" });
const endpoints = plugin(mockClient);
const themeHandler = endpoints.portal.handler;

const mockSession = {
token: "session-token-123",
customerPortalUrl: "https://polar.sh/portal/session-123",
};

vi.mocked(mockClient.customerSessions.create).mockResolvedValue(
mockSession,
);

const ctx = {
context: {
session: { user: { id: "user-123" } },
},
json: vi.fn(),
};

await themeHandler(ctx);

expect(ctx.json).toHaveBeenCalledWith({
url: "https://polar.sh/portal/session-123?theme=light",
redirect: true,
});
});

it("should not add theme parameter when not provided", async () => {
const plugin = portal();
const endpoints = plugin(mockClient);
const noThemeHandler = endpoints.portal.handler;

const mockSession = {
token: "session-token-123",
customerPortalUrl: "https://polar.sh/portal/session-123",
};

vi.mocked(mockClient.customerSessions.create).mockResolvedValue(
mockSession,
);

const ctx = {
context: {
session: { user: { id: "user-123" } },
},
json: vi.fn(),
};

await noThemeHandler(ctx);

expect(ctx.json).toHaveBeenCalledWith({
url: "https://polar.sh/portal/session-123",
redirect: true,
});
});

it("should preserve existing query parameters when adding theme", async () => {
const plugin = portal({ theme: "dark" });
const endpoints = plugin(mockClient);
const themeHandler = endpoints.portal.handler;

const mockSession = {
token: "session-token-123",
customerPortalUrl: "https://polar.sh/portal/session-123?foo=bar",
};

vi.mocked(mockClient.customerSessions.create).mockResolvedValue(
mockSession,
);

const ctx = {
context: {
session: { user: { id: "user-123" } },
},
json: vi.fn(),
};

await themeHandler(ctx);

expect(ctx.json).toHaveBeenCalledWith({
url: "https://polar.sh/portal/session-123?foo=bar&theme=dark",
redirect: true,
});
});
});

describe("state endpoint", () => {
Expand Down
14 changes: 12 additions & 2 deletions packages/polar-betterauth/src/plugins/portal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ import { z } from "zod";

export interface PortalConfig {
returnUrl?: string;
/**
* Portal theme
*/
theme?: "light" | "dark";
}

export const portal =
({ returnUrl }: PortalConfig = {}) =>
({ returnUrl, theme }: PortalConfig = {}) =>
(polar: Polar) => {
const retUrl = returnUrl ? new URL(returnUrl) : undefined;

Expand All @@ -33,8 +37,14 @@ export const portal =
returnUrl: retUrl ? decodeURI(retUrl.toString()) : undefined,
});

const portalUrl = new URL(customerSession.customerPortalUrl);

if (theme) {
portalUrl.searchParams.set("theme", theme);
}

return ctx.json({
url: customerSession.customerPortalUrl,
url: portalUrl.toString(),
redirect: true,
});
} catch (e: unknown) {
Expand Down