Login logout finally working

This commit is contained in:
hz
2025-05-13 00:12:13 -07:00
parent fbd48297e2
commit 8f1cba5f82
5 changed files with 135 additions and 55 deletions
+54
View File
@@ -0,0 +1,54 @@
import React from "react";
import { Navigate, useNavigate } from "react-router-dom";
import PageWrapper from "./PageWrapper";
const ProtectedRoute = ({ children }) => {
const token = localStorage.getItem("token");
const navigate = useNavigate();
const handleLogout = () => {
localStorage.removeItem("token");
navigate("/login");
};
if (!token) {
return <Navigate to="/login" />;
}
return (
<PageWrapper>
<div
style={{
height: "100vh",
margin: 0,
padding: 0,
display: "flex",
alignItems: "center",
justifyContent: "center",
backgroundColor: "#242424",
position: "relative",
}}
>
<button
onClick={handleLogout}
style={{
position: "absolute",
top: "1rem",
right: "1rem",
backgroundColor: "#1a1a1a",
color: "#fff",
padding: "0.5rem 1rem",
border: "none",
borderRadius: "4px",
cursor: "pointer",
}}
>
Logout
</button>
{children}
</div>
</PageWrapper>
);
};
export default ProtectedRoute;