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); }