Skip to content

Commit

Permalink
feat: add logout func to logout button (dont navigate to login)
Browse files Browse the repository at this point in the history
  • Loading branch information
SplitCode committed Oct 3, 2024
1 parent c3fec26 commit 3a7d31b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 18 deletions.
14 changes: 0 additions & 14 deletions frontend/src/components/AuthButton.jsx

This file was deleted.

4 changes: 3 additions & 1 deletion frontend/src/components/Header.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import Navbar from 'react-bootstrap/Navbar';
import Container from 'react-bootstrap/Container';
import { NavLink } from 'react-router-dom';
import LogoutButton from './LogoutButton';

const Header = () => (
<Navbar expand="lg" className="shadow-sm navbar navbar-expand-lg navbar-light bg-white">
<Container>
<Navbar.Brand as={NavLink} to="/">Hexlet Chat</Navbar.Brand>
<button type="button" className="btn btn-primary">Выйти</button>
<LogoutButton />
{/* <button type="button" className="btn btn-primary">Выйти</button> */}
</Container>
</Navbar>
);
Expand Down
24 changes: 24 additions & 0 deletions frontend/src/components/LogoutButton.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Button } from 'react-bootstrap';
import { useNavigate } from 'react-router-dom';
import useAuth from '../store/hooks/useAuth';

const LogoutButton = () => {
const auth = useAuth();
const navigate = useNavigate();

return (
auth.loggedIn
? (
<Button onClick={() => {
auth.logOut();
navigate('/login');
}}
>
Выйти
</Button>
)
: null
);
};

export default LogoutButton;
6 changes: 3 additions & 3 deletions frontend/src/contexts/AuthContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ const AuthProvider = ({ children }) => {

const logIn = () => setLoggedIn(true);
const logOut = () => {
localStorage.removeItem('chat-token');
localStorage.clear();
setLoggedIn(false);
};

const authContextValue = useMemo(() => ({ loggedIn, logIn, logOut }), [loggedIn]);
const context = useMemo(() => ({ loggedIn, logIn, logOut }), [loggedIn]);

return (
<AuthContext.Provider value={authContextValue}>
<AuthContext.Provider value={context}>
{children}
</AuthContext.Provider>
);
Expand Down

0 comments on commit 3a7d31b

Please sign in to comment.