import React, { useState, useEffect } from 'react'; import { useForm } from 'react-hook-form'; import { useNavigate } from 'react-router-dom'; import { useRegisterUserMutation, useGetStartupConfig } from 'librechat-data-provider/react-query'; import type { TRegisterUser } from 'librechat-data-provider'; import { GoogleIcon, FacebookIcon, OpenIDIcon, GithubIcon, DiscordIcon } from '~/components'; import { useLocalize } from '~/hooks'; import SocialButton from './SocialButton'; const Registration: React.FC = () => { const navigate = useNavigate(); const { data: startupConfig } = useGetStartupConfig(); const localize = useLocalize(); const { register, watch, handleSubmit, formState: { errors }, } = useForm({ mode: 'onChange' }); const [error, setError] = useState(false); const [errorMessage, setErrorMessage] = useState(''); const registerUser = useRegisterUserMutation(); const password = watch('password'); const onRegisterUserFormSubmit = async (data: TRegisterUser) => { try { await registerUser.mutateAsync(data); navigate('/c/new'); } catch (error) { setError(true); //@ts-ignore - error is of type unknown if (error.response?.data?.message) { //@ts-ignore - error is of type unknown setErrorMessage(error.response?.data?.message); } } }; useEffect(() => { if (startupConfig?.registrationEnabled === false) { navigate('/login'); } }, [startupConfig, navigate]); if (!startupConfig) { return null; } const socialLogins = startupConfig.socialLogins ?? []; const renderInput = (id: string, label: string, type: string, validation: object) => (
{errors[id] && ( {String(errors[id]?.message) ?? ''} )}
); const providerComponents = { discord: ( ), facebook: ( ), github: ( ), google: ( ), openid: ( startupConfig.openidImageUrl ? ( OpenID Logo ) : ( ) } label={startupConfig.openidLabel} id="openid" /> ), }; return (

{localize('com_auth_create_account')}

{error && (
{localize('com_auth_error_create')} {errorMessage}
)}
{renderInput('name', 'com_auth_full_name', 'text', { required: localize('com_auth_name_required'), minLength: { value: 3, message: localize('com_auth_name_min_length'), }, maxLength: { value: 80, message: localize('com_auth_name_max_length'), }, })} {renderInput('username', 'com_auth_username', 'text', { minLength: { value: 2, message: localize('com_auth_username_min_length'), }, maxLength: { value: 80, message: localize('com_auth_username_max_length'), }, })} {renderInput('email', 'com_auth_email', 'email', { required: localize('com_auth_email_required'), minLength: { value: 3, message: localize('com_auth_email_min_length'), }, maxLength: { value: 120, message: localize('com_auth_email_max_length'), }, pattern: { value: /\S+@\S+\.\S+/, message: localize('com_auth_email_pattern'), }, })} {renderInput('password', 'com_auth_password', 'password', { required: localize('com_auth_password_required'), minLength: { value: 8, message: localize('com_auth_password_min_length'), }, maxLength: { value: 128, message: localize('com_auth_password_max_length'), }, })} {renderInput('confirm_password', 'com_auth_password_confirm', 'password', { validate: (value) => value === password || localize('com_auth_password_not_match'), })}

{localize('com_auth_already_have_account')}{' '} {localize('com_auth_login')}

{startupConfig.socialLoginEnabled && ( <> {startupConfig.emailLoginEnabled && ( <>
Or
)}
{socialLogins.map((provider) => providerComponents[provider] || null)}
)}
); }; export default Registration;