Skip to content

Commit

Permalink
fix: implements logout
Browse files Browse the repository at this point in the history
  • Loading branch information
erarich committed Dec 1, 2023
1 parent 4b0e1bd commit e3bd765
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
26 changes: 25 additions & 1 deletion src/layout/MenuButtons.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,27 @@ import { Link } from "react-router-dom";
import styles from "../styles/Menu.module.css";
import { Button } from "@mui/material";
import { userStore } from "../stores/userState";
import api from "../services/api";
import { useNavigate } from "react-router-dom";

export const MenuButtons = () => {
const userState = userStore((state) => state.userLogged);
const setuserStore = userStore((state) => state.setUserState);
const navigate = useNavigate();

const submitLogOut = () => {
api
.get(`/users/logout/`)
.then((response) => {
navigate("/");
setuserStore(false);
localStorage.clear();
})
.catch((error) => {
// Trate o erro, se necessário
console.error(error);
});
};

let buttons;

Expand Down Expand Up @@ -35,7 +52,14 @@ export const MenuButtons = () => {
</li>

<li className={styles.menu__item}>
<Button variant="contained">Log Out</Button>
<Button
onClick={() => {
submitLogOut();
}}
variant="contained"
>
Log Out
</Button>
</li>
</ul>
);
Expand Down
6 changes: 3 additions & 3 deletions src/pages/Home.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@ export const Home = () => {
const userState = userStore((state) => state.userLogged);

if (userState) {
console.log("userState: ", userState);
localStorage.setItem("isUserLogged", true);
}

let storageIsUserLogged = localStorage.getItem("isUserLogged");
console.log("storageIsUserLogged: ", storageIsUserLogged);
console.log("storageIsUserLogged type: ", typeof storageIsUserLogged);
let boolStorageIsUserLogged = storageIsUserLogged === "true";
console.log("boolStorageIsUserLogged: ", boolStorageIsUserLogged);
const setUserState = userStore((state) => state.setUserState);

if (boolStorageIsUserLogged) {
Expand Down Expand Up @@ -110,6 +108,8 @@ export const Home = () => {
// console.log(storageUsername);
// console.log(storageEmail);

console.log("boolStorageIsUserLogged: ", boolStorageIsUserLogged);

if (boolStorageIsUserLogged === false) {
content = (
<Section>
Expand Down

0 comments on commit e3bd765

Please sign in to comment.