2023-12-13 14:32:57 +03:00
|
|
|
|
'use client';
|
|
|
|
|
|
2025-01-31 21:05:04 +03:00
|
|
|
|
import { zodResolver } from '@hookform/resolvers/zod';
|
2023-09-03 18:26:50 +03:00
|
|
|
|
import axios from 'axios';
|
2023-12-15 17:34:50 +03:00
|
|
|
|
import clsx from 'clsx';
|
2025-01-31 21:05:04 +03:00
|
|
|
|
import { useForm } from 'react-hook-form';
|
2023-07-15 17:46:19 +03:00
|
|
|
|
|
2025-01-27 15:50:37 +03:00
|
|
|
|
import { useConceptNavigation } from '@/app/Navigation/NavigationContext';
|
2024-04-01 21:45:10 +03:00
|
|
|
|
import { urls } from '@/app/urls';
|
2025-01-31 21:05:04 +03:00
|
|
|
|
import { IUserLoginDTO, UserLoginSchema } from '@/backend/auth/api';
|
2025-01-29 16:18:41 +03:00
|
|
|
|
import { useAuthSuspense } from '@/backend/auth/useAuth';
|
2025-01-27 15:03:48 +03:00
|
|
|
|
import { useLogin } from '@/backend/auth/useLogin';
|
2025-01-29 16:18:41 +03:00
|
|
|
|
import ExpectedAnonymous from '@/components/ExpectedAnonymous';
|
2025-01-29 23:18:20 +03:00
|
|
|
|
import { ErrorData } from '@/components/info/InfoError';
|
2025-01-31 21:05:04 +03:00
|
|
|
|
import ErrorField from '@/components/ui/ErrorField';
|
2024-01-04 19:38:12 +03:00
|
|
|
|
import SubmitButton from '@/components/ui/SubmitButton';
|
|
|
|
|
import TextInput from '@/components/ui/TextInput';
|
|
|
|
|
import TextURL from '@/components/ui/TextURL';
|
2023-12-13 14:32:57 +03:00
|
|
|
|
import useQueryStrings from '@/hooks/useQueryStrings';
|
2024-03-19 19:19:08 +03:00
|
|
|
|
import { resources } from '@/utils/constants';
|
2023-12-13 14:32:57 +03:00
|
|
|
|
|
2023-07-15 17:46:19 +03:00
|
|
|
|
function LoginPage() {
|
2023-12-13 14:32:57 +03:00
|
|
|
|
const router = useConceptNavigation();
|
|
|
|
|
const query = useQueryStrings();
|
2025-01-30 21:01:36 +03:00
|
|
|
|
const initialName = query.get('username') ?? '';
|
2023-12-13 14:32:57 +03:00
|
|
|
|
|
2025-01-31 21:05:04 +03:00
|
|
|
|
const {
|
|
|
|
|
register,
|
|
|
|
|
handleSubmit,
|
|
|
|
|
clearErrors,
|
|
|
|
|
setError,
|
|
|
|
|
resetField,
|
|
|
|
|
formState: { errors }
|
|
|
|
|
} = useForm({
|
|
|
|
|
resolver: zodResolver(UserLoginSchema),
|
|
|
|
|
defaultValues: { username: initialName, password: '' }
|
|
|
|
|
});
|
|
|
|
|
|
2025-01-29 16:18:41 +03:00
|
|
|
|
const { isAnonymous } = useAuthSuspense();
|
2025-01-30 21:01:36 +03:00
|
|
|
|
const { login, isPending, error: loginError, reset } = useLogin();
|
|
|
|
|
|
2025-01-31 21:05:04 +03:00
|
|
|
|
function onSubmit(data: IUserLoginDTO) {
|
|
|
|
|
login(
|
|
|
|
|
data,
|
|
|
|
|
() => {
|
|
|
|
|
resetField('password');
|
|
|
|
|
if (router.canBack()) {
|
|
|
|
|
router.back();
|
|
|
|
|
} else {
|
|
|
|
|
router.push(urls.library);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
error => {
|
|
|
|
|
if (axios.isAxiosError(error) && error.response && error.response.status === 400) {
|
|
|
|
|
setError('root', { message: 'На Портале отсутствует такое сочетание имени пользователя и пароля' });
|
|
|
|
|
}
|
2025-01-30 21:01:36 +03:00
|
|
|
|
}
|
2025-01-31 21:05:04 +03:00
|
|
|
|
);
|
2023-07-31 23:47:18 +03:00
|
|
|
|
}
|
2023-07-15 17:46:19 +03:00
|
|
|
|
|
2025-01-30 21:01:36 +03:00
|
|
|
|
function resetErrors() {
|
|
|
|
|
reset();
|
2025-01-31 21:05:04 +03:00
|
|
|
|
clearErrors();
|
2025-01-30 21:01:36 +03:00
|
|
|
|
}
|
|
|
|
|
|
2025-01-28 19:47:24 +03:00
|
|
|
|
if (!isAnonymous) {
|
2023-12-28 14:04:44 +03:00
|
|
|
|
return <ExpectedAnonymous />;
|
2023-09-18 15:25:25 +03:00
|
|
|
|
}
|
2023-07-15 17:46:19 +03:00
|
|
|
|
return (
|
2025-01-31 21:05:04 +03:00
|
|
|
|
<form
|
|
|
|
|
className={clsx('cc-column cc-fade-in', 'w-[24rem] mx-auto', 'pt-12 pb-6 px-6')}
|
|
|
|
|
onSubmit={event => void handleSubmit(onSubmit)(event)}
|
|
|
|
|
onChange={resetErrors}
|
|
|
|
|
>
|
2024-12-12 13:19:12 +03:00
|
|
|
|
<img alt='Концепт Портал' src={resources.logo} className='max-h-[2.5rem] min-w-[2.5rem] mb-3' />
|
|
|
|
|
<TextInput
|
|
|
|
|
id='username'
|
|
|
|
|
autoComplete='username'
|
2025-01-30 21:01:36 +03:00
|
|
|
|
label='Логин или email'
|
2025-01-31 21:05:04 +03:00
|
|
|
|
{...register('username', { required: true })}
|
2024-12-12 13:19:12 +03:00
|
|
|
|
autoFocus
|
|
|
|
|
allowEnter
|
|
|
|
|
spellCheck={false}
|
2025-01-30 21:01:36 +03:00
|
|
|
|
defaultValue={initialName}
|
2025-01-31 21:05:04 +03:00
|
|
|
|
error={errors.username}
|
2024-12-12 13:19:12 +03:00
|
|
|
|
/>
|
|
|
|
|
<TextInput
|
|
|
|
|
id='password'
|
2025-01-31 21:05:04 +03:00
|
|
|
|
{...register('password', { required: true })}
|
2024-12-12 13:19:12 +03:00
|
|
|
|
type='password'
|
|
|
|
|
autoComplete='current-password'
|
2025-01-30 21:01:36 +03:00
|
|
|
|
label='Пароль'
|
2024-12-12 13:19:12 +03:00
|
|
|
|
allowEnter
|
2025-01-31 21:05:04 +03:00
|
|
|
|
error={errors.password}
|
2024-12-12 13:19:12 +03:00
|
|
|
|
/>
|
2023-07-15 17:46:19 +03:00
|
|
|
|
|
2025-01-30 21:01:36 +03:00
|
|
|
|
<SubmitButton text='Войти' className='self-center w-[12rem] mt-3' loading={isPending} />
|
2024-12-12 13:19:12 +03:00
|
|
|
|
<div className='flex flex-col text-sm'>
|
|
|
|
|
<TextURL text='Восстановить пароль...' href='/restore-password' />
|
|
|
|
|
<TextURL text='Нет аккаунта? Зарегистрируйтесь...' href='/signup' />
|
|
|
|
|
</div>
|
2025-01-31 21:05:04 +03:00
|
|
|
|
<ErrorField error={errors.root} />
|
|
|
|
|
<EscalateError error={loginError} />
|
2024-12-12 13:19:12 +03:00
|
|
|
|
</form>
|
2023-12-28 14:04:44 +03:00
|
|
|
|
);
|
2023-07-15 17:46:19 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-28 14:04:44 +03:00
|
|
|
|
export default LoginPage;
|
2024-06-05 12:28:08 +03:00
|
|
|
|
|
|
|
|
|
// ====== Internals =========
|
2025-01-31 21:05:04 +03:00
|
|
|
|
function EscalateError({ error }: { error: ErrorData }): React.ReactElement | null {
|
|
|
|
|
// TODO: rework error escalation mechanism. Probably make it global.
|
|
|
|
|
if (!error) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2024-06-05 12:28:08 +03:00
|
|
|
|
if (axios.isAxiosError(error) && error.response && error.response.status === 400) {
|
2025-01-31 21:05:04 +03:00
|
|
|
|
return null;
|
2024-06-05 12:28:08 +03:00
|
|
|
|
}
|
2025-01-29 23:18:20 +03:00
|
|
|
|
throw error as Error;
|
2024-06-05 12:28:08 +03:00
|
|
|
|
}
|