Skip to content

Commit

Permalink
Merge pull request #70 from BUMETCS673/Hw-175-logout
Browse files Browse the repository at this point in the history
logout component
  • Loading branch information
ccerav-bu authored Oct 7, 2024
2 parents 4f67fe1 + 4c65b8b commit 0dd05cc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 18 deletions.
25 changes: 7 additions & 18 deletions code/client/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import ManageProfile from "./components/ManageProfile.js";
import DailyData from "./components/DailyData.js";
import CreateGoal from "./components/CreateGoal.js";
import ManageDailyData from "./components/ManageDailyData.js";
import LogoutButton from './components/Logout.js';

import React, { useState, useEffect } from 'react';
import LogoutIcon from '@mui/icons-material/Logout';
Expand Down Expand Up @@ -36,6 +37,9 @@ import AccountCircleIcon from "@mui/icons-material/AccountCircle";
import EditNoteIcon from "@mui/icons-material/EditNote";
import TrackChangesIcon from "@mui/icons-material/TrackChanges";
import TodayIcon from '@mui/icons-material/Today';
import CloseIcon from "@mui/icons-material/Close";

import { useNavigate } from 'react-router-dom';

import "@fontsource/mulish";

Expand Down Expand Up @@ -64,14 +68,6 @@ function App({ RouterComponent = Router }) {
setIsAuthenticated(!!token);
}, []);

const handleLogout = () => {

localStorage.removeItem('authToken');

setIsAuthenticated(false);

// navigate('/login');
};
return (
<ThemeProvider theme={theme}>
<RouterComponent>
Expand Down Expand Up @@ -143,16 +139,9 @@ function App({ RouterComponent = Router }) {
<ListItemText primary="Login" />
</ListItemButton>
</ListItem>
) : (
<ListItem disablePadding>
<ListItemButton onClick={handleLogout}>
<ListItemIcon>
<LogoutIcon />
</ListItemIcon>
<ListItemText primary="Logout" />
</ListItemButton>
</ListItem>
)}
) : (
<LogoutButton setIsAuthenticated={setIsAuthenticated} />
)}

<ListItem disablePadding>
<ListItemButton component={Link} to="/enter-daily-data">
Expand Down
32 changes: 32 additions & 0 deletions code/client/src/components/Logout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// LogoutButton.js

import React from 'react';
import { useNavigate } from 'react-router-dom';
import LogoutIcon from '@mui/icons-material/Logout';
import { ListItem, ListItemButton, ListItemIcon, ListItemText } from '@mui/material';

function LogoutButton({ setIsAuthenticated }) {
const navigate = useNavigate();

const handleLogout = () => {

localStorage.removeItem('authToken');

setIsAuthenticated(false);

navigate('/login');
};

return (
<ListItem disablePadding>
<ListItemButton onClick={handleLogout}>
<ListItemIcon>
<LogoutIcon />
</ListItemIcon>
<ListItemText primary="Logout" />
</ListItemButton>
</ListItem>
);
}

export default LogoutButton;

0 comments on commit 0dd05cc

Please sign in to comment.