feat: add admin layout with sidebar, auth guard, and navbar link
- Admin layout with server-side auth guard (redirects non-admins) - Reusable sidebar component with active state detection - Navbar profile dropdown shows admin link for admin users - Admin routes are noindexed (robots: false) - /admin redirects to /admin/users
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
import { redirect } from "next/navigation"
|
||||
|
||||
export default function AdminPage() {
|
||||
redirect("/admin/users")
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
import { redirect } from "next/navigation"
|
||||
import { headers } from "next/headers"
|
||||
import { auth } from "@/lib/auth"
|
||||
import { AdminSidebar } from "@/components/admin/admin-sidebar"
|
||||
import type { Metadata } from "next"
|
||||
|
||||
export const metadata: Metadata = {
|
||||
title: { template: "%s | Admin — DeckyVault", default: "Admin — DeckyVault" },
|
||||
robots: { index: false, follow: false },
|
||||
}
|
||||
|
||||
export default async function AdminLayout({
|
||||
children,
|
||||
}: {
|
||||
children: React.ReactNode
|
||||
}) {
|
||||
const session = await auth.api.getSession({
|
||||
headers: await headers(),
|
||||
})
|
||||
|
||||
if (!session || session.user.role !== "admin") {
|
||||
redirect("/")
|
||||
}
|
||||
|
||||
return (
|
||||
<section className="w-full flex flex-col gap-8 pb-16">
|
||||
<div className="px-4 md:px-[10svw]">
|
||||
<div className="max-w-7xl mx-auto">
|
||||
<h1 className="text-2xl sm:text-3xl font-bold">Admin</h1>
|
||||
<p className="text-sm text-text/60 mt-1">
|
||||
Manage users, hardware, and games
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="px-4 md:px-[10svw]">
|
||||
<div className="max-w-7xl mx-auto flex flex-col md:flex-row gap-6">
|
||||
<AdminSidebar />
|
||||
<div className="flex-1 min-w-0">{children}</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
"use client"
|
||||
|
||||
import Link from "next/link"
|
||||
import { usePathname } from "next/navigation"
|
||||
import { UsersIcon, CpuIcon, Gamepad2Icon } from "lucide-react"
|
||||
|
||||
const adminNavItems = [
|
||||
{ href: "/admin/users", label: "Users", icon: UsersIcon },
|
||||
{ href: "/admin/hardware", label: "Hardware", icon: CpuIcon },
|
||||
{ href: "/admin/games", label: "Games", icon: Gamepad2Icon },
|
||||
]
|
||||
|
||||
export function AdminSidebar() {
|
||||
const pathname = usePathname()
|
||||
|
||||
return (
|
||||
<nav className="md:w-48 shrink-0">
|
||||
<div className="flex md:flex-col gap-1 overflow-x-auto md:overflow-visible pb-2 md:pb-0 md:border-r md:border-border">
|
||||
{adminNavItems.map((item) => {
|
||||
const isActive =
|
||||
pathname === item.href || pathname.startsWith(item.href + "/")
|
||||
return (
|
||||
<Link
|
||||
key={item.href}
|
||||
href={item.href}
|
||||
className={`flex items-center gap-2 px-4 py-2.5 text-sm font-medium transition-colors whitespace-nowrap rounded-lg md:rounded-none md:border-l-2 md:border-r-0 md:border-transparent ${
|
||||
isActive
|
||||
? "bg-primary/10 text-primary md:border-l-primary"
|
||||
: "text-text/50 hover:text-text/70 hover:bg-text/5"
|
||||
}`}
|
||||
>
|
||||
<item.icon className="h-4 w-4 shrink-0" />
|
||||
{item.label}
|
||||
</Link>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
</nav>
|
||||
)
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import {
|
||||
Gamepad2Icon,
|
||||
LogOut,
|
||||
MenuIcon,
|
||||
ShieldIcon,
|
||||
User,
|
||||
XIcon,
|
||||
} from "lucide-react"
|
||||
@@ -302,6 +303,19 @@ export default function Navbar() {
|
||||
{route.title}
|
||||
</Link>
|
||||
))}
|
||||
{session.user.role === "admin" && (
|
||||
<>
|
||||
<div className='my-1 border-t border-white/10' />
|
||||
<Link
|
||||
href="/admin"
|
||||
onClick={() => setUserMenuOpen(false)}
|
||||
className="w-full flex items-center gap-2 px-3 py-2 text-sm text-primary/80 hover:text-primary hover:bg-primary/5 transition-colors cursor-pointer"
|
||||
>
|
||||
<ShieldIcon className="h-4 w-4" />
|
||||
Admin
|
||||
</Link>
|
||||
</>
|
||||
)}
|
||||
<div className='my-1 border-t border-white/10' />
|
||||
<button
|
||||
onClick={async () => {
|
||||
@@ -419,6 +433,16 @@ export default function Navbar() {
|
||||
{route.title}
|
||||
</Link>
|
||||
))}
|
||||
{session.user.role === "admin" && (
|
||||
<Link
|
||||
href="/admin"
|
||||
onClick={() => setMobileMenuOpen(false)}
|
||||
className="flex items-center gap-2 px-3 py-2 rounded-lg text-sm text-primary/80 hover:text-primary hover:bg-primary/5 transition-colors cursor-pointer"
|
||||
>
|
||||
<ShieldIcon className="h-4 w-4" />
|
||||
Admin
|
||||
</Link>
|
||||
)}
|
||||
<button
|
||||
onClick={async () => {
|
||||
setMobileMenuOpen(false)
|
||||
|
||||
@@ -39,4 +39,22 @@ export const authRoutes = [
|
||||
href: "/profile?tab=saved",
|
||||
icon: "Bookmark",
|
||||
},
|
||||
]
|
||||
|
||||
export const adminRoutes = [
|
||||
{
|
||||
title: "Users",
|
||||
href: "/admin/users",
|
||||
icon: "Users",
|
||||
},
|
||||
{
|
||||
title: "Hardware",
|
||||
href: "/admin/hardware",
|
||||
icon: "Cpu",
|
||||
},
|
||||
{
|
||||
title: "Games",
|
||||
href: "/admin/games",
|
||||
icon: "Gamepad2",
|
||||
},
|
||||
]
|
||||
Reference in New Issue
Block a user