diff --git a/src/hooks.server.ts b/src/hooks.server.ts index bde0b72..6105ad5 100644 --- a/src/hooks.server.ts +++ b/src/hooks.server.ts @@ -2,27 +2,30 @@ import { auth } from '$lib/auth'; import { svelteKitHandler } from 'better-auth/svelte-kit'; import { building } from '$app/environment'; import { validateApiKey } from '$lib/server/api-key'; +import { env } from '$env/dynamic/private'; export async function handle({ event, resolve }) { if ( event.url.pathname.startsWith('/api/v1/') && !event.url.pathname.startsWith('/api/v1/automations/') ) { - const authHeader = event.request.headers.get('authorization'); - if (!authHeader?.startsWith('Bearer ')) { - return new Response(JSON.stringify({ error: 'API key required' }), { - status: 401, - headers: { 'content-type': 'application/json' } - }); - } + if (env.API_KEYS_ACTIVE !== 'false') { + const authHeader = event.request.headers.get('authorization'); + if (!authHeader?.startsWith('Bearer ')) { + return new Response(JSON.stringify({ error: 'API key required' }), { + status: 401, + headers: { 'content-type': 'application/json' } + }); + } - const key = authHeader.slice(7); - const record = await validateApiKey(key); - if (!record) { - return new Response(JSON.stringify({ error: 'Invalid or expired API key' }), { - status: 401, - headers: { 'content-type': 'application/json' } - }); + const key = authHeader.slice(7); + const record = await validateApiKey(key); + if (!record) { + return new Response(JSON.stringify({ error: 'Invalid or expired API key' }), { + status: 401, + headers: { 'content-type': 'application/json' } + }); + } } return resolve(event);