import React, {useState} from 'react' import {Link, useNavigate} from 'react-router-dom' import {useDispatch, useSelector} from 'react-redux' import {withSwalInstance} from 'sweetalert2-react' import swal from 'sweetalert2' import {Loader} from '../Loader/Loader' import ErrorBoundary from "../../hoc/ErrorBoundary"; import {auth, selectAuth, setUserInfo} from '../../redux/outstaffingSlice' import {loading} from '../../redux/loaderSlice' import {setRole} from '../../redux/roleSlice' import {selectIsLoading} from '../../redux/loaderSlice' import {useRequest} from "../../hooks/useRequest"; import ellipse from '../../images/ellipse.png' import './authBox.scss' const SweetAlert = withSwalInstance(swal); export const AuthBox = ({title, altTitle, roleChangeLink}) => { const dispatch = useDispatch(); const navigate = useNavigate(); const isAuth = useSelector(selectAuth); const isLoading = useSelector(selectIsLoading); const {apiRequest} = useRequest(); const [username, setUsername] = useState(''); const [password, setPassword] = useState(''); const [error, setError] = useState(null); if (isAuth) { navigate('/') } const submitHandler = () => { if (!isLoading) { dispatch(loading(true)); apiRequest('/user/login', { method: 'POST', data: JSON.stringify({ username, password }) }).then((res) => { if (!res.access_token) { setError('Некорректные данные для входа'); dispatch(loading(false)) } else { localStorage.setItem('auth_token', res.access_token); localStorage.setItem('id', res.id); localStorage.setItem('cardId', res.card_id); localStorage.setItem( 'access_token_expired_at', res.access_token_expired_at ); dispatch(auth(true)); dispatch(setUserInfo(res)); dispatch(loading(false)); dispatch(setRole('ROLE_PARTNER')) } }) } }; return (

Войти в систему

{title}
setUsername(e.target.value)} /> setPassword(e.target.value)} /> {error && (
setError(null)} />
)}
) };