import { useMemo } from 'react' import * as z from 'zod' import { useForm } from 'react-hook-form' import { zodResolver } from '@hookform/resolvers/zod' import { useTranslation } from 'react-i18next' import { Button } from '@/components/ui/button' import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from '@/components/ui/form' import { Switch } from '@/components/ui/switch' import { Textarea } from '@/components/ui/textarea' import { SettingsSection } from '../components/settings-section' import { useResetForm } from '../hooks/use-reset-form' import { useUpdateOption } from '../hooks/use-update-option' const basicAuthSchema = z.object({ PasswordLoginEnabled: z.boolean(), PasswordRegisterEnabled: z.boolean(), EmailVerificationEnabled: z.boolean(), RegisterEnabled: z.boolean(), EmailDomainRestrictionEnabled: z.boolean(), EmailAliasRestrictionEnabled: z.boolean(), EmailDomainWhitelist: z.string(), }) type BasicAuthFormValues = z.infer type BasicAuthSectionProps = { defaultValues: BasicAuthFormValues } export function BasicAuthSection({ defaultValues }: BasicAuthSectionProps) { const { t } = useTranslation() const updateOption = useUpdateOption() const formDefaults = useMemo( () => ({ ...defaultValues, EmailDomainWhitelist: defaultValues.EmailDomainWhitelist.split(',') .map((domain) => domain.trim()) .filter(Boolean) .join('\n'), }), [defaultValues] ) const form = useForm({ resolver: zodResolver(basicAuthSchema), defaultValues: formDefaults, }) useResetForm(form, formDefaults) const onSubmit = async (data: BasicAuthFormValues) => { const updates: Array<{ key: string; value: string | boolean }> = [] Object.entries(data).forEach(([key, value]) => { if (key === 'EmailDomainWhitelist') { if (typeof value !== 'string') return const domains = value .split('\n') .map((domain) => domain.trim()) .filter(Boolean) .join(',') if (domains !== defaultValues.EmailDomainWhitelist) { updates.push({ key, value: domains }) } } else if (value !== defaultValues[key as keyof typeof defaultValues]) { updates.push({ key, value }) } }) for (const update of updates) { await updateOption.mutateAsync(update) } } return (
(
{t('Password Login')} {t('Allow users to log in with password')}
)} /> (
{t('Registration Enabled')} {t('Allow new users to register')}
)} /> (
{t('Password Registration')} {t('Allow registration with password')}
)} /> (
{t('Email Verification')} {t('Require email verification for new accounts')}
)} /> (
{t('Email Domain Restriction')} {t('Only allow specific email domains')}
)} /> (
{t('Email Alias Restriction')} {t('Block email aliases (e.g., user+alias@domain.com)')}
)} /> ( {t('Email Domain Whitelist')}