import { useRouter } from 'next/router' import Link from 'next/link' import useTranslation from 'next-translate/useTranslation' import { useTheme } from 'next-themes' import axios from 'axios' import { Main } from '../design/Main' import { Input } from '../design/Input' import { Button } from '../design/Button' import { FormState } from '../design/FormState' import { AuthenticationForm } from '.' import { userSchema } from '../../models/User' import { api } from '../../tools/api' import { Tokens, Authentication as AuthenticationClass } from '../../tools/authentication' import { useForm, HandleSubmitCallback } from '../../hooks/useForm' import { AuthenticationSocialMedia } from './AuthenticationSocialMedia' export interface AuthenticationProps { mode: 'signup' | 'signin' } export const Authentication: React.FC = (props) => { const { mode } = props const router = useRouter() const { lang, t } = useTranslation() const { theme } = useTheme() const { errors, fetchState, message, getErrorTranslation, handleSubmit } = useForm({ validateSchema: { ...(mode === 'signup' && { name: userSchema.name }), email: userSchema.email, password: userSchema.password }, resetOnSuccess: true }) const onSubmit: HandleSubmitCallback = async (formData) => { if (mode === 'signup') { try { await api.post( `/users/signup?redirectURI=${window.location.origin}/authentication/signin`, { ...formData, language: lang, theme } ) return { type: 'success', value: 'authentication:success-signup' } } catch (error) { if (axios.isAxiosError(error) && error.response?.status === 400) { const message = error.response.data.message as string if (message.endsWith('already taken.')) { return { type: 'error', value: 'authentication:already-used' } } return { type: 'error', value: 'errors:server-error' } } return { type: 'error', value: 'errors:server-error' } } } else { try { const { data } = await api.post('/users/signin', formData) const authentication = new AuthenticationClass(data) authentication.signin() await router.push('/application') return null } catch (error) { if (axios.isAxiosError(error) && error.response?.status === 400) { return { type: 'error', value: 'authentication:wrong-credentials' } } return { type: 'error', value: 'errors:server-error' } } } } return (
{t('authentication:or')}
{mode === 'signup' && ( )}

{mode === 'signup' ? t('authentication:already-have-an-account') : t('authentication:dont-have-an-account')}

) }