- Change /api/v1/license from GET with query params to POST with JSON body - Request body uses license_key and email_address - Response returns key, email_address, subscription_level - Add active check (reject deactivated license keys) - Exclude /api/v1/automations/ from API key auth (GHL webhook) - Add copyableFields prop to AdminTable with clipboard icon - Enable copy button on license key field in license-keys admin page
42 lines
1.1 KiB
TypeScript
42 lines
1.1 KiB
TypeScript
import { auth } from '$lib/auth';
|
|
import { svelteKitHandler } from 'better-auth/svelte-kit';
|
|
import { building } from '$app/environment';
|
|
import { validateApiKey } from '$lib/server/api-key';
|
|
|
|
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' }
|
|
});
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
const session = await auth.api.getSession({
|
|
headers: event.request.headers
|
|
});
|
|
|
|
if (session) {
|
|
event.locals.session = session.session;
|
|
event.locals.user = session.user;
|
|
}
|
|
|
|
return svelteKitHandler({ event, resolve, auth, building });
|
|
}
|