Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

fix: use the SaaS login session for Usage & Billing (#8006)

ConnorYohbfc1549b5be6
diff --git a/frontend/editor/src/portal-saas/auth/portalSaasSession.test.ts b/frontend/editor/src/portal-saas/auth/portalSaasSession.test.ts
new file mode 100644
index 0000000000..5a0425af09
--- /dev/null
+++ b/frontend/editor/src/portal-saas/auth/portalSaasSession.test.ts
@@ -0,0 +1,71 @@
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+const { getSession, getLinkedClient, ensureLinkClient, fetchMock } = vi.hoisted(
+ () => ({
+ getSession: vi.fn(),
+ getLinkedClient: vi.fn(() => null),
+ ensureLinkClient: vi.fn(() => null),
+ fetchMock: vi.fn(),
+ }),
+);
+
+vi.mock("@app/auth/supabase", () => ({ supabase: { auth: { getSession } } }));
+vi.mock("@app/auth/supabase/supabaseClient", () => ({
+ getSupabaseClient: getLinkedClient,
+}));
+vi.mock("@portal/auth/saasSupabase", () => ({
+ ensureSaasSupabase: ensureLinkClient,
+}));
+
+import { getPortalSaasToken } from "@portal/auth/portalSaasSession";
+import { fetchWallet } from "@portal/api/billing";
+import { SaasNotLinkedError } from "@portal/api/http";
+
+describe("SaaS billing session", () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ vi.stubEnv("VITE_API_BASE_URL", "https://saas.example");
+ vi.stubGlobal("fetch", fetchMock);
+ getSession.mockResolvedValue({
+ data: { session: { access_token: "signed-in-saas-token" } },
+ });
+ fetchMock.mockImplementation(
+ async () => new Response(JSON.stringify({ teamId: 42 }), { status: 200 }),
+ );
+ });
+
+ afterEach(() => {
+ vi.unstubAllGlobals();
+ vi.unstubAllEnvs();
+ });
+
+ it("loads the wallet using the signed-in SaaS session without an instance link", async () => {
+ expect(await fetchWallet()).toEqual({ teamId: 42 });
+ expect(fetchMock).toHaveBeenCalledWith(
+ "https://saas.example/api/v1/payg/wallet",
+ expect.objectContaining({
+ headers: expect.objectContaining({
+ Authorization: "Bearer signed-in-saas-token",
+ }),
+ }),
+ );
+ expect(ensureLinkClient).not.toHaveBeenCalled();
+ expect(getLinkedClient).not.toHaveBeenCalled();
+ });
+
+ it("reads the current SaaS session after its token changes", async () => {
+ expect(await getPortalSaasToken()).toBe("signed-in-saas-token");
+ getSession.mockResolvedValue({
+ data: { session: { access_token: "refreshed-saas-token" } },
+ });
+ expect(await getPortalSaasToken()).toBe("refreshed-saas-token");
+ });
+
+ it("does not borrow an account-link session when the SaaS user signs out", async () => {
+ getSession.mockResolvedValue({ data: { session: null } });
+ expect(await getPortalSaasToken()).toBeNull();
+ await expect(fetchWallet()).rejects.toBeInstanceOf(SaasNotLinkedError);
+ expect(fetchMock).not.toHaveBeenCalled();
+ expect(ensureLinkClient).not.toHaveBeenCalled();
+ });
+});
diff --git a/frontend/editor/src/portal-saas/auth/portalSaasSession.ts b/frontend/editor/src/portal-saas/auth/portalSaasSession.ts
new file mode 100644
index 0000000000..c5838366d7
--- /dev/null
+++ b/frontend/editor/src/portal-saas/auth/portalSaasSession.ts
@@ -0,0 +1,2 @@
+/** Hosted billing shares the app's signed-in session; it has no instance-link session to initialise. */
+export { getAccessToken as getPortalSaasToken } from "@app/auth/session";
diff --git a/frontend/editor/src/portal/auth/portalSaasSession.ts b/frontend/editor/src/portal/auth/portalSaasSession.ts
index eb75cc7650..600f75082c 100644
--- a/frontend/editor/src/portal/auth/portalSaasSession.ts
+++ b/frontend/editor/src/portal/auth/portalSaasSession.ts
@@ -2,13 +2,9 @@ import { getSupabaseClient } from "@app/auth/supabase/supabaseClient";
import { ensureSaasSupabase } from "@portal/auth/saasSupabase";
/**
- * SaaS access token for attended portal→SaaS reads. Reads the shared Supabase
- * client, which {@link ensureSaasSupabase} configures against the one Stirling
- * Supabase project (VITE_SUPABASE_*) both flavors talk to: self-hosted mints a
- * SaaS JWT in-app for account-linking (alongside its own Spring session); SaaS is
- * already signed into that same project, so the editor session is inherited.
- * Flavor-agnostic — no per-flavor override needed. Returns null until a session
- * exists.
+ * Self-hosted portal→SaaS reads use the account-link session alongside the local
+ * Spring session. Returns null until that separate SaaS session exists. The
+ * hosted build overrides this with its existing app session.
*/
export async function getPortalSaasToken(): Promise<string | null> {
ensureSaasSupabase();