import { useState } from 'react'; import { useNavigate } from 'react-router-dom'; import { useMutation } from '@tanstack/react-query'; import { useTranslation } from 'react-i18next'; import { api } from '../api/client'; import { useToast } from '../contexts/ToastContext'; import { useTheme } from '../contexts/ThemeContext'; import { useAuth } from '../contexts/AuthContext'; import { Info } from 'lucide-react'; export function SetupPage() { const navigate = useNavigate(); const { t } = useTranslation(); const { showToast } = useToast(); const { mode } = useTheme(); const { refreshAuth } = useAuth(); const [authEnabled, setAuthEnabled] = useState(false); const [adminUsername, setAdminUsername] = useState(''); const [adminPassword, setAdminPassword] = useState(''); const [confirmPassword, setConfirmPassword] = useState(''); const setupMutation = useMutation({ mutationFn: () => api.setupAuth({ auth_enabled: authEnabled, admin_username: authEnabled ? adminUsername : undefined, admin_password: authEnabled ? adminPassword : undefined, }), onSuccess: async (data) => { // Refresh auth status after setup await refreshAuth(); if (data.auth_enabled) { if (data.admin_created) { showToast(t('setup.toast.authEnabledAdminCreated')); navigate('/login'); } else { showToast(t('setup.toast.authEnabledExistingAdmins')); navigate('/login'); } } else { showToast(t('setup.toast.setupCompleted')); navigate('/'); } }, onError: (error: Error) => { showToast(error.message, 'error'); }, }); const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); if (authEnabled) { // Only validate if credentials are provided // If no credentials provided, backend will use existing admin users if they exist if (adminUsername || adminPassword) { if (!adminUsername || !adminPassword) { showToast(t('setup.toast.enterBothCredentials'), 'error'); return; } if (adminPassword !== confirmPassword) { showToast(t('setup.toast.passwordsDoNotMatch'), 'error'); return; } if (adminPassword.length < 6) { showToast(t('setup.toast.passwordTooShort'), 'error'); return; } } } setupMutation.mutate(); }; return (
{t('setup.subtitle')}