49 lines
1.3 KiB
JavaScript
49 lines
1.3 KiB
JavaScript
import styles from './Form.module.scss';
|
|
import { publicRuntimeConfig } from '@/config/runtime';
|
|
import axios from 'axios';
|
|
import { useState } from 'react';
|
|
import { useForm } from 'react-hook-form';
|
|
|
|
const { APP_BASE_PATH } = publicRuntimeConfig;
|
|
|
|
export function Form() {
|
|
const [hasError, setHasError] = useState(false);
|
|
const { handleSubmit, register } = useForm();
|
|
|
|
return (
|
|
<form
|
|
className={styles.form}
|
|
onSubmit={handleSubmit((data) => {
|
|
axios
|
|
.post('/login', data)
|
|
.then(() => {
|
|
const url =
|
|
(window.location.pathname.replace(APP_BASE_PATH, '') || '/') +
|
|
(window.location.search || '');
|
|
window.location.replace(url);
|
|
})
|
|
.catch(() => {
|
|
setHasError(true);
|
|
});
|
|
})}
|
|
>
|
|
<input
|
|
type="text"
|
|
placeholder="Логин"
|
|
required
|
|
autoComplete="on"
|
|
{...register('login', { required: true })}
|
|
/>
|
|
<input
|
|
type="password"
|
|
placeholder="Пароль"
|
|
required
|
|
autoComplete="on"
|
|
{...register('password', { required: true })}
|
|
/>
|
|
{hasError ? <span className="error">Неверный логин или пароль</span> : null}
|
|
<button type="submit">Войти</button>
|
|
</form>
|
|
);
|
|
}
|