Skip to content

Commit

Permalink
feat: secure the update route by api token
Browse files Browse the repository at this point in the history
  • Loading branch information
Mokto committed May 11, 2024
1 parent d455dbd commit 1a1c6f3
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/routes/api/openapi/+server.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import type { RequestHandler } from './$types';
import { json } from '@sveltejs/kit';
import { prepareDatabase } from '$lib/utils/db';
import { error } from '@sveltejs/kit';
import { env } from '$env/dynamic/private';

export const PUT: RequestHandler = async ({ request }) => {
const data = await request.json();
export const PUT: RequestHandler = async ({ request, url }) => {
if (!env.API_TOKEN) {
error(
403,
'API_TOKEN must be defined in the environment variables before using this endpoint.'
);
}
if (url.searchParams.get('apiToken') !== env.API_TOKEN) {
error(403, 'Invalid API token.');
}

const data = await request.json();
await prepareDatabase(data);

return json({});
Expand Down

0 comments on commit 1a1c6f3

Please sign in to comment.