2022-08-28 18:26:56 +02:00
|
|
|
import { useMemo } from 'react'
|
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'
|
2022-08-28 18:26:56 +02:00
|
|
|
import { useForm } from 'react-component-form'
|
|
|
|
import type { HandleUseFormCallback } from 'react-component-form'
|
2021-10-24 06:09:43 +02:00
|
|
|
|
|
|
|
import { Main } from '../design/Main'
|
|
|
|
import { Input } from '../design/Input'
|
|
|
|
import { Button } from '../design/Button'
|
|
|
|
import { FormState } from '../design/FormState'
|
2022-02-19 23:20:33 +01:00
|
|
|
import { AuthenticationForm } from '.'
|
2021-10-24 06:09:43 +02:00
|
|
|
import { userSchema } from '../../models/User'
|
2022-01-07 21:21:38 +01:00
|
|
|
import { api } from '../../tools/api'
|
2022-08-31 21:44:33 +02:00
|
|
|
import type { Tokens } from '../../tools/authentication'
|
|
|
|
import { Authentication as AuthenticationClass } from '../../tools/authentication'
|
2022-03-16 12:18:09 +01:00
|
|
|
import { AuthenticationSocialMedia } from './AuthenticationSocialMedia'
|
2022-08-28 18:26:56 +02:00
|
|
|
import { useFormTranslation } from '../../hooks/useFormTranslation'
|
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()
|
|
|
|
|
2022-08-28 18:26:56 +02:00
|
|
|
const schema = useMemo(() => {
|
|
|
|
return {
|
|
|
|
...(mode === 'signup' && { name: userSchema.name }),
|
|
|
|
email: userSchema.email,
|
|
|
|
password: userSchema.password
|
|
|
|
}
|
|
|
|
}, [mode])
|
|
|
|
|
|
|
|
const { handleUseForm, errors, fetchState, message } = useForm(schema)
|
|
|
|
const { getFirstErrorTranslation } = useFormTranslation()
|
2021-10-24 06:09:43 +02:00
|
|
|
|
2022-08-28 18:26:56 +02:00
|
|
|
const onSubmit: HandleUseFormCallback<typeof schema> = async (
|
|
|
|
formData,
|
|
|
|
formElement
|
|
|
|
) => {
|
2021-10-26 16:38:55 +02:00
|
|
|
if (mode === 'signup') {
|
|
|
|
try {
|
|
|
|
await api.post(
|
|
|
|
`/users/signup?redirectURI=${window.location.origin}/authentication/signin`,
|
|
|
|
{ ...formData, language: lang, theme }
|
|
|
|
)
|
2022-08-28 18:26:56 +02:00
|
|
|
formElement.reset()
|
2021-10-26 16:38:55 +02:00
|
|
|
return {
|
|
|
|
type: 'success',
|
|
|
|
value: 'authentication:success-signup'
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
if (axios.isAxiosError(error) && error.response?.status === 400) {
|
2022-02-19 23:20:33 +01:00
|
|
|
const message = error.response.data.message as string
|
|
|
|
if (message.endsWith('already taken.')) {
|
|
|
|
return {
|
|
|
|
type: 'error',
|
|
|
|
value: 'authentication:already-used'
|
|
|
|
}
|
|
|
|
}
|
2021-10-26 16:38:55 +02:00
|
|
|
return {
|
|
|
|
type: 'error',
|
2022-02-19 23:20:33 +01:00
|
|
|
value: 'errors:server-error'
|
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)
|
2022-08-24 17:22:55 +02:00
|
|
|
const authentication = new AuthenticationClass(data, true)
|
2021-10-26 16:38:55 +02:00
|
|
|
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>
|
2022-03-16 12:18:09 +01:00
|
|
|
<AuthenticationSocialMedia />
|
2022-02-19 23:20:33 +01:00
|
|
|
<div className='pt-8 text-center font-paragraph text-lg'>
|
2021-10-24 06:09:43 +02:00
|
|
|
{t('authentication:or')}
|
2022-01-07 21:21:38 +01:00
|
|
|
</div>
|
2022-08-28 18:26:56 +02:00
|
|
|
<AuthenticationForm onSubmit={handleUseForm(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')}
|
2022-08-28 18:26:56 +02:00
|
|
|
error={getFirstErrorTranslation(errors.name)}
|
2021-10-24 06:09:43 +02:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
<Input
|
|
|
|
type='email'
|
|
|
|
placeholder='Email'
|
|
|
|
name='email'
|
|
|
|
label='Email'
|
2022-08-28 18:26:56 +02:00
|
|
|
error={getFirstErrorTranslation(errors.email)}
|
2021-10-24 06:09:43 +02:00
|
|
|
/>
|
|
|
|
<Input
|
|
|
|
type='password'
|
|
|
|
placeholder={t('authentication:password')}
|
|
|
|
name='password'
|
|
|
|
label={t('authentication:password')}
|
|
|
|
showForgotPassword={mode === 'signin'}
|
2022-08-28 18:26:56 +02:00
|
|
|
error={getFirstErrorTranslation(errors.password)}
|
2021-10-24 06:09:43 +02:00
|
|
|
/>
|
2022-02-19 23:20:33 +01:00
|
|
|
<Button data-cy='submit' className='mt-6 w-full' type='submit'>
|
2021-10-24 06:09:43 +02:00
|
|
|
{t('authentication:submit')}
|
|
|
|
</Button>
|
2022-02-19 23:20:33 +01:00
|
|
|
<p className='mt-3 font-headline text-sm text-green-800 hover:underline dark:text-green-400'>
|
2021-10-24 06:09:43 +02:00
|
|
|
<Link
|
|
|
|
href={
|
|
|
|
mode === 'signup'
|
|
|
|
? '/authentication/signin'
|
|
|
|
: '/authentication/signup'
|
|
|
|
}
|
|
|
|
>
|
2022-12-13 11:38:07 +01:00
|
|
|
{mode === 'signup'
|
|
|
|
? t('authentication:already-have-an-account')
|
|
|
|
: t('authentication:dont-have-an-account')}
|
2021-10-24 06:09:43 +02:00
|
|
|
</Link>
|
|
|
|
</p>
|
|
|
|
</AuthenticationForm>
|
2022-08-28 18:26:56 +02:00
|
|
|
<FormState
|
|
|
|
id='message'
|
|
|
|
state={fetchState}
|
|
|
|
message={message != null ? t(message) : undefined}
|
|
|
|
/>
|
2021-10-24 06:09:43 +02:00
|
|
|
</Main>
|
|
|
|
)
|
|
|
|
}
|