{
  "id": "create-an-admin-dashboard",
  "type": "prompts",
  "category": "prompts",
  "locale": "zh",
  "url": "/zh/prompts/create-an-admin-dashboard",
  "title": "在 Next.js 中创建管理后台的提示词",
  "description": "AI 代理提示词，用于构建受保护的 Next.js 管理后台，包含数据表格、服务端认证守卫和使用 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": "将此提示词交给您的 AI 代理，用于搭建一个基于角色的管理区域，包含固定侧边栏、可排序数据表格和服务端认证检查——无需搭建一个完整的全新 Next.js 项目或安装臃肿的管理框架。\n\n## 主要提示词\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## 实现说明\n\n- `(admin)` 路由组的括号将 `/admin/dashboard` 从 URL 中隐藏，同时共享布局——AI 代理有时会忘记括号，从而创建字面上的 `/admin` 路径段。\n- 可排序列应使用 `?sort=email&dir=asc` URL 参数，在服务端组件中通过 `searchParams` 属性读取——无需客户端状态。\n- KPI 查询应使用 `Promise.all` 并行执行，以减少数据库往返次数。\n\n## 预期文件变更\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## 验收标准\n\n- 非管理员用户访问 `/dashboard` 时重定向到 `/login`。\n- KPI 卡片显示来自 PostgreSQL 的真实值。\n- 用户表按 `sort` 和 `dir` URL 参数在服务端排序。\n- `bun run build` 退出码为 0，无类型错误。\n\n## 测试命令\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## 常见 AI 错误\n\n- 创建了 `src/app/admin/layout.tsx` 而不是 `src/app/(admin)/layout.tsx`。\n- 在客户端执行认证检查，这可以被绕过。\n- 在 SQL 查询中使用了字符串插值：`` `SELECT * FROM users WHERE role = '${role}'` ``。\n- 依次获取 KPI 统计数据，而不是使用 `Promise.all`。\n\n## 修正提示词\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```"
}