This repository has been archived on 2024-10-29. You can view files and clone it, but cannot push or open issues or pull requests.
website/pages/authentication/reset-password.tsx

92 lines
2.8 KiB
TypeScript
Raw Normal View History

2021-12-28 16:06:58 +01:00
import { NextPage } from 'next'
import { useRouter } from 'next/router'
import useTranslation from 'next-translate/useTranslation'
import axios from 'axios'
2022-03-16 12:18:09 +01:00
import { Head } from '../../components/Head'
import { Header } from '../../components/Header'
import { FormState } from '../../components/design/FormState'
import { Main } from '../../components/design/Main'
import { Footer, FooterProps } from '../../components/Footer'
import { Input } from '../../components/design/Input'
import { Button } from '../../components/design/Button'
import { authenticationFromServerSide } from '../../tools/authentication'
import { AuthenticationForm } from '../../components/Authentication'
import { ScrollableBody } from '../../components/ScrollableBody/ScrollableBody'
import { HandleSubmitCallback, useForm } from '../../hooks/useForm'
import { api } from '../../tools/api'
import { userSchema } from '../../models/User'
2021-12-28 16:06:58 +01:00
const ResetPassword: NextPage<FooterProps> = (props) => {
const { t } = useTranslation()
const router = useRouter()
const { version } = props
const { fetchState, message, errors, getErrorTranslation, handleSubmit } =
useForm({
validateSchema: { password: userSchema.password },
resetOnSuccess: true
})
2021-10-26 16:38:55 +02:00
const onSubmit: HandleSubmitCallback = async (formData) => {
try {
await api.put(`/users/reset-password`, {
...formData,
temporaryToken: router.query.temporaryToken
})
await router.push('/authentication/signin')
return null
} catch (error) {
if (axios.isAxiosError(error) && error.response?.status === 400) {
return {
type: 'error',
value: 'errors:invalid'
}
}
2021-10-26 16:38:55 +02:00
return {
type: 'error',
value: 'errors:server-error'
}
}
}
return (
<ScrollableBody>
<Head title={`Thream | ${t('authentication:reset-password')}`} />
<Header />
<Main>
2021-10-26 16:38:55 +02:00
<AuthenticationForm onSubmit={handleSubmit(onSubmit)}>
<Input
type='password'
placeholder='Password'
name='password'
label='Password'
/>
<Button data-cy='submit' className='mt-6 w-full' type='submit'>
{t('authentication:submit')}
</Button>
</AuthenticationForm>
<FormState
id='message'
state={fetchState}
message={
2021-10-26 16:38:55 +02:00
message != null ? message : getErrorTranslation(errors.password)
}
/>
</Main>
<Footer version={version} />
</ScrollableBody>
)
}
export const getServerSideProps = authenticationFromServerSide({
shouldBeAuthenticated: false,
fetchData: async () => {
const { readPackage } = await import('read-pkg')
const { version } = await readPackage()
return { version }
}
})
export default ResetPassword