From 7b9b024f598a72c1990476ee3474c096f1c34068 Mon Sep 17 00:00:00 2001 From: Snazzah Date: Sat, 22 Jun 2024 00:18:17 -0500 Subject: [PATCH] add auth token checking --- client/index.ts | 2 +- server/.env.example | 4 ++++ server/index.ts | 3 +-- 3 files changed, 6 insertions(+), 3 deletions(-) create mode 100644 server/.env.example diff --git a/client/index.ts b/client/index.ts index 460929b..f688b80 100644 --- a/client/index.ts +++ b/client/index.ts @@ -66,7 +66,7 @@ async function _upload() { if (!reader.getTableNames().includes('tblContacts')) return console.log('!! No "tblContacts" table present.'); - const table = reader.getTable("tblContacts"); + const table = reader.getTable('tblContacts'); const tableData = table.getData>(); const data = tableData.reduce((p, r) => ({ ...p, [r.fldPrimaryKey]: r }), {} as Record); diff --git a/server/.env.example b/server/.env.example new file mode 100644 index 0000000..20755ce --- /dev/null +++ b/server/.env.example @@ -0,0 +1,4 @@ +PORT=9992 +HOST=0.0.0.0 +DATABASE_URL="postgresql://user:pass@localhost:5432/fdlogup?schema=public" +AUTH_TOKEN="" # https://generate-secret.vercel.app/128 diff --git a/server/index.ts b/server/index.ts index 7181423..4a06fbe 100644 --- a/server/index.ts +++ b/server/index.ts @@ -16,13 +16,12 @@ const server = Bun.serve({ const path = new URL(req.url).pathname; console.log('-> Recieved logs, parsing...'); if (req.method !== 'POST') new Response('Invalid method', { status: 405 }); + if (process.env.AUTH_TOKEN && req.headers.get('Authorization') === process.env.AUTH_TOKEN) new Response('Unauthorized', { status: 401 }); if (req.headers.get('Content-Type') !== 'application/json') new Response('Invalid content type', { status: 404 }); try { const newRows: any[] = await req.json(); - const query = `INSERT INTO contacts (${fields.join(', ')}) VALUES (${fields.map((_, i) => `$${i + 1}`).join(', ')}) ON CONFLICT (id) DO UPDATE SET ${fields.map((f, i) => `${f} = $${i + 1}`).slice(1).join(', ')}` - console.log(query); for (const row of newRows) { const data: Record = {