2023-12-13 14:32:57 +03:00
|
|
|
|
'use client';
|
|
|
|
|
|
2023-09-03 18:26:50 +03:00
|
|
|
|
import axios from 'axios';
|
2023-12-15 17:34:50 +03:00
|
|
|
|
import clsx from 'clsx';
|
2023-07-15 17:46:19 +03:00
|
|
|
|
import { useEffect, useState } from 'react';
|
|
|
|
|
|
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-27 15:03:48 +03:00
|
|
|
|
import { useAuth } from '@/backend/auth/useAuth';
|
|
|
|
|
import { useLogin } from '@/backend/auth/useLogin';
|
2024-03-20 15:27:32 +03:00
|
|
|
|
import InfoError, { ErrorData } from '@/components/info/InfoError';
|
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';
|
2024-03-20 15:27:32 +03:00
|
|
|
|
import ExpectedAnonymous from '@/components/wrap/ExpectedAnonymous';
|
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();
|
|
|
|
|
const userQuery = query.get('username');
|
|
|
|
|
|
2025-01-27 15:03:48 +03:00
|
|
|
|
const { user } = useAuth();
|
|
|
|
|
const { login, isPending, error, reset } = useLogin();
|
2023-12-28 14:04:44 +03:00
|
|
|
|
|
2023-12-13 14:32:57 +03:00
|
|
|
|
const [username, setUsername] = useState(userQuery || '');
|
2023-07-31 23:47:18 +03:00
|
|
|
|
const [password, setPassword] = useState('');
|
2023-07-15 17:46:19 +03:00
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2025-01-27 15:03:48 +03:00
|
|
|
|
reset();
|
|
|
|
|
}, [username, password, reset]);
|
2023-07-15 17:46:19 +03:00
|
|
|
|
|
2023-07-31 23:47:18 +03:00
|
|
|
|
function handleSubmit(event: React.FormEvent<HTMLFormElement>) {
|
2023-07-15 17:46:19 +03:00
|
|
|
|
event.preventDefault();
|
2025-01-27 15:03:48 +03:00
|
|
|
|
if (!isPending) {
|
|
|
|
|
login(username, password, () => {
|
2023-12-13 14:32:57 +03:00
|
|
|
|
if (router.canBack()) {
|
|
|
|
|
router.back();
|
2023-09-02 01:11:27 +03:00
|
|
|
|
} else {
|
2024-04-01 21:45:10 +03:00
|
|
|
|
router.push(urls.library);
|
2023-09-02 01:11:27 +03:00
|
|
|
|
}
|
|
|
|
|
});
|
2023-07-15 17:46:19 +03:00
|
|
|
|
}
|
2023-07-31 23:47:18 +03:00
|
|
|
|
}
|
2023-07-15 17:46:19 +03:00
|
|
|
|
|
2023-12-08 00:33:47 +03:00
|
|
|
|
if (user) {
|
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 (
|
2024-12-12 13:19:12 +03:00
|
|
|
|
<form className={clsx('cc-column cc-fade-in', 'w-[24rem] mx-auto', 'pt-12 pb-6 px-6')} onSubmit={handleSubmit}>
|
|
|
|
|
<img alt='Концепт Портал' src={resources.logo} className='max-h-[2.5rem] min-w-[2.5rem] mb-3' />
|
|
|
|
|
<TextInput
|
|
|
|
|
id='username'
|
|
|
|
|
label='Логин или email'
|
|
|
|
|
autoComplete='username'
|
|
|
|
|
autoFocus
|
|
|
|
|
required
|
|
|
|
|
allowEnter
|
|
|
|
|
spellCheck={false}
|
|
|
|
|
value={username}
|
|
|
|
|
onChange={event => setUsername(event.target.value)}
|
|
|
|
|
/>
|
|
|
|
|
<TextInput
|
|
|
|
|
id='password'
|
|
|
|
|
type='password'
|
|
|
|
|
label='Пароль'
|
|
|
|
|
autoComplete='current-password'
|
|
|
|
|
required
|
|
|
|
|
allowEnter
|
|
|
|
|
value={password}
|
|
|
|
|
onChange={event => setPassword(event.target.value)}
|
|
|
|
|
/>
|
2023-07-15 17:46:19 +03:00
|
|
|
|
|
2024-12-12 13:19:12 +03:00
|
|
|
|
<SubmitButton
|
|
|
|
|
text='Войти'
|
|
|
|
|
className='self-center w-[12rem] mt-3'
|
2025-01-27 15:03:48 +03:00
|
|
|
|
loading={isPending}
|
2024-12-12 13:19:12 +03:00
|
|
|
|
disabled={!username || !password}
|
|
|
|
|
/>
|
|
|
|
|
<div className='flex flex-col text-sm'>
|
|
|
|
|
<TextURL text='Восстановить пароль...' href='/restore-password' />
|
|
|
|
|
<TextURL text='Нет аккаунта? Зарегистрируйтесь...' href='/signup' />
|
|
|
|
|
</div>
|
|
|
|
|
{error ? <ProcessError error={error} /> : null}
|
|
|
|
|
</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 =========
|
|
|
|
|
function ProcessError({ error }: { error: ErrorData }): React.ReactElement {
|
|
|
|
|
if (axios.isAxiosError(error) && error.response && error.response.status === 400) {
|
|
|
|
|
return (
|
2024-12-16 23:52:11 +03:00
|
|
|
|
<div className='text-sm select-text text-warn-600'>
|
2024-06-05 12:28:08 +03:00
|
|
|
|
На Портале отсутствует такое сочетание имени пользователя и пароля
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
return <InfoError error={error} />;
|
|
|
|
|
}
|
|
|
|
|
}
|