From 5cbe90e5403ccff13349f9ec39c126fb43e98b16 Mon Sep 17 00:00:00 2001 From: Saravana Achu Mac Date: Sat, 4 Apr 2026 14:29:59 -0700 Subject: [PATCH] refactor: use common mobile sdk for platform auth requests --- mobile/lib/platformAuth.ts | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/mobile/lib/platformAuth.ts b/mobile/lib/platformAuth.ts index 4d69ac7..addc15d 100644 --- a/mobile/lib/platformAuth.ts +++ b/mobile/lib/platformAuth.ts @@ -1,5 +1,5 @@ import { secureSessionStorage, clearMobileSessionStorage, MOBILE_SESSION_STORAGE_KEY } from '@/lib/secureSessionStorage'; -import { mobileRuntime } from '@/lib/runtime'; +import { createMobilePlatformSdk, mobileRuntime } from '@/lib/runtime'; export interface PlatformAuthUser { id: string; @@ -25,6 +25,10 @@ class PlatformAuthError extends Error { } } +function createPlatformAuthSdk(accessToken: string | null = null) { + return createMobilePlatformSdk(() => accessToken); +} + function normalizeUser(value: any): PlatformAuthUser { return { id: String(value?.id || value?.sub || '').trim(), @@ -43,13 +47,9 @@ async function platformRequest( body?: Record; } ): Promise { - const response = await fetch(`${mobileRuntime.platformApiUrl}${path}`, { + const sdk = createPlatformAuthSdk(options?.accessToken ?? null); + const response = await sdk.fetch(path, { method: options?.method || 'GET', - headers: { - 'Content-Type': 'application/json', - 'x-product-id': mobileRuntime.productId, - ...(options?.accessToken ? { Authorization: `Bearer ${options.accessToken}` } : {}), - }, body: options?.body ? JSON.stringify(options.body) : undefined, }); @@ -79,6 +79,10 @@ async function writeSession(session: StoredPlatformSession): Promise { await secureSessionStorage.setItem(MOBILE_SESSION_STORAGE_KEY, JSON.stringify(session)); } +export async function persistPlatformSession(session: StoredPlatformSession): Promise { + await writeSession(session); +} + export async function readPlatformSession(): Promise { const raw = await secureSessionStorage.getItem(MOBILE_SESSION_STORAGE_KEY); if (!raw) { @@ -119,6 +123,20 @@ export async function refreshPlatformSession(refreshToken: string): Promise { + const user = await getCurrentPlatformUser(accessToken); + const session: StoredPlatformSession = { + accessToken, + refreshToken, + user, + }; + await writeSession(session); + return session; +} + export async function restorePlatformSession(): Promise { const stored = await readPlatformSession(); if (!stored) {