2024-06-07 20:17:03 +03:00
|
|
|
|
'use client';
|
|
|
|
|
|
2025-02-12 21:36:03 +03:00
|
|
|
|
import { useForm } from 'react-hook-form';
|
2025-01-31 21:04:21 +03:00
|
|
|
|
import { zodResolver } from '@hookform/resolvers/zod';
|
2024-06-07 20:17:03 +03:00
|
|
|
|
|
2025-02-10 01:32:16 +03:00
|
|
|
|
import { urls, useConceptNavigation } from '@/app';
|
2025-02-12 21:36:03 +03:00
|
|
|
|
|
2025-02-10 01:32:16 +03:00
|
|
|
|
import { isAxiosError } from '@/backend/apiTransport';
|
|
|
|
|
import { SubmitButton, TextURL } from '@/components/Control';
|
2025-02-20 20:22:05 +03:00
|
|
|
|
import { type ErrorData } from '@/components/InfoError';
|
2025-02-10 01:32:16 +03:00
|
|
|
|
import { TextInput } from '@/components/Input';
|
2025-02-19 23:29:45 +03:00
|
|
|
|
import { useQueryStrings } from '@/hooks/useQueryStrings';
|
2024-06-07 20:17:03 +03:00
|
|
|
|
import { resources } from '@/utils/constants';
|
|
|
|
|
|
2025-02-20 20:22:05 +03:00
|
|
|
|
import { type IUserLoginDTO, schemaUserLogin } from '../backend/types';
|
2025-02-10 01:32:16 +03:00
|
|
|
|
import { useAuthSuspense } from '../backend/useAuth';
|
|
|
|
|
import { useLogin } from '../backend/useLogin';
|
2025-02-12 13:41:58 +03:00
|
|
|
|
import { ExpectedAnonymous } from '../components/ExpectedAnonymous';
|
2025-02-10 01:32:16 +03:00
|
|
|
|
|
2025-02-19 23:29:45 +03:00
|
|
|
|
export function LoginPage() {
|
2024-06-07 20:17:03 +03:00
|
|
|
|
const router = useConceptNavigation();
|
|
|
|
|
const query = useQueryStrings();
|
2025-01-30 21:00:59 +03:00
|
|
|
|
const initialName = query.get('username') ?? '';
|
2024-06-07 20:17:03 +03:00
|
|
|
|
|
2025-01-31 21:04:21 +03:00
|
|
|
|
const {
|
|
|
|
|
register,
|
|
|
|
|
handleSubmit,
|
|
|
|
|
clearErrors,
|
|
|
|
|
formState: { errors }
|
|
|
|
|
} = useForm({
|
2025-02-10 14:10:51 +03:00
|
|
|
|
resolver: zodResolver(schemaUserLogin),
|
2025-01-31 21:04:21 +03:00
|
|
|
|
defaultValues: { username: initialName, password: '' }
|
|
|
|
|
});
|
|
|
|
|
|
2025-01-29 16:17:50 +03:00
|
|
|
|
const { isAnonymous } = useAuthSuspense();
|
2025-02-03 18:48:29 +03:00
|
|
|
|
const { login, isPending, error: serverError, reset: clearServerError } = useLogin();
|
2025-01-30 21:00:59 +03:00
|
|
|
|
|
2025-01-31 21:04:21 +03:00
|
|
|
|
function onSubmit(data: IUserLoginDTO) {
|
2025-02-11 20:15:34 +03:00
|
|
|
|
return login(data).then(() => {
|
2025-02-03 18:17:07 +03:00
|
|
|
|
if (router.canBack()) {
|
|
|
|
|
router.back();
|
|
|
|
|
} else {
|
2025-02-26 16:28:16 +03:00
|
|
|
|
router.push({ path: urls.library, force: true });
|
2025-01-30 21:00:59 +03:00
|
|
|
|
}
|
2025-02-03 18:17:07 +03:00
|
|
|
|
});
|
2024-06-07 20:17:03 +03:00
|
|
|
|
}
|
|
|
|
|
|
2025-01-30 21:00:59 +03:00
|
|
|
|
function resetErrors() {
|
2025-02-03 18:48:29 +03:00
|
|
|
|
clearServerError();
|
2025-01-31 21:04:21 +03:00
|
|
|
|
clearErrors();
|
2025-01-30 21:00:59 +03:00
|
|
|
|
}
|
|
|
|
|
|
2025-01-28 19:45:31 +03:00
|
|
|
|
if (!isAnonymous) {
|
2024-06-07 20:17:03 +03:00
|
|
|
|
return <ExpectedAnonymous />;
|
|
|
|
|
}
|
|
|
|
|
return (
|
2025-01-31 21:04:21 +03:00
|
|
|
|
<form
|
2025-03-10 16:01:40 +03:00
|
|
|
|
className='cc-column cc-fade-in w-96 mx-auto pt-12 pb-6 px-6'
|
2025-01-31 21:04:21 +03:00
|
|
|
|
onSubmit={event => void handleSubmit(onSubmit)(event)}
|
|
|
|
|
onChange={resetErrors}
|
|
|
|
|
>
|
2025-03-10 16:01:40 +03:00
|
|
|
|
<img alt='Концепт Портал' src={resources.logo} className='max-h-10 min-w-10 mb-3' />
|
2024-12-12 13:17:24 +03:00
|
|
|
|
<TextInput
|
|
|
|
|
id='username'
|
|
|
|
|
autoComplete='username'
|
2025-01-30 21:00:59 +03:00
|
|
|
|
label='Логин или email'
|
2025-02-03 13:13:11 +03:00
|
|
|
|
{...register('username')}
|
2024-12-12 13:17:24 +03:00
|
|
|
|
autoFocus
|
|
|
|
|
allowEnter
|
|
|
|
|
spellCheck={false}
|
2025-01-30 21:00:59 +03:00
|
|
|
|
defaultValue={initialName}
|
2025-01-31 21:04:21 +03:00
|
|
|
|
error={errors.username}
|
2024-12-12 13:17:24 +03:00
|
|
|
|
/>
|
|
|
|
|
<TextInput
|
|
|
|
|
id='password'
|
2025-02-03 13:13:11 +03:00
|
|
|
|
{...register('password')}
|
2024-12-12 13:17:24 +03:00
|
|
|
|
type='password'
|
|
|
|
|
autoComplete='current-password'
|
2025-01-30 21:00:59 +03:00
|
|
|
|
label='Пароль'
|
2024-12-12 13:17:24 +03:00
|
|
|
|
allowEnter
|
2025-01-31 21:04:21 +03:00
|
|
|
|
error={errors.password}
|
2024-12-12 13:17:24 +03:00
|
|
|
|
/>
|
2024-06-07 20:17:03 +03:00
|
|
|
|
|
2025-03-10 16:01:40 +03:00
|
|
|
|
<SubmitButton text='Войти' className='self-center w-48 mt-3' loading={isPending} />
|
2024-12-12 13:17:24 +03:00
|
|
|
|
<div className='flex flex-col text-sm'>
|
|
|
|
|
<TextURL text='Восстановить пароль...' href='/restore-password' />
|
|
|
|
|
<TextURL text='Нет аккаунта? Зарегистрируйтесь...' href='/signup' />
|
|
|
|
|
</div>
|
2025-02-03 18:17:07 +03:00
|
|
|
|
{serverError ? <ServerError error={serverError} /> : null}
|
2024-12-12 13:17:24 +03:00
|
|
|
|
</form>
|
2024-06-07 20:17:03 +03:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ====== Internals =========
|
2025-02-03 18:17:07 +03:00
|
|
|
|
function ServerError({ error }: { error: ErrorData }): React.ReactElement | null {
|
2025-02-10 01:32:16 +03:00
|
|
|
|
if (isAxiosError(error) && error.response && error.response.status === 400) {
|
2025-02-03 18:17:07 +03:00
|
|
|
|
return (
|
|
|
|
|
<div className='text-sm select-text text-warn-600'>
|
|
|
|
|
На Портале отсутствует такое сочетание имени пользователя и пароля
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2024-06-07 20:17:03 +03:00
|
|
|
|
}
|
2025-01-29 23:18:08 +03:00
|
|
|
|
throw error as Error;
|
2024-06-07 20:17:03 +03:00
|
|
|
|
}
|