Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions client/src/components/navbar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,44 @@
import React, { useState } from 'react';
import { MessageCircle, Menu, X } from 'lucide-react';

// importing useNavigate
import { Link, useNavigate } from 'react-router-dom';
// importing useNavigate and useLocation
import { Link, useLocation, useNavigate } from 'react-router-dom';
export default function Navbar() {
const [menuOpen, setMenuOpen] = useState(false);

const toggleMenu = () => setMenuOpen(!menuOpen);

const navigate = useNavigate();
const location = useLocation();
const currentpath = location.pathname;
// a condition to check whethere the current page is login/signup page
const hideLogout = currentpath==="/authpage";

const handleSignup = ()=>{
navigate('/authpage');
}

// a function to handle Logout feature
const handleLogoutuser =async ()=>{
try {
const response = await fetch(`http://localhost:5000/api/auth/logout`, {
method: "GET",
credentials: "include",
});

const data = await response.json();
if (response.ok) {
// a message to verify the logout which is send by backend
console.log(data.message);
navigate("/authpage");
} else {
console.error("Logout failed:", data.message);
}
} catch (err) {
console.error("Logout error:", err);
}
};

return (
<nav className="fixed top-0 left-0 w-full z-50 bg-white/10 backdrop-blur-md border-b border-white/20">
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
Expand All @@ -37,6 +63,8 @@ export default function Navbar() {
>
Sign Up
</button>

{!hideLogout && <button className="ml-4 px-4 py-2 bg-gradient-to-r from-red-500 to-red-500 text-white rounded-lg hover:from-red-600 hover:to-red-600 transition" onClick={handleLogoutuser} >Log Out</button>}
</div>

{/* Mobile Hamburger Icon */}
Expand Down
6 changes: 5 additions & 1 deletion server/controllers/authController.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,15 @@ const loginUser = async (req, res) => {
// @route POST /api/auth/logout
// @access public
const logoutUser = async (req, res) => {
// for testing
console.log("Logout endpoint hit");

res.cookie("token", "", {
httpOnly: true,
expires: new Date(0),
});
res.status(200).json({ success: true, message: "Logged out successfully" });
return res.status(200).json({ success: true, message: "Logged out successfully" });

};
// by Samay2006
// @desc Update user details (name, username, email)
Expand Down