From 71062a57be1a961e0b2a8983139190afabc78a23 Mon Sep 17 00:00:00 2001 From: saravanakumardb1 Date: Mon, 13 Apr 2026 09:59:18 -0700 Subject: [PATCH] fix(web): fix auth token key inconsistency and DRY getAccessToken() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit feature-flags.ts and prompt-client.ts used bare 'access_token' key instead of PRODUCT_ID-prefixed key — auth tokens were never sent. Consolidates 10 web lib files to import the shared getAccessToken() from api-helpers.ts instead of each redefining their own copy. --- web/src/lib/blob-client.ts | 6 +----- web/src/lib/broadcast-client.ts | 8 ++------ web/src/lib/diagnostics.ts | 6 ++---- web/src/lib/extraction-client.ts | 6 +----- web/src/lib/feature-flags.ts | 6 ++---- web/src/lib/feedback-client.ts | 6 +----- web/src/lib/platform-api.ts | 6 +----- web/src/lib/platform.ts | 6 ++---- web/src/lib/prompt-client.ts | 4 ++-- web/src/lib/survey-client.ts | 8 ++------ 10 files changed, 16 insertions(+), 46 deletions(-) diff --git a/web/src/lib/blob-client.ts b/web/src/lib/blob-client.ts index 80a4d66..cd732a6 100644 --- a/web/src/lib/blob-client.ts +++ b/web/src/lib/blob-client.ts @@ -2,11 +2,7 @@ import { createBlobClient } from "@bytelyst/blob-client"; import { PLATFORM_SERVICE_URL, PRODUCT_ID } from "@/lib/product-config"; - -function getAccessToken(): string | null { - if (typeof window === "undefined") return null; - return localStorage.getItem(`${PRODUCT_ID}_access_token`); -} +import { getAccessToken } from "@/lib/api-helpers"; let _blobClient: ReturnType | null = null; function getBlobClient() { diff --git a/web/src/lib/broadcast-client.ts b/web/src/lib/broadcast-client.ts index b323f1f..ea0fcce 100644 --- a/web/src/lib/broadcast-client.ts +++ b/web/src/lib/broadcast-client.ts @@ -2,11 +2,7 @@ import { createBroadcastClient, type BroadcastClient } from "@bytelyst/broadcast-client"; import { PLATFORM_SERVICE_URL, PRODUCT_ID } from "@/lib/product-config"; - -function getAccessToken(): string { - if (typeof window === "undefined") return ""; - return localStorage.getItem(`${PRODUCT_ID}_access_token`) ?? ""; -} +import { getAccessToken } from "@/lib/api-helpers"; let _client: BroadcastClient | null = null; export function getBroadcastClient(): BroadcastClient { @@ -14,7 +10,7 @@ export function getBroadcastClient(): BroadcastClient { _client = createBroadcastClient({ baseUrl: PLATFORM_SERVICE_URL, productId: PRODUCT_ID, - getAuthToken: getAccessToken, + getAuthToken: () => getAccessToken() ?? "", platform: "web", appVersion: "0.1.0", osVersion: typeof navigator !== "undefined" ? navigator.userAgent.slice(0, 40) : "unknown", diff --git a/web/src/lib/diagnostics.ts b/web/src/lib/diagnostics.ts index 5effa19..38a1b9d 100644 --- a/web/src/lib/diagnostics.ts +++ b/web/src/lib/diagnostics.ts @@ -2,15 +2,13 @@ import { createWebDiagnostics } from "@bytelyst/diagnostics-client"; import { DIAGNOSTICS_URL, PRODUCT_ID } from "@/lib/product-config"; +import { getAccessToken } from "@/lib/api-helpers"; const { init: initDiagnostics, stop: stopDiagnostics } = createWebDiagnostics({ productId: PRODUCT_ID, channel: "notes_web", serverUrl: DIAGNOSTICS_URL, - getAuthToken: () => { - if (typeof window === "undefined") return ""; - return localStorage.getItem(`${PRODUCT_ID}_access_token`) ?? ""; - }, + getAuthToken: () => getAccessToken() ?? "", }); export { initDiagnostics, stopDiagnostics }; diff --git a/web/src/lib/extraction-client.ts b/web/src/lib/extraction-client.ts index 9118da2..c313f6a 100644 --- a/web/src/lib/extraction-client.ts +++ b/web/src/lib/extraction-client.ts @@ -2,13 +2,9 @@ import { createExtractionClient, type ExtractionClient } from "@bytelyst/extraction"; import { EXTRACTION_SERVICE_URL, PRODUCT_ID } from "@/lib/product-config"; +import { getAccessToken } from "@/lib/api-helpers"; import type { NoteTask } from "@/lib/types"; -function getAccessToken(): string | null { - if (typeof window === "undefined") return null; - return localStorage.getItem(`${PRODUCT_ID}_access_token`); -} - let _client: ExtractionClient | null = null; function getClient(): ExtractionClient { if (!_client) { diff --git a/web/src/lib/feature-flags.ts b/web/src/lib/feature-flags.ts index 88520c4..aa4864e 100644 --- a/web/src/lib/feature-flags.ts +++ b/web/src/lib/feature-flags.ts @@ -2,6 +2,7 @@ import { createFeatureFlagClient } from "@bytelyst/feature-flag-client"; import { PLATFORM_SERVICE_URL, PRODUCT_ID } from "@/lib/product-config"; +import { getAccessToken } from "@/lib/api-helpers"; let initialized = false; @@ -11,10 +12,7 @@ const featureFlagClient = createFeatureFlagClient({ platform: "web", useStreaming: true, pollIntervalMs: 5 * 60 * 1000, - getAccessToken: () => - typeof window !== "undefined" - ? localStorage.getItem("access_token") - : null, + getAccessToken, }); export async function initFeatureFlags(userId?: string) { diff --git a/web/src/lib/feedback-client.ts b/web/src/lib/feedback-client.ts index 0942c9f..f65f141 100644 --- a/web/src/lib/feedback-client.ts +++ b/web/src/lib/feedback-client.ts @@ -2,11 +2,7 @@ import { createFeedbackClient, type FeedbackClient } from "@bytelyst/feedback-client"; import { PLATFORM_SERVICE_URL, PRODUCT_ID } from "@/lib/product-config"; - -function getAccessToken(): string | null { - if (typeof window === "undefined") return null; - return localStorage.getItem(`${PRODUCT_ID}_access_token`); -} +import { getAccessToken } from "@/lib/api-helpers"; let _client: FeedbackClient | null = null; export function getFeedbackClient(): FeedbackClient { diff --git a/web/src/lib/platform-api.ts b/web/src/lib/platform-api.ts index 680d5c9..e0c4f75 100644 --- a/web/src/lib/platform-api.ts +++ b/web/src/lib/platform-api.ts @@ -2,11 +2,7 @@ import { createPlatformClient, type PlatformClient } from "@bytelyst/platform-client"; import { PLATFORM_SERVICE_URL, PRODUCT_ID } from "@/lib/product-config"; - -function getAccessToken(): string | null { - if (typeof window === "undefined") return null; - return localStorage.getItem(`${PRODUCT_ID}_access_token`); -} +import { getAccessToken } from "@/lib/api-helpers"; let _client: PlatformClient | null = null; function getClient(): PlatformClient { diff --git a/web/src/lib/platform.ts b/web/src/lib/platform.ts index fc8bf41..5fc7d50 100644 --- a/web/src/lib/platform.ts +++ b/web/src/lib/platform.ts @@ -2,13 +2,11 @@ import { createPlatformClient } from "@bytelyst/platform-client"; import { PLATFORM_SERVICE_URL, PRODUCT_ID } from "@/lib/product-config"; +import { getAccessToken } from "@/lib/api-helpers"; export const platformClient = createPlatformClient({ baseUrl: PLATFORM_SERVICE_URL, productId: PRODUCT_ID, - getAccessToken: () => { - if (typeof window === "undefined") return null; - return localStorage.getItem(`${PRODUCT_ID}_access_token`); - }, + getAccessToken, refreshAccessToken: async () => false, }); diff --git a/web/src/lib/prompt-client.ts b/web/src/lib/prompt-client.ts index e564238..d69e9da 100644 --- a/web/src/lib/prompt-client.ts +++ b/web/src/lib/prompt-client.ts @@ -1,6 +1,6 @@ "use client"; -import { createNotesApiClient } from "@/lib/api-helpers"; +import { createNotesApiClient, getAccessToken } from "@/lib/api-helpers"; import type { PromptTemplate, RunPromptInput, @@ -57,7 +57,7 @@ export async function runPromptStream( ): Promise { const api = createNotesApiClient(); const baseUrl = (api as unknown as { baseUrl?: string }).baseUrl ?? ""; - const token = typeof window !== "undefined" ? localStorage.getItem("access_token") : null; + const token = getAccessToken(); const headers: Record = { "Content-Type": "application/json" }; if (token) headers["Authorization"] = `Bearer ${token}`; diff --git a/web/src/lib/survey-client.ts b/web/src/lib/survey-client.ts index b4362de..891dd85 100644 --- a/web/src/lib/survey-client.ts +++ b/web/src/lib/survey-client.ts @@ -2,11 +2,7 @@ import { createSurveyClient, type SurveyClient } from "@bytelyst/survey-client"; import { PLATFORM_SERVICE_URL, PRODUCT_ID } from "@/lib/product-config"; - -function getAccessToken(): string { - if (typeof window === "undefined") return ""; - return localStorage.getItem(`${PRODUCT_ID}_access_token`) ?? ""; -} +import { getAccessToken } from "@/lib/api-helpers"; let _client: SurveyClient | null = null; export function getSurveyClient(): SurveyClient { @@ -14,7 +10,7 @@ export function getSurveyClient(): SurveyClient { _client = createSurveyClient({ baseUrl: PLATFORM_SERVICE_URL, productId: PRODUCT_ID, - getAuthToken: getAccessToken, + getAuthToken: () => getAccessToken() ?? "", platform: "web", appVersion: "0.1.0", osVersion: typeof navigator !== "undefined" ? navigator.userAgent.slice(0, 40) : "unknown",