diff --git a/src/pages/LoginPage/index.tsx b/src/pages/LoginPage/index.tsx index fb45451..891d6c8 100644 --- a/src/pages/LoginPage/index.tsx +++ b/src/pages/LoginPage/index.tsx @@ -1,21 +1,118 @@ +import { LockOutlined, UserOutlined } from '@ant-design/icons' +import { Button, Divider } from 'antd' +import { AxiosError } from 'axios' +import { FormComponents, useAuth } from 'isp-ui-kit' +import { useForm } from 'react-hook-form' import { useNavigate } from 'react-router-dom' +import { apiPaths } from '@constants/api/apiPaths.ts' +import { ValidationRules } from '@constants/form/validationRules.ts' +import { localStorageKeys } from '@constants/localStorageKeys.ts' +import { messages } from '@constants/messages.ts' + +import { getConfigProperty } from '@utils/configUtils.ts' +import { LocalStorage } from '@utils/localStorageUtils.ts' + +import { routePaths } from '@routes/routePaths.ts' + +import { MSPError } from '@type/index.ts' +import { LoginRequest } from '@type/login.type.ts' + +import './login-page.scss' + +const passwordLoginEnabled = getConfigProperty('ENABLE_PASSWORD_LOGIN', true) + const LoginPage = () => { + const { login, isLoading } = useAuth() + const { handleSubmit, control, setError } = useForm({ + mode: 'onChange' + }) const navigate = useNavigate() + const setFormError = (statusText: string, message: string): void => { + setError('email', {}) + setError('password', { + message, + type: statusText + }) + } + + const handleError = (err: AxiosError): void => { + const { response } = err + if (response && response.status === 401) { + setFormError(response.statusText, messages.loginError) + } else { + setFormError('serverError', messages.serverError) + } + } + + const handleSubmitForm = (data: LoginRequest): void => { + login(apiPaths.login, data, { + 'X-APPLICATION-TOKEN': getConfigProperty( + 'APP_TOKEN', + import.meta.env.VITE_APP_TOKEN + ) + }) + .then((response) => { + LocalStorage.set(localStorageKeys.HEADER_NAME, response.headerName) + LocalStorage.set(localStorageKeys.USER_TOKEN, response.token) + const redirectUrl = + localStorage.getItem('redirectUrl') || routePaths.home + navigate(redirectUrl, { replace: true }) + localStorage.removeItem('redirectUrl') + }) + .catch((err: any) => handleError(err)) + } + + const renderInternalAuthForm = () => { + if (!passwordLoginEnabled) { + return null + } + + return ( + <> + } + placeholder="Логин" + rules={{ required: ValidationRules.required }} + /> + } + type="password" + placeholder="Пароль" + rules={{ required: ValidationRules.required }} + /> + + + ) + } + return (
- +
+

+ Вход в систему +

+ + {renderInternalAuthForm()} +
) } diff --git a/yarn.lock b/yarn.lock index f60f1af..637d1f0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3742,8 +3742,8 @@ isexe@^2.0.0: integrity sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw== "isp-ui-kit@https://github.com/txix-open/isp-ui-kit": - version "0.3.2" - resolved "https://github.com/txix-open/isp-ui-kit#dbbe386bfa9e870cf27d483dc8dcd9ae1494a51a" + version "0.3.3" + resolved "https://github.com/txix-open/isp-ui-kit#508a5bd9cb4efac0e9389ad2f9f8c60683d1712e" dependencies: "@codemirror/lang-javascript" "^6.2.1" "@codemirror/lang-json" "^6.0.1"