Add API_KEYS_ACTIVE toggle to API key middleware
This commit is contained in:
@@ -2,12 +2,14 @@ import { auth } from '$lib/auth';
|
|||||||
import { svelteKitHandler } from 'better-auth/svelte-kit';
|
import { svelteKitHandler } from 'better-auth/svelte-kit';
|
||||||
import { building } from '$app/environment';
|
import { building } from '$app/environment';
|
||||||
import { validateApiKey } from '$lib/server/api-key';
|
import { validateApiKey } from '$lib/server/api-key';
|
||||||
|
import { env } from '$env/dynamic/private';
|
||||||
|
|
||||||
export async function handle({ event, resolve }) {
|
export async function handle({ event, resolve }) {
|
||||||
if (
|
if (
|
||||||
event.url.pathname.startsWith('/api/v1/') &&
|
event.url.pathname.startsWith('/api/v1/') &&
|
||||||
!event.url.pathname.startsWith('/api/v1/automations/')
|
!event.url.pathname.startsWith('/api/v1/automations/')
|
||||||
) {
|
) {
|
||||||
|
if (env.API_KEYS_ACTIVE !== 'false') {
|
||||||
const authHeader = event.request.headers.get('authorization');
|
const authHeader = event.request.headers.get('authorization');
|
||||||
if (!authHeader?.startsWith('Bearer ')) {
|
if (!authHeader?.startsWith('Bearer ')) {
|
||||||
return new Response(JSON.stringify({ error: 'API key required' }), {
|
return new Response(JSON.stringify({ error: 'API key required' }), {
|
||||||
@@ -24,6 +26,7 @@ export async function handle({ event, resolve }) {
|
|||||||
headers: { 'content-type': 'application/json' }
|
headers: { 'content-type': 'application/json' }
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return resolve(event);
|
return resolve(event);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user