diff --git a/components/navbar.tsx b/components/navbar.tsx index 7e2dbbb..d324228 100644 --- a/components/navbar.tsx +++ b/components/navbar.tsx @@ -9,6 +9,8 @@ import { CircleXIcon, Gamepad2Icon, MenuIcon, XIcon } from "lucide-react" import { routes } from "@/lib/routes" import { usePathname, useRouter, useSearchParams } from "next/navigation" import { useDebounce } from "@/lib/hooks/useDebounce" +import { authClient } from "@/lib/auth-client" +import { LogOut, User } from "lucide-react" export default function Navbar() { const pathname = usePathname() @@ -24,6 +26,10 @@ export default function Navbar() { const [forceFocusStyles, setForceFocusStyles] = useState(false) const inputRef = useRef(null) + const { data: session, isPending: isSessionLoading } = + authClient.useSession() + const [userMenuOpen, setUserMenuOpen] = useState(false) + // Sync search query with URL ?q= param useEffect(() => { const q = searchParams.get("q") || "" @@ -183,6 +189,70 @@ export default function Navbar() { ))} + {/* Desktop Auth Controls */} +
+ {isSessionLoading ? ( +
+ ) : session ? ( +
+ + {userMenuOpen && ( + <> +
setUserMenuOpen(false)} + /> +
+ +
+ + )} +
+ ) : ( + <> + + Sign in + + + Sign up + + + )} +
+ {/* Mobile Hamburger Button */} +
+ ) : ( +
+ setMobileMenuOpen(false)} + className='block w-full text-center px-3 py-2 rounded-lg border border-white/10 text-sm text-[#ebe4f1] hover:bg-white/[0.05] transition-colors' + > + Sign in + + setMobileMenuOpen(false)} + className='block w-full text-center px-3 py-2 rounded-lg bg-[#eb3779] text-white text-sm font-medium hover:bg-[#eb3779]/90 transition-colors' + > + Sign up + +
+ )} +
)} diff --git a/middleware.ts b/middleware.ts new file mode 100644 index 0000000..3a3183f --- /dev/null +++ b/middleware.ts @@ -0,0 +1,29 @@ +import { NextRequest, NextResponse } from "next/server" + +const authRoutes = [ + "/login", + "/signup", + "/forgot-password", + "/reset-password", +] + +export default async function middleware(req: NextRequest) { + const path = req.nextUrl.pathname + const isAuthRoute = authRoutes.some((route) => path.startsWith(route)) + + // Check for better-auth session cookie + const hasSession = req.cookies + .getAll() + .some((cookie) => cookie.name.startsWith("better-auth.")) + + // Redirect authenticated users away from auth pages + if (isAuthRoute && hasSession) { + return NextResponse.redirect(new URL("/", req.url)) + } + + return NextResponse.next() +} + +export const config = { + matcher: ["/((?!api|_next/static|_next/image|.*\\.png$).*)"], +}