From 9d746dd4124cc80970c55f94135ff20e8a749c05 Mon Sep 17 00:00:00 2001 From: Natalia Bandurova Date: Sat, 2 Nov 2024 00:36:15 +0300 Subject: [PATCH] refactor: delete useAuth hook --- frontend/src/App.jsx | 11 ++++------ frontend/src/contexts/AuthContext.js | 23 -------------------- frontend/src/pages/LoginPage/LoginForm.jsx | 3 --- frontend/src/pages/SignUpPage/SignUpForm.jsx | 3 --- frontend/src/store/hooks/hooks.js | 4 +--- 5 files changed, 5 insertions(+), 39 deletions(-) delete mode 100644 frontend/src/contexts/AuthContext.js diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx index 223a33b..38249b2 100644 --- a/frontend/src/App.jsx +++ b/frontend/src/App.jsx @@ -1,7 +1,6 @@ import { Outlet } from 'react-router-dom'; import { ToastContainer } from 'react-toastify'; import Header from './components/Header'; -import { AuthProvider } from './contexts/AuthContext'; const App = () => ( <> @@ -9,12 +8,10 @@ const App = () => ( autoClose={2000} draggable /> - -
-
- -
-
+
+
+ +
); diff --git a/frontend/src/contexts/AuthContext.js b/frontend/src/contexts/AuthContext.js deleted file mode 100644 index 4a85a0c..0000000 --- a/frontend/src/contexts/AuthContext.js +++ /dev/null @@ -1,23 +0,0 @@ -import { createContext, useState, useMemo } from 'react'; - -const AuthContext = createContext({}); - -const AuthProvider = ({ children }) => { - const [loggedIn, setLoggedIn] = useState(!!localStorage.getItem('chat-token')); - - const logIn = () => setLoggedIn(true); - const logOut = () => { - localStorage.clear(); - setLoggedIn(false); - }; - - const context = useMemo(() => ({ loggedIn, logIn, logOut }), [loggedIn]); - - return ( - - {children} - - ); -}; - -export { AuthProvider, AuthContext }; diff --git a/frontend/src/pages/LoginPage/LoginForm.jsx b/frontend/src/pages/LoginPage/LoginForm.jsx index 19be539..95c6fc9 100644 --- a/frontend/src/pages/LoginPage/LoginForm.jsx +++ b/frontend/src/pages/LoginPage/LoginForm.jsx @@ -5,13 +5,11 @@ import { Button, Form } from 'react-bootstrap'; import { useTranslation } from 'react-i18next'; import { useFormik } from 'formik'; import { toast } from 'react-toastify'; -import { useAuth } from '../../store/hooks/hooks'; import { setUserData } from '../../store/slices/authSlice'; import { useLoginMutation } from '../../api/authApi'; import { ROOT_PATH, getRoutesPath } from '../../router/routesPath'; const LoginForm = () => { - const auth = useAuth(); const navigate = useNavigate(); const dispatch = useDispatch(); const [authFailed, setAuthFailed] = useState(false); @@ -33,7 +31,6 @@ const LoginForm = () => { try { const data = await login(values).unwrap(); dispatch(setUserData(data)); - auth.logIn(); navigate(getRoutesPath(ROOT_PATH)); } catch (err) { if (err.status === 401) { diff --git a/frontend/src/pages/SignUpPage/SignUpForm.jsx b/frontend/src/pages/SignUpPage/SignUpForm.jsx index 6248080..950d23c 100644 --- a/frontend/src/pages/SignUpPage/SignUpForm.jsx +++ b/frontend/src/pages/SignUpPage/SignUpForm.jsx @@ -7,12 +7,10 @@ import { useRef } from 'react'; import { object, string, ref } from 'yup'; import { toast } from 'react-toastify'; import { useSignupMutation } from '../../api/authApi'; -import { useAuth } from '../../store/hooks/hooks'; import { setUserData } from '../../store/slices/authSlice'; import { ROOT_PATH, getRoutesPath } from '../../router/routesPath'; const SignUpForm = () => { - const auth = useAuth(); const navigate = useNavigate(); const dispatch = useDispatch(); const inputRef = useRef(); @@ -36,7 +34,6 @@ const SignUpForm = () => { try { const data = await signup(values).unwrap(); dispatch(setUserData(data)); - auth.logIn(); navigate(getRoutesPath(ROOT_PATH)); } catch (err) { if (err.status === 409) { diff --git a/frontend/src/store/hooks/hooks.js b/frontend/src/store/hooks/hooks.js index acc4c27..09d9b6d 100644 --- a/frontend/src/store/hooks/hooks.js +++ b/frontend/src/store/hooks/hooks.js @@ -1,8 +1,6 @@ import { useContext } from 'react'; -import { AuthContext } from '../../contexts/AuthContext'; import { SocketContext } from '../../contexts/SocketContext'; -const useAuth = () => useContext(AuthContext); const useSocket = () => useContext(SocketContext); -export { useAuth, useSocket }; +export default useSocket;