| import { base } from '$app/paths'; |
| import { error } from '@sveltejs/kit'; |
| import { browser } from '$app/environment'; |
| import { config } from '$lib/stores/settings.svelte'; |
|
|
| |
| |
| |
| |
| export async function validateApiKey(fetch: typeof globalThis.fetch): Promise<void> { |
| if (!browser) { |
| return; |
| } |
|
|
| try { |
| const apiKey = config().apiKey; |
|
|
| const headers: Record<string, string> = { |
| 'Content-Type': 'application/json' |
| }; |
|
|
| if (apiKey) { |
| headers.Authorization = `Bearer ${apiKey}`; |
| } |
|
|
| const response = await fetch(`${base}/props`, { headers }); |
|
|
| if (!response.ok) { |
| if (response.status === 401 || response.status === 403) { |
| throw error(401, 'Access denied'); |
| } |
|
|
| console.warn(`Server responded with status ${response.status} during API key validation`); |
| return; |
| } |
| } catch (err) { |
| |
| if (err && typeof err === 'object' && 'status' in err) { |
| throw err; |
| } |
|
|
| |
| console.warn('Cannot connect to server for API key validation:', err); |
| } |
| } |
|
|