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 { secureSessionStorage, clearMobileSessionStorage, MOBILE_SESSION_STORAGE_KEY } from '@/lib/secureSessionStorage';
|
||||||
import { mobileRuntime } from '@/lib/runtime';
|
import { createMobilePlatformSdk, mobileRuntime } from '@/lib/runtime';
|
||||||
|
|
||||||
export interface PlatformAuthUser {
|
export interface PlatformAuthUser {
|
||||||
id: string;
|
id: string;
|
||||||
@ -25,6 +25,10 @@ class PlatformAuthError extends Error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function createPlatformAuthSdk(accessToken: string | null = null) {
|
||||||
|
return createMobilePlatformSdk(() => accessToken);
|
||||||
|
}
|
||||||
|
|
||||||
function normalizeUser(value: any): PlatformAuthUser {
|
function normalizeUser(value: any): PlatformAuthUser {
|
||||||
return {
|
return {
|
||||||
id: String(value?.id || value?.sub || '').trim(),
|
id: String(value?.id || value?.sub || '').trim(),
|
||||||
@ -43,13 +47,9 @@ async function platformRequest<T>(
|
|||||||
body?: Record<string, unknown>;
|
body?: Record<string, unknown>;
|
||||||
}
|
}
|
||||||
): Promise<T> {
|
): 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',
|
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,
|
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));
|
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> {
|
export async function readPlatformSession(): Promise<StoredPlatformSession | null> {
|
||||||
const raw = await secureSessionStorage.getItem(MOBILE_SESSION_STORAGE_KEY);
|
const raw = await secureSessionStorage.getItem(MOBILE_SESSION_STORAGE_KEY);
|
||||||
if (!raw) {
|
if (!raw) {
|
||||||
@ -119,6 +123,20 @@ export async function refreshPlatformSession(refreshToken: string): Promise<Stor
|
|||||||
return session;
|
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> {
|
export async function restorePlatformSession(): Promise<StoredPlatformSession | null> {
|
||||||
const stored = await readPlatformSession();
|
const stored = await readPlatformSession();
|
||||||
if (!stored) {
|
if (!stored) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user