import { useForm } from 'react-hook-form'; import React, { useState, useEffect } from 'react'; import { useGetStartupConfig } from 'librechat-data-provider/react-query'; import type { TLoginUser, TStartupConfig } from 'librechat-data-provider'; import type { TAuthContext } from '~/common'; import { useResendVerificationEmail } from '~/data-provider'; import { useLocalize } from '~/hooks'; type TLoginFormProps = { onSubmit: (data: TLoginUser) => void; startupConfig: TStartupConfig; error: Pick['error']; setError: Pick['setError']; }; const LoginForm: React.FC = ({ onSubmit, startupConfig, error, setError }) => { const localize = useLocalize(); const { register, getValues, handleSubmit, formState: { errors }, } = useForm(); const [showResendLink, setShowResendLink] = useState(false); const { data: config } = useGetStartupConfig(); const useUsernameLogin = config?.ldap?.username; useEffect(() => { if (error && error.includes('422') && !showResendLink) { setShowResendLink(true); } }, [error, showResendLink]); const resendLinkMutation = useResendVerificationEmail({ onMutate: () => { setError(undefined); setShowResendLink(false); }, }); if (!startupConfig) { return null; } const renderError = (fieldName: string) => { const errorMessage = errors[fieldName]?.message; return errorMessage ? ( {String(errorMessage)} ) : null; }; const handleResendEmail = () => { const email = getValues('email'); if (!email) { return setShowResendLink(false); } resendLinkMutation.mutate({ email }); }; return ( <> {showResendLink && (
{localize('com_auth_email_verification_resend_prompt')}
)}
onSubmit(data))} >
{renderError('email')}
{renderError('password')}
{startupConfig.passwordResetEnabled && ( {localize('com_auth_password_forgot')} )}
); }; export default LoginForm;