import env from '@beam-australia/react-env'; import { joiResolver } from '@hookform/resolvers/joi'; import { Google, Login, Visibility, VisibilityOff } from '@mui/icons-material'; import { Button, IconButton, InputAdornment, TextField } from '@mui/material'; import Joi from 'joi'; import { Trans, useTranslation } from 'next-i18next'; import { useMemo, useState } from 'react'; import { GoogleLoginResponse, GoogleLoginResponseOffline, useGoogleLogin } from 'react-google-login'; import { Controller, useForm } from 'react-hook-form'; import toast from 'react-hot-toast'; import { useIsMutating, useMutation } from 'react-query'; import BaseModal from '@/components/shared/BaseModal'; import { login, LoginParams, loginWithGoogle, LoginWithGoogleParams } from '@/services/auth'; import { ServerError } from '@/services/axios'; import { useAppDispatch, useAppSelector } from '@/store/hooks'; import { setModalState } from '@/store/modal/modalSlice'; type FormData = { identifier: string; password: string; }; const defaultState: FormData = { identifier: '', password: '', }; const schema = Joi.object({ identifier: Joi.string().required(), password: Joi.string().min(6).required(), }); const LoginModal: React.FC = () => { const { t } = useTranslation(); const dispatch = useAppDispatch(); const [showPassword, setShowPassword] = useState(false); const isMutating = useIsMutating(); const isLoading = useMemo(() => isMutating > 0, [isMutating]); const { open: isOpen } = useAppSelector((state) => state.modal['auth.login']); const { reset, control, handleSubmit } = useForm({ defaultValues: defaultState, resolver: joiResolver(schema), }); const { mutateAsync: loginMutation } = useMutation(login); const { mutateAsync: loginWithGoogleMutation } = useMutation( loginWithGoogle ); const { signIn } = useGoogleLogin({ clientId: env('GOOGLE_CLIENT_ID'), onSuccess: async (response: GoogleLoginResponse | GoogleLoginResponseOffline) => { await loginWithGoogleMutation({ accessToken: (response as GoogleLoginResponse).accessToken }); handleClose(); }, }); const handleClose = () => { dispatch(setModalState({ modal: 'auth.login', state: { open: false } })); reset(); }; const onSubmit = async ({ identifier, password }: FormData) => { await loginMutation( { identifier, password }, { onError: (error) => { toast.error(error.message); }, } ); handleClose(); }; const handleCreateAccount = () => { handleClose(); dispatch(setModalState({ modal: 'auth.register', state: { open: true } })); }; const handleRecoverAccount = () => { handleClose(); dispatch(setModalState({ modal: 'auth.forgot', state: { open: true } })); }; const handleLoginWithGoogle = () => { signIn(); }; const PasswordVisibility = (): React.ReactElement => { const handleToggle = () => setShowPassword((showPassword) => !showPassword); return ( {showPassword ? : } ); }; return ( } isOpen={isOpen} heading={t('modals.auth.login.heading')} handleClose={handleClose} footerChildren={
} >

{t('modals.auth.login.body')}

( )} /> ( }} {...field} /> )} />

If you don't have one, you can create an account here.

In case you have forgotten your password, you can recover your account here.

); }; export default LoginModal;