Spaces:
Sleeping
Sleeping
File size: 1,839 Bytes
fd92eae 4282a40 4ae79ba 4282a40 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | const AUTH_TOKEN_KEY = 'authToken';
const USER_KEY = 'user';
export function readStoredAuth() {
const token = localStorage.getItem(AUTH_TOKEN_KEY);
const userData = localStorage.getItem(USER_KEY);
if (!token || !userData) {
return null;
}
try {
return { token, user: JSON.parse(userData) };
} catch {
clearStoredAuth();
return null;
}
}
export function persistAuth(user, token) {
localStorage.setItem(AUTH_TOKEN_KEY, token);
localStorage.setItem(USER_KEY, JSON.stringify(user));
}
export function clearStoredAuth() {
localStorage.removeItem(AUTH_TOKEN_KEY);
localStorage.removeItem(USER_KEY);
}
export function getApiBaseUrl() {
return process.env.REACT_APP_API_URL || '';
}
export async function fetchStorageStatus() {
try {
const response = await fetch(`${getApiBaseUrl()}/api/health`);
if (!response.ok) return null;
const data = await response.json();
return data.storage || null;
} catch {
return null;
}
}
/** FastAPI may return detail as a string or a list of validation objects. */
export function formatApiDetail(detail, fallback = 'Something went wrong. Please try again.') {
if (detail == null || detail === '') return fallback;
if (typeof detail === 'string') return detail;
if (Array.isArray(detail)) {
const parts = detail.map((item) => {
if (typeof item === 'string') return item;
if (item && typeof item.msg === 'string') {
const loc = Array.isArray(item.loc)
? item.loc.filter((x) => x !== 'body' && x !== 'query').join(' ')
: '';
return loc ? `${loc}: ${item.msg}` : item.msg;
}
return null;
}).filter(Boolean);
return parts.join(' ') || fallback;
}
if (typeof detail === 'object' && typeof detail.msg === 'string') {
return detail.msg;
}
return fallback;
}
|