2021-10-24 06:09:43 +02:00
|
|
|
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 { SocialMediaButton } from '../design/SocialMediaButton'
|
|
|
|
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'
|
2022-01-01 20:42:25 +01:00
|
|
|
import { api } from 'tools/api'
|
2021-10-24 06:09:43 +02:00
|
|
|
import {
|
|
|
|
Tokens,
|
|
|
|
Authentication as AuthenticationClass
|
2022-01-01 20:42:25 +01:00
|
|
|
} from '../../tools/authentication'
|
2021-10-26 16:38:55 +02:00
|
|
|
import { useForm, HandleSubmitCallback } from '../../hooks/useForm'
|
2021-10-24 06:09:43 +02:00
|
|
|
|
|
|
|
export interface AuthenticationProps {
|
|
|
|
mode: 'signup' | 'signin'
|
|
|
|
}
|
|
|
|
|
|
|
|
export const Authentication: React.FC<AuthenticationProps> = (props) => {
|
|
|
|
const { mode } = props
|
|
|
|
|
|
|
|
const router = useRouter()
|
|
|
|
const { lang, t } = useTranslation()
|
|
|
|
const { theme } = useTheme()
|
|
|
|
|
2021-11-13 21:50:34 +01:00
|
|
|
const { errors, fetchState, message, getErrorTranslation, handleSubmit } =
|
2021-10-26 16:38:55 +02:00
|
|
|
useForm({
|
|
|
|
validateSchemaObject: {
|
|
|
|
...(mode === 'signup' && { name: userSchema.name }),
|
|
|
|
email: userSchema.email,
|
|
|
|
password: userSchema.password
|
|
|
|
}
|
2021-10-24 06:09:43 +02:00
|
|
|
})
|
|
|
|
|
2021-10-26 16:38:55 +02:00
|
|
|
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) {
|
|
|
|
return {
|
|
|
|
type: 'error',
|
|
|
|
value: 'authentication:alreadyUsed'
|
2021-10-24 06:09:43 +02:00
|
|
|
}
|
|
|
|
}
|
2021-10-26 16:38:55 +02:00
|
|
|
return {
|
|
|
|
type: 'error',
|
|
|
|
value: 'errors:server-error'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
const { data } = await api.post<Tokens>('/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'
|
2021-10-24 06:09:43 +02:00
|
|
|
}
|
|
|
|
}
|
2021-10-26 16:38:55 +02:00
|
|
|
return {
|
|
|
|
type: 'error',
|
|
|
|
value: 'errors:server-error'
|
|
|
|
}
|
2021-10-24 06:09:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Main>
|
|
|
|
<section className='flex flex-col sm:items-center sm:w-full'>
|
|
|
|
<div className='flex flex-col items-center justify-center space-y-6 sm:w-4/6 sm:flex-row sm:space-x-6 sm:space-y-0'>
|
|
|
|
<SocialMediaButton socialMedia='Google' />
|
|
|
|
<SocialMediaButton socialMedia='GitHub' />
|
|
|
|
<SocialMediaButton socialMedia='Discord' />
|
|
|
|
</div>
|
|
|
|
</section>
|
|
|
|
<section className='text-center text-lg font-paragraph pt-8'>
|
|
|
|
{t('authentication:or')}
|
|
|
|
</section>
|
2021-10-26 16:38:55 +02:00
|
|
|
<AuthenticationForm onSubmit={handleSubmit(onSubmit)}>
|
2021-10-24 06:09:43 +02:00
|
|
|
{mode === 'signup' && (
|
|
|
|
<Input
|
|
|
|
type='text'
|
2021-10-26 16:38:55 +02:00
|
|
|
placeholder={t('common:name')}
|
2021-10-24 06:09:43 +02:00
|
|
|
name='name'
|
2021-10-26 16:38:55 +02:00
|
|
|
label={t('common:name')}
|
2021-10-24 06:09:43 +02:00
|
|
|
error={getErrorTranslation(errors.name)}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
<Input
|
|
|
|
type='email'
|
|
|
|
placeholder='Email'
|
|
|
|
name='email'
|
|
|
|
label='Email'
|
|
|
|
error={getErrorTranslation(errors.email)}
|
|
|
|
/>
|
|
|
|
<Input
|
|
|
|
type='password'
|
|
|
|
placeholder={t('authentication:password')}
|
|
|
|
name='password'
|
|
|
|
label={t('authentication:password')}
|
|
|
|
showForgotPassword={mode === 'signin'}
|
|
|
|
error={getErrorTranslation(errors.password)}
|
|
|
|
/>
|
|
|
|
<Button data-cy='submit' className='w-full mt-6' type='submit'>
|
|
|
|
{t('authentication:submit')}
|
|
|
|
</Button>
|
|
|
|
<p className='mt-3 font-headline text-sm text-green-800 dark:text-green-400 hover:underline'>
|
|
|
|
<Link
|
|
|
|
href={
|
|
|
|
mode === 'signup'
|
|
|
|
? '/authentication/signin'
|
|
|
|
: '/authentication/signup'
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<a>
|
|
|
|
{mode === 'signup'
|
|
|
|
? t('authentication:already-have-an-account')
|
|
|
|
: t('authentication:dont-have-an-account')}
|
|
|
|
</a>
|
|
|
|
</Link>
|
|
|
|
</p>
|
|
|
|
</AuthenticationForm>
|
2021-11-13 21:50:34 +01:00
|
|
|
<FormState id='message' state={fetchState} message={message} />
|
2021-10-24 06:09:43 +02:00
|
|
|
</Main>
|
|
|
|
)
|
|
|
|
}
|