Files
ersatztv/web/src/api/auth.ts
T
2026-07-02 18:36:56 +02:00

34 lines
711 B
TypeScript

const apiKeyStorageKey = 'ctv-api-key';
function getStorage(): Storage | undefined {
if (typeof window === 'undefined') {
return undefined;
}
try {
return window.localStorage;
} catch {
return undefined;
}
}
export function getStoredApiKey(): string | null {
const value = getStorage()?.getItem(apiKeyStorageKey);
return value && value.trim().length > 0 ? value : null;
}
export function setStoredApiKey(apiKey: string): void {
const trimmed = apiKey.trim();
if (trimmed.length === 0) {
clearStoredApiKey();
return;
}
getStorage()?.setItem(apiKeyStorageKey, trimmed);
}
export function clearStoredApiKey(): void {
getStorage()?.removeItem(apiKeyStorageKey);
}