diff --git a/frontend/src/Components/App.js b/frontend/src/Components/App.js index 0e0d120..82074fe 100644 --- a/frontend/src/Components/App.js +++ b/frontend/src/Components/App.js @@ -8,6 +8,7 @@ import AuthProvider from '../hoc/AuthProvider.js'; import ChatPage from '../page/ChatPage.js'; import store from '../store/store.js'; import ApiProvider from '../hoc/ApiProvider.js'; +import RegistrationPage from '../page/RegistrationPage.js'; const App = () => (
@@ -20,6 +21,7 @@ const App = () => ( } /> } /> } /> + } /> diff --git a/frontend/src/Components/Layout.js b/frontend/src/Components/Layout.js index 73948b0..eb2a495 100644 --- a/frontend/src/Components/Layout.js +++ b/frontend/src/Components/Layout.js @@ -1,20 +1,31 @@ import { - Link, Outlet, useLocation, Navigate, + Link, Outlet, useLocation, Navigate, useNavigate, } from 'react-router-dom'; +import { Button } from 'react-bootstrap'; +import { useContext } from 'react'; import getRoutes from '../routes'; import useAuth from '../hook/useAuth'; +import { AuthContext } from '../hoc/AuthProvider'; const Layout = () => { const location = useLocation(); const { userToken } = useAuth(); + const { signOut } = useContext(AuthContext); + const navigate = useNavigate(); + + const hendlesignOut = () => { + signOut(() => navigate(getRoutes.loginpage())); + }; + if (location.pathname === getRoutes.main() && !userToken) { - return ; + return ; } return ( <> diff --git a/frontend/src/Components/RegistrationForm.js b/frontend/src/Components/RegistrationForm.js new file mode 100644 index 0000000..9c65a72 --- /dev/null +++ b/frontend/src/Components/RegistrationForm.js @@ -0,0 +1,126 @@ +import axios from 'axios'; +import { Formik } from 'formik'; +import { Button, Form } from 'react-bootstrap'; +import * as yup from 'yup'; +import { + useContext, useEffect, useRef, useState, +} from 'react'; +import { useLocation, useNavigate } from 'react-router-dom'; +import getRoutes from '../routes.js'; +import { AuthContext } from '../hoc/AuthProvider'; + +const RegistrationForm = () => { + const { signIn } = useContext(AuthContext); + const navigate = useNavigate(); + const location = useLocation(); + const [isExistingUser, setExistingUser] = useState(false); + const usernameRef = useRef(); + + const fromPage = location.state?.from?.pathname ?? getRoutes.main(); + + const registrationSchema = yup.object().shape({ + username: yup.string().required('Обязательное поле').min(3, 'От 3 до 20 символов').max(20, 'От 3 до 20 символов'), + password: yup.string().required('Обязательное поле').min(6, 'Не менее 6 символов'), + confirmPassword: yup.string().required('Обязательное поле').oneOf([yup.ref('password')], 'Пароли должны совпадать'), + }); + + useEffect(() => { + usernameRef.current.select(); + }, []); + + return ( + { + try { + setExistingUser(false); + const { data } = await axios.post(getRoutes.signup(), { username, password }); + signIn(data, () => navigate(fromPage, { replace: true })); + } catch (err) { + switch (err.response.status) { + case 409: + setExistingUser(true); + break; + + default: + console.error('Возникла непредвиденная ошибка'); + break; + } + } + }} + > + {(props) => ( +
+

Регистрация

+ + + Имя пользователя + { + props.touched.username + && props.errors.username + && {props.errors.username} + } + {isExistingUser && Такой пользователь уже существует} + + + + Пароль + { + props.touched.password + && props.errors.password + && {props.errors.password} + } + {isExistingUser && Такой пользователь уже существует} + + + + Подтвердите пароль + { + props.touched.confirmPassword + && props.errors.confirmPassword + && {props.errors.confirmPassword} + } + {isExistingUser && Такой пользователь уже существует} + + +
+ )} +
+ ); +}; + +export default RegistrationForm; diff --git a/frontend/src/assets/avatar_1.jpg b/frontend/src/assets/avatar1.jpg similarity index 100% rename from frontend/src/assets/avatar_1.jpg rename to frontend/src/assets/avatar1.jpg diff --git a/frontend/src/page/LoginPage.js b/frontend/src/page/LoginPage.js index 2393c8b..e991315 100644 --- a/frontend/src/page/LoginPage.js +++ b/frontend/src/page/LoginPage.js @@ -17,7 +17,7 @@ const LoginPage = () => (
Нет аккаунта? - Регистрация + Регистрация
diff --git a/frontend/src/page/RegistrationPage.js b/frontend/src/page/RegistrationPage.js new file mode 100644 index 0000000..236b43d --- /dev/null +++ b/frontend/src/page/RegistrationPage.js @@ -0,0 +1,21 @@ +import avatar1 from '../assets/avatar1.jpg'; +import RegistrationForm from '../Components/RegistrationForm'; + +const RegistrationPage = () => ( +
+
+
+
+
+
+ Регистрация +
+ +
+
+
+
+
+); + +export default RegistrationPage; diff --git a/frontend/src/routes.js b/frontend/src/routes.js index b66037d..2259749 100644 --- a/frontend/src/routes.js +++ b/frontend/src/routes.js @@ -1,6 +1,7 @@ export default { main: () => '/', loginpage: () => '/login', + signuppage: () => '/signup', login: () => '/api/v1/login', signup: () => '/api/v1/signup', data: () => '/api/v1/data',