From c3da4bbaefdac62112428bbf182fd5501405407a Mon Sep 17 00:00:00 2001 From: Felipe Cardoso Date: Wed, 5 Mar 2025 11:04:39 +0100 Subject: [PATCH] Add a navbar component and refactor dashboard logic Introduce a reusable Navbar component to streamline navigation and hide it on public pages. Simplify dashboard logic by removing redundant authentication checks and improve layout structure. Enhances user experience with a cleaner and more maintainable codebase. --- frontend/src/app/(main)/dashboard/page.tsx | 90 ++++++++-------------- frontend/src/components/layout/navbar.tsx | 74 ++++++++++++++++++ 2 files changed, 104 insertions(+), 60 deletions(-) create mode 100644 frontend/src/components/layout/navbar.tsx diff --git a/frontend/src/app/(main)/dashboard/page.tsx b/frontend/src/app/(main)/dashboard/page.tsx index 4bea044..ad7a18b 100644 --- a/frontend/src/app/(main)/dashboard/page.tsx +++ b/frontend/src/app/(main)/dashboard/page.tsx @@ -2,32 +2,10 @@ "use client"; import { useAuth } from "@/context/auth-context"; -import { useRouter } from "next/navigation"; -import { useEffect } from "react"; +import Navbar from "@/components/layout/navbar"; export default function DashboardPage() { - const { user, isLoading, isAuthenticated, logout } = useAuth(); - const router = useRouter(); - - // Redirect to login if not authenticated - useEffect(() => { - // Add a small delay to ensure context is fully updated - const checkAuth = setTimeout(() => { - if (!isLoading && !isAuthenticated) { - console.log("Not authenticated, redirecting to login"); - router.push("/login"); - } - }, 100); - - return () => clearTimeout(checkAuth); - }, [isLoading, isAuthenticated, router]); - - // Handle logout with explicit redirect - const handleLogout = () => { - logout(); - // Force redirection to login - router.push("/login"); - }; + const { user, isLoading } = useAuth(); // Show loading state if (isLoading) { @@ -41,48 +19,40 @@ export default function DashboardPage() { ); } - // Extra safeguard - if we somehow get here without authentication, redirect - if (!isAuthenticated) { - router.push("/login"); - return
Redirecting...
; - } - return ( -
-
-

Dashboard

+ <> + +
+
+

Dashboard

-
-

- Welcome, {user?.first_name || "User"}! -

-

- You are now logged in to EventSpace. -

+
+

+ Welcome, {user?.first_name || "User"}! +

+

+ You are now logged in to EventSpace. +

-
-

Your Events

-
-

No events yet

- +
+

Your Events

+
+

+ No events yet +

+ +
- -
- -
-
+ ); } diff --git a/frontend/src/components/layout/navbar.tsx b/frontend/src/components/layout/navbar.tsx new file mode 100644 index 0000000..fbd3098 --- /dev/null +++ b/frontend/src/components/layout/navbar.tsx @@ -0,0 +1,74 @@ +// src/components/layout/navbar.tsx +"use client"; + +import { useAuth } from "@/context/auth-context"; +import Link from "next/link"; +import { usePathname } from "next/navigation"; + +export default function Navbar() { + const { user, isAuthenticated, logout } = useAuth(); + const pathname = usePathname(); + + // Skip rendering navbar on login and public pages + const hideNavbarOn = ["/login", "/register", "/invite"]; + if (hideNavbarOn.includes(pathname || "")) { + return null; + } + + return ( +
+
+
+
+ + EventSpace + + + {isAuthenticated && ( + + )} +
+ +
+ {isAuthenticated ? ( +
+ + {user?.first_name} + + +
+ ) : ( + + Sign in + + )} +
+
+
+
+ ); +}