refactor: use common mobile sdk for platform auth requests
This commit is contained in:
parent
b551ab2a4f
commit
5cbe90e540
@ -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<T>(
|
||||
body?: Record<string, unknown>;
|
||||
}
|
||||
): Promise<T> {
|
||||
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<void> {
|
||||
await secureSessionStorage.setItem(MOBILE_SESSION_STORAGE_KEY, JSON.stringify(session));
|
||||
}
|
||||
|
||||
export async function persistPlatformSession(session: StoredPlatformSession): Promise<void> {
|
||||
await writeSession(session);
|
||||
}
|
||||
|
||||
export async function readPlatformSession(): Promise<StoredPlatformSession | null> {
|
||||
const raw = await secureSessionStorage.getItem(MOBILE_SESSION_STORAGE_KEY);
|
||||
if (!raw) {
|
||||
@ -119,6 +123,20 @@ export async function refreshPlatformSession(refreshToken: string): Promise<Stor
|
||||
return session;
|
||||
}
|
||||
|
||||
export async function syncPlatformSessionFromTokens(
|
||||
accessToken: string,
|
||||
refreshToken: string
|
||||
): Promise<StoredPlatformSession> {
|
||||
const user = await getCurrentPlatformUser(accessToken);
|
||||
const session: StoredPlatformSession = {
|
||||
accessToken,
|
||||
refreshToken,
|
||||
user,
|
||||
};
|
||||
await writeSession(session);
|
||||
return session;
|
||||
}
|
||||
|
||||
export async function restorePlatformSession(): Promise<StoredPlatformSession | null> {
|
||||
const stored = await readPlatformSession();
|
||||
if (!stored) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user