{
  "id": "create-an-admin-dashboard",
  "type": "prompts",
  "category": "prompts",
  "locale": "en",
  "url": "/prompts/create-an-admin-dashboard",
  "title": "Prompt to Create an Admin Dashboard in Next.js",
  "description": "AI agent prompt to build a protected Next.js admin dashboard with data tables, server-side auth guards, and a sidebar layout using Tailwind.",
  "tools": [
    "Cursor",
    "Claude Code",
    "Codex",
    "Windsurf"
  ],
  "stack": [
    "Next.js",
    "PostgreSQL",
    "TypeScript",
    "Tailwind"
  ],
  "tags": [
    "nextjs",
    "tailwind",
    "typescript",
    "auth",
    "postgres"
  ],
  "difficulty": "hard",
  "updated": "2026-06-08",
  "markdown": "Give this prompt to your agent to scaffold a role-gated admin area with a persistent\nsidebar, sortable data tables, and server-side auth checks — without it scaffolding\na full new Next.js project or installing a bloated admin framework.\n\n## Main Prompt\n\n```txt title=\"Main Prompt\"\nYou are working in an existing Next.js App Router project with TypeScript, Tailwind CSS v4,\nand PostgreSQL. Auth is already set up (assume a `getSession()` helper exists in `src/lib/get-session.ts`\nthat returns `{ user: { id, email, role } } | null`).\n\nTask: add an admin dashboard at the `/admin` route group.\n\nRequirements:\n- Create a `src/app/(admin)/layout.tsx` that:\n  - Calls `getSession()` and redirects to `/login` if the session is null or `role !== 'admin'`.\n  - Renders a persistent sidebar with links: Dashboard, Users, Settings.\n  - Uses Tailwind for layout — a fixed-width sidebar and a scrollable main area.\n- Create `src/app/(admin)/dashboard/page.tsx` with:\n  - Four KPI cards: Total Users, Active Today, Revenue (MRR), Churn Rate — all fetched from\n    PostgreSQL using parameterized queries via `postgres` npm package.\n  - A `<UsersTable>` server component that renders the 50 most recent users (id, email, role,\n    created_at) with sortable column headers (URL-based sort param).\n- Create `src/components/admin/KpiCard.tsx` and `src/components/admin/UsersTable.tsx`.\n- All DB queries must use parameterized placeholders — never string interpolation.\n- Do not install Prisma, Drizzle, or any ORM. Use the `postgres` package directly.\n- Do not use any pre-built admin UI library (Refine, AdminJS, etc.).\n\nStop and list all planned file changes before writing any code.\n```\n\n## Implementation Notes\n\n- The `(admin)` route group parentheses keep `/admin/dashboard` out of the URL while sharing the\n  layout — the agent sometimes forgets the parentheses and creates a literal `/admin` segment.\n- Sortable columns should use `?sort=email&dir=asc` URL params read in the Server Component via\n  `searchParams` prop — no client state needed.\n- KPI queries should be run in parallel with `Promise.all` to minimize DB round-trips.\n\n## Expected File Changes\n\n```txt\nsrc/app/(admin)/layout.tsx                  (new)\nsrc/app/(admin)/dashboard/page.tsx          (new)\nsrc/components/admin/KpiCard.tsx            (new)\nsrc/components/admin/UsersTable.tsx         (new)\nsrc/lib/queries/admin-stats.ts             (new)\n```\n\n## Acceptance Criteria\n\n- Visiting `/dashboard` as a non-admin user redirects to `/login`.\n- KPI cards show real values from PostgreSQL.\n- Users table is sorted by the `sort` + `dir` URL params server-side.\n- `bun run build` exits 0 with no type errors.\n\n## Test Commands\n\n```bash\nbun run typecheck\nbun run build\nbun run dev\n# log in as admin and visit /dashboard\n# visit /dashboard without auth — confirm redirect to /login\n# add ?sort=email&dir=asc to the URL and confirm table order\n```\n\n## Common AI Mistakes\n\n- Creating `src/app/admin/layout.tsx` instead of `src/app/(admin)/layout.tsx`.\n- Performing the auth check client-side, which is bypassable.\n- Using string interpolation in SQL queries: `` `SELECT * FROM users WHERE role = '${role}'` ``.\n- Fetching KPI stats sequentially instead of using `Promise.all`.\n\n## Fix Prompt\n\n```txt title=\"Fix Prompt\"\nThe admin layout does not redirect unauthenticated users, or the route group folder name is wrong.\nFix in order:\n1. Rename `src/app/admin/` to `src/app/(admin)/` — the parentheses are required for a route group.\n2. In `layout.tsx`, add the auth check at the very top using `getSession()` and `redirect('/login')`.\n3. Audit all SQL query strings and replace any interpolated variables with `$1`, `$2` placeholders.\nShow the corrected diff only.\n```"
}