Skip to content

Commit

Permalink
Logout Functionality (#1583)
Browse files Browse the repository at this point in the history
Created Logout File in client/src/components/Authorization/Logout.js, JWT Token Issue resolved
  • Loading branch information
neyaadeez authored Jan 25, 2024
1 parent 1ca85d2 commit 2f1f7c0
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 4 deletions.
2 changes: 2 additions & 0 deletions client/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import ResetPassword from "./components/Authorization/ResetPassword";
import ForgotPassword from "./components/Authorization/ForgotPassword";
import Feedback from "./components/Feedback/FeedbackPage";
import ErrorPage from "./components/ErrorPage";
import Logout from "./components/Authorization/Logout";

const calculationPath = "/calculation/:page/:projectId?/*";

Expand Down Expand Up @@ -138,6 +139,7 @@ const App = ({
<Route path="/updateaccount/:email?" element={<UpdateAccount />} />
<Route path="/confirm/:token?" element={<ConfirmEmail />} />
<Route path="/login/:email?" element={<Login />} />
<Route path="/logout" element={<Logout />} />
<Route path="/forgotpassword" element={<ForgotPassword />} />
<Route path="/resetPassword/:token" element={<ResetPassword />} />
<Route
Expand Down
18 changes: 18 additions & 0 deletions client/src/components/Authorization/Logout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from "react";
import { useNavigate } from "react-router-dom";
import * as accountService from "../../services/account.service";

const Logout = () => {
const navigate = useNavigate();
const handleLogout = async () => {
await accountService.logout();
// Redirect to the "/login" page after the logout
navigate("/login");
};
// Call handleLogout when the component mounts
handleLogout();
// You can return some UI if needed (e.g., a loading spinner)
return <div>Logging out...</div>;
};

export default Logout;
2 changes: 1 addition & 1 deletion client/src/components/Layout/NavBarLogin.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const NavBarLogin = ({ classes, handleHamburgerMenuClick }) => {
<Link
className={`${classes.link} ${classes.lastItem}`}
to={{
pathname: `/login/${(account && account.email) || ""}`,
pathname: `/logout`,
state: { prevPath: location.pathname }
}}
onClick={() => {
Expand Down
5 changes: 5 additions & 0 deletions client/src/services/account.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ export const login = async (email, password) => {
}
};

export const logout = async () => {
const response = await axios.get(`${baseUrl}/logout`);
return response;
};

export const confirmRegister = async token => {
const body = { token };
const response = await axios.post(baseUrl + "/confirmRegister", body);
Expand Down
11 changes: 8 additions & 3 deletions server/app/routes/account.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,14 @@ router.post("/resetPassword", accountController.resetPassword);

router.post("/login/:email?", accountController.login, jwtSession.login);
router.get("/logout", (req, res) => {
console.log("logging out");
req.logout();
res.sendStatus(200);
// Clear the "jwt" cookie
res.clearCookie("jwt", { httpOnly: true });
// res.redirect("/login");
// Additional logic, such as redirecting to the login page
setTimeout(() => {
// Respond after the delay
res.send("Logout successful");
}, 10);
});

router.put(
Expand Down

0 comments on commit 2f1f7c0

Please sign in to comment.