From f4c9a8d89702fbe61ae1a06fa7fcffc5e9508510 Mon Sep 17 00:00:00 2001 From: Gautam25Raj Date: Thu, 28 May 2026 20:51:27 +0530 Subject: [PATCH] fix: update fetchAccountProfile to ensure valid authentication cookie is present --- .../features/auth/services/current-user.ts | 68 +++++++++++-------- .../profile/services/account-profile.ts | 4 ++ 2 files changed, 43 insertions(+), 29 deletions(-) diff --git a/apps/studio/features/auth/services/current-user.ts b/apps/studio/features/auth/services/current-user.ts index abe6bc2..8bea289 100644 --- a/apps/studio/features/auth/services/current-user.ts +++ b/apps/studio/features/auth/services/current-user.ts @@ -13,27 +13,37 @@ export type SessionUser = { shareResumeCount?: number; }; -type MasterProfileSummaryPayload = { - profile?: { userId?: string }; - summary?: Partial< - Pick< - SessionUser, - | "id" - | "email" - | "name" - | "createdAt" - | "emailVerified" - | "autoSyncEnabled" - | "shareResumeCount" - > - >; +type AccountProfileResponse = { + data?: { + id: string; + email: string; + name?: string | null; + createdAt?: string; + emailVerified?: boolean; + autoSyncEnabled?: boolean; + _count?: { + shareLinks?: number; + }; + }; }; let memoryCache: SessionUser | null = null; -async function fetchMasterProfileSummary(cookieHeader?: string) { +async function fetchAccountProfileSummary(cookieHeader?: string) { try { - const response = await fetch(backendApiUrl("/profiles/master"), { + if ( + typeof window === "undefined" && + cookieHeader !== undefined && + !cookieHeader.includes("veriworkly-auth") + ) { + return null; + } + + if (typeof window !== "undefined" && !document.cookie.includes("veriworkly-auth")) { + return null; + } + + const response = await fetch(backendApiUrl("/users/me"), { method: "GET", headers: cookieHeader ? { Cookie: cookieHeader } : undefined, credentials: cookieHeader ? undefined : "include", @@ -43,19 +53,19 @@ async function fetchMasterProfileSummary(cookieHeader?: string) { return null; } - const payload = (await response.json()) as { data?: MasterProfileSummaryPayload }; - const summary = payload.data?.summary; + const payload = (await response.json()) as AccountProfileResponse; + const user = payload.data; - if (!summary) return null; + if (!user) return null; return { - id: summary.id ?? payload.data?.profile?.userId ?? "", - email: summary.email ?? "", - name: summary.name, - createdAt: summary.createdAt, - emailVerified: summary.emailVerified, - autoSyncEnabled: summary.autoSyncEnabled, - shareResumeCount: summary.shareResumeCount, + id: user.id, + email: user.email, + name: user.name ?? undefined, + createdAt: user.createdAt, + emailVerified: user.emailVerified, + autoSyncEnabled: user.autoSyncEnabled, + shareResumeCount: user._count?.shareLinks, } satisfies Partial; } catch { return null; @@ -71,7 +81,7 @@ export async function fetchCurrentUser(force = false): Promise throw new Error("Could not update name"); } - const summary = await fetchMasterProfileSummary(); + const summary = await fetchAccountProfileSummary(); const merged = { ...updatedUser, ...summary, diff --git a/apps/studio/features/profile/services/account-profile.ts b/apps/studio/features/profile/services/account-profile.ts index 31b4fe8..a55b1ba 100644 --- a/apps/studio/features/profile/services/account-profile.ts +++ b/apps/studio/features/profile/services/account-profile.ts @@ -22,6 +22,10 @@ export async function fetchAccountProfile(): Promise { const requestHeaders = await headers(); const cookie = requestHeaders.get("cookie"); + if (!cookie || !cookie.includes("veriworkly-auth")) { + return null; + } + const response = await fetch(backendApiUrl("/users/me"), { method: "GET", cache: "no-store",