'use client'; import { Button } from '@/components/ui/Button'; import { Input } from '@/components/ui/Input'; import { ArrowRight, ShieldCheck } from 'lucide-react'; import Link from 'next/link'; import { useRouter } from 'next/navigation'; import { useState } from 'react'; export function AdminSetup({ databaseReady = true, initialEmail = '', }: { databaseReady?: boolean; initialEmail?: string; }) { const router = useRouter(); const [email, setEmail] = useState(initialEmail); const [password, setPassword] = useState(''); const [confirmPassword, setConfirmPassword] = useState(''); const [setupKey, setSetupKey] = useState(''); const [isSubmitting, setIsSubmitting] = useState(false); const [errorMessage, setErrorMessage] = useState( databaseReady ? '' : 'The setup service is unavailable. Check the database connection and try again.', ); const submit = async (event: React.FormEvent) => { event.preventDefault(); setErrorMessage(''); if (password.length < 12) { setErrorMessage('Choose a password with at least 12 characters.'); return; } if (password !== confirmPassword) { setErrorMessage('The passwords do not match.'); return; } setIsSubmitting(true); try { const response = await fetch('/api/auth/setup', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email, password, setupKey }), }); const body = await response.json(); if (!response.ok) throw new Error(body.error || 'Admin setup failed.'); router.replace('/vcon/dashboard'); router.refresh(); } catch (submitError) { setErrorMessage( submitError instanceof Error ? submitError.message : 'Admin setup failed.', ); setIsSubmitting(false); } }; return (
{errorMessage && (
{errorMessage}
)} setEmail(event.target.value)} compact required /> setPassword(event.target.value)} compact minLength={12} required /> setConfirmPassword(event.target.value)} compact minLength={12} required /> setSetupKey(event.target.value)} compact required />
); }