From f1b504a70110b6911ea7dc67680d2baecd4bba67 Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Tue, 11 Feb 2025 00:09:29 +0000 Subject: [PATCH] fix: add API key header and improve form error handling Co-Authored-By: G --- .env.example | 1 + components/ConsultationForm.tsx | 12 ++++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/.env.example b/.env.example index 4d99446..c44afbf 100644 --- a/.env.example +++ b/.env.example @@ -3,6 +3,7 @@ MONGODB_URI=mongodb://localhost:27017/botsmann # API key for authentication API_KEY=your-secure-api-key-here +NEXT_PUBLIC_API_KEY=your-secure-api-key-here # Email Configuration SENDGRID_API_KEY=your_sendgrid_api_key diff --git a/components/ConsultationForm.tsx b/components/ConsultationForm.tsx index e19ba43..03c6618 100644 --- a/components/ConsultationForm.tsx +++ b/components/ConsultationForm.tsx @@ -23,18 +23,22 @@ export default function ConsultationForm() { const response = await fetch('/api/consultations', { method: 'POST', - headers: { 'Content-Type': 'application/json' }, + headers: { + 'Content-Type': 'application/json', + 'x-api-key': process.env.NEXT_PUBLIC_API_KEY || '' + }, body: JSON.stringify(data), }); if (!response.ok) { - throw new Error('Submission failed'); + const errorData = await response.json(); + throw new Error(errorData.error || 'Submission failed'); } reset(); setSubmitSuccess(true); - } catch (error) { - setSubmitError('Failed to submit form. Please try again.'); + } catch (error: any) { + setSubmitError(error.message || 'Failed to submit form. Please try again.'); } finally { setIsSubmitting(false); }