Skip to content

Commit

Permalink
added dispatch inside useAuth hook
Browse files Browse the repository at this point in the history
  • Loading branch information
bobrov-site committed May 21, 2024
1 parent 30b402b commit 4336069
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
5 changes: 1 addition & 4 deletions frontend/src/components/Header.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@ import Button from 'react-bootstrap/esm/Button';
import Nav from 'react-bootstrap/Nav';
import Navbar from 'react-bootstrap/Navbar';
import { Link } from 'react-router-dom';
import { useDispatch, useSelector } from 'react-redux';
import { useSelector } from 'react-redux';
import { useTranslation } from 'react-i18next';
import { setUserData } from '../store/slices/appSlice';
import useAuth from '../hooks';

const Header = () => {
const dispatch = useDispatch();
const { logOut } = useAuth();
const { t } = useTranslation();
const app = useSelector((state) => state.app);
const logOutUser = () => {
logOut();
dispatch(setUserData({ nickname: '', token: null }));
};
return (
<Navbar expand="lg" className="bg-body-tertiary mb-2">
Expand Down
26 changes: 18 additions & 8 deletions frontend/src/hooks/useAuth.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
const useAuth = () => ({
logOut: () => {
localStorage.removeItem('token');
localStorage.removeItem('nickname');
},
logIn: (token, nickname) => {
import { useDispatch } from 'react-redux';
import { setUserData } from '../store/slices/appSlice';

const useAuth = () => {
const dispatch = useDispatch();

const logIn = (token, nickname) => {
localStorage.setItem('token', token);
localStorage.setItem('nickname', nickname);
},
});
dispatch(setUserData({ nickname, token }));
};

const logOut = () => {
localStorage.removeItem('token');
localStorage.removeItem('nickname');
dispatch(setUserData({ nickname: '', token: null }));
};

return { logIn, logOut };
};

export default useAuth;
4 changes: 0 additions & 4 deletions frontend/src/pages/Login.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useDispatch } from 'react-redux';
import { Formik } from 'formik';
import { useNavigate, Link } from 'react-router-dom';
import Container from 'react-bootstrap/Container';
Expand All @@ -10,7 +9,6 @@ import Button from 'react-bootstrap/Button';
import Form from 'react-bootstrap/Form';
import { useTranslation } from 'react-i18next';
import { toast } from 'react-toastify';
import { setUserData } from '../store/slices/appSlice';
import { useLoginMutation } from '../api/auth';
import { appPaths } from '../routes';
import useAuth from '../hooks';
Expand All @@ -19,7 +17,6 @@ const Login = () => {
const { t } = useTranslation();
const { logIn } = useAuth();
const navigate = useNavigate();
const dispatch = useDispatch();
const [login] = useLoginMutation();
const handleFormSubmit = async (values, { setErrors }) => {
const { nickname, password } = values;
Expand All @@ -29,7 +26,6 @@ const Login = () => {
};
const { data, error } = await login(user);
if (data) {
dispatch(setUserData({ nickname, token: data.token }));
logIn(data.token, nickname);
navigate(appPaths.home());
}
Expand Down

0 comments on commit 4336069

Please sign in to comment.