Skip to content

Commit 30e8eb8

Browse files
authored
Merge pull request #59 from adityacosmos24/handlelogout_frontend
bug: fixed logout button in frontend added handle logout logic
2 parents e9c0f9f + ea5f435 commit 30e8eb8

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

client/src/components/Canvas.jsx

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,34 @@ export const Canvas = () => {
5252
const [hoveredHandle, setHoveredHandle] = useState(null); // { id, dir }
5353

5454
const handleLogout = async () => {
55-
// placeholder
55+
try {
56+
const token = localStorage.getItem("token");
57+
if (!token) {
58+
toast.error("You are not logged in.");
59+
return;
60+
}
61+
62+
const res = await fetch("http://localhost:3000/api/auth/logout", {
63+
method: "POST",
64+
headers: { "Content-Type": "application/json" },
65+
body: JSON.stringify({ token }),
66+
});
67+
68+
if (!res.ok) {
69+
const data = await res.json().catch(() => ({}));
70+
toast.error(data.message || "Logout failed");
71+
return;
72+
}
73+
74+
// Clear auth state and redirect
75+
localStorage.removeItem("token");
76+
setIsLoggedIn(false);
77+
toast.success("Logged out successfully");
78+
window.location.href = "/";
79+
} catch (err) {
80+
console.error("Logout error:", err);
81+
toast.error("Logout failed. Please try again.");
82+
}
5683
};
5784

5885
// --- Helpers ---

0 commit comments

Comments
 (0)