Add user menu with logout in navbar

Replaced the text-based logout button with a user menu using a popover. The menu includes the user's name and a logout option with an icon for better UX. This improves the design and accessibility of the authenticated navbar.
This commit is contained in:
2025-03-12 10:14:08 +01:00
parent d5a24234d0
commit 768d1820e5

View File

@@ -6,12 +6,17 @@ import Link from "next/link";
import { usePathname } from "next/navigation"; import { usePathname } from "next/navigation";
import ThemeToggle from "@/components/ui/theme-toggle"; import ThemeToggle from "@/components/ui/theme-toggle";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import {
Popover,
PopoverContent,
PopoverTrigger,
} from "@/components/ui/popover";
import { User, LogOut } from "lucide-react";
export default function Navbar() { export default function Navbar() {
const { user, isAuthenticated, logout } = useAuth(); const { user, isAuthenticated, logout } = useAuth();
const pathname = usePathname(); const pathname = usePathname();
// Skip rendering navbar on login and public pages
const hideNavbarOn = ["/login", "/register", "/invite"]; const hideNavbarOn = ["/login", "/register", "/invite"];
if (hideNavbarOn.includes(pathname || "")) { if (hideNavbarOn.includes(pathname || "")) {
return null; return null;
@@ -51,14 +56,29 @@ export default function Navbar() {
<ThemeToggle /> <ThemeToggle />
{isAuthenticated ? ( {isAuthenticated ? (
<div className="flex items-center gap-3"> <Popover>
<span className="text-sm text-muted-foreground"> <PopoverTrigger asChild>
{user?.first_name} <Button variant="outline" size="icon">
</span> <User className="h-4 w-4" />
<Button variant="ghost" className="px-2" onClick={logout}> <span className="sr-only">Toggle user menu</span>
Sign out </Button>
</Button> </PopoverTrigger>
</div> <PopoverContent align="end" className="w-48">
<div className="px-2 py-2 flex flex-col">
<span className="mb-2 text-sm font-medium text-muted-foreground">
{user?.first_name} {user?.last_name}
</span>
<Button
variant="ghost"
onClick={logout}
className="justify-start space-x-2 px-2"
>
<LogOut className="h-4 w-4" />
<span>Sign out</span>
</Button>
</div>
</PopoverContent>
</Popover>
) : ( ) : (
<Button asChild size="sm"> <Button asChild size="sm">
<Link href="/login">Sign in</Link> <Link href="/login">Sign in</Link>