- Extract handleOAuthCallback to eliminate GET/POST duplication in oauth.ts - Add P2002 race condition handling in findOrCreateUser - Add .unref() to stateStore cleanup timer to not block process exit - Use Provider union type instead of bare strings throughout OAuth code - Export API_BASE from api.ts, reuse in OAuthButtons - Extract MobileBranding component to deduplicate Login/Register mobile brand - Extract shared Logo component in AuthBranding - Remove unnecessary WHAT comments Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
81 lines
2.2 KiB
TypeScript
81 lines
2.2 KiB
TypeScript
export const API_BASE = '/api';
|
|
|
|
type ApiResponse<T> = {
|
|
success: boolean;
|
|
data?: T;
|
|
error?: { code: string; message: string };
|
|
};
|
|
|
|
let accessToken: string | null = localStorage.getItem('accessToken');
|
|
let refreshToken: string | null = localStorage.getItem('refreshToken');
|
|
|
|
export function setTokens(access: string, refresh: string) {
|
|
accessToken = access;
|
|
refreshToken = refresh;
|
|
localStorage.setItem('accessToken', access);
|
|
localStorage.setItem('refreshToken', refresh);
|
|
}
|
|
|
|
export function clearTokens() {
|
|
accessToken = null;
|
|
refreshToken = null;
|
|
localStorage.removeItem('accessToken');
|
|
localStorage.removeItem('refreshToken');
|
|
}
|
|
|
|
export function getAccessToken() {
|
|
return accessToken;
|
|
}
|
|
|
|
async function refreshAccessToken(): Promise<boolean> {
|
|
if (!refreshToken) return false;
|
|
try {
|
|
const res = await fetch(`${API_BASE}/auth/refresh`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ refreshToken }),
|
|
});
|
|
if (!res.ok) return false;
|
|
const json: ApiResponse<{ accessToken: string; refreshToken: string }> = await res.json();
|
|
if (json.success && json.data) {
|
|
setTokens(json.data.accessToken, json.data.refreshToken);
|
|
return true;
|
|
}
|
|
return false;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export async function apiFetch<T>(path: string, options: RequestInit = {}): Promise<T> {
|
|
const headers = new Headers(options.headers);
|
|
if (!headers.has('Content-Type') && !(options.body instanceof FormData)) {
|
|
headers.set('Content-Type', 'application/json');
|
|
}
|
|
if (accessToken) {
|
|
headers.set('Authorization', `Bearer ${accessToken}`);
|
|
}
|
|
|
|
let res = await fetch(`${API_BASE}${path}`, { ...options, headers });
|
|
|
|
if (res.status === 401 && refreshToken) {
|
|
const refreshed = await refreshAccessToken();
|
|
if (refreshed) {
|
|
headers.set('Authorization', `Bearer ${accessToken}`);
|
|
res = await fetch(`${API_BASE}${path}`, { ...options, headers });
|
|
}
|
|
}
|
|
|
|
const text = await res.text();
|
|
let json: ApiResponse<T>;
|
|
try {
|
|
json = JSON.parse(text);
|
|
} catch {
|
|
throw new Error(`Server error (${res.status})`);
|
|
}
|
|
if (!json.success) {
|
|
throw new Error(json.error?.message || 'Request failed');
|
|
}
|
|
return json.data as T;
|
|
}
|