create-mohit-next 1.0.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +96 -65
- package/index.js +195 -195
- package/lib/features.js +358 -0
- package/lib/scaffold.js +471 -0
- package/package.json +10 -6
- package/templates/common/_editorconfig +12 -0
- package/templates/common/_gitattributes +5 -0
- package/templates/common/_github/dependabot.yml +14 -0
- package/templates/common/_github/workflows/ci.yml +66 -0
- package/templates/common/_husky/commit-msg +1 -0
- package/templates/common/_husky/pre-commit +1 -0
- package/templates/common/_nvmrc +1 -0
- package/{template → templates/common}/_prettierignore +4 -0
- package/templates/common/commitlint.config.mjs +10 -0
- package/templates/next/README.md +328 -0
- package/{template → templates/next}/_env.example +10 -1
- package/{template → templates/next}/_gitignore +8 -0
- package/templates/next/e2e/rbac.spec.ts +69 -0
- package/templates/next/e2e/server.mjs +35 -0
- package/{template → templates/next}/eslint.config.mjs +12 -0
- package/{template → templates/next}/next.config.ts +2 -0
- package/{template → templates/next}/package.json +18 -3
- package/templates/next/playwright.config.ts +30 -0
- package/templates/next/scripts/seed.ts +111 -0
- package/templates/next/src/app/(app)/admin/audit/page.tsx +99 -0
- package/templates/next/src/app/(app)/admin/page.tsx +92 -0
- package/{template → templates/next}/src/app/(app)/dashboard/page.tsx +22 -29
- package/{template → templates/next}/src/app/(app)/error.tsx +7 -7
- package/{template → templates/next}/src/app/(app)/layout.tsx +25 -2
- package/templates/next/src/app/(app)/reports/page.tsx +128 -0
- package/templates/next/src/app/(app)/settings/page.tsx +66 -0
- package/templates/next/src/app/(auth)/forgot-password/page.tsx +17 -0
- package/templates/next/src/app/(auth)/layout.tsx +18 -0
- package/{template → templates/next}/src/app/(auth)/register/page.tsx +2 -4
- package/templates/next/src/app/(auth)/reset-password/page.tsx +18 -0
- package/templates/next/src/app/api/account/password/route.ts +45 -0
- package/templates/next/src/app/api/account/route.ts +25 -0
- package/templates/next/src/app/api/account/sessions/route.ts +12 -0
- package/templates/next/src/app/api/admin/audit/route.ts +13 -0
- package/{template → templates/next}/src/app/api/admin/users/[id]/route.ts +25 -4
- package/templates/next/src/app/api/auth/forgot-password/route.ts +47 -0
- package/{template → templates/next}/src/app/api/auth/login/route.ts +19 -2
- package/{template → templates/next}/src/app/api/auth/me/route.ts +4 -2
- package/{template → templates/next}/src/app/api/auth/register/route.ts +38 -4
- package/templates/next/src/app/api/auth/resend-verification/route.ts +25 -0
- package/templates/next/src/app/api/auth/reset-password/route.ts +43 -0
- package/templates/next/src/app/api/auth/verify-email/route.ts +29 -0
- package/{template → templates/next}/src/app/forbidden/page.tsx +11 -13
- package/{template → templates/next}/src/app/layout.tsx +9 -6
- package/templates/next/src/app/not-found.tsx +20 -0
- package/templates/next/src/app/opengraph-image.tsx +49 -0
- package/templates/next/src/app/page.tsx +165 -0
- package/templates/next/src/app/robots.ts +15 -0
- package/templates/next/src/app/sitemap.ts +13 -0
- package/templates/next/src/app/verify-email/page.tsx +23 -0
- package/templates/next/src/components/admin/user-actions.tsx +124 -0
- package/templates/next/src/components/admin/users-data-table.tsx +221 -0
- package/templates/next/src/components/admin/users-table.tsx +67 -0
- package/{template → templates/next}/src/components/auth/auth-card.tsx +8 -8
- package/templates/next/src/components/auth/forgot-password-form.tsx +76 -0
- package/templates/next/src/components/auth/form-field.tsx +40 -0
- package/{template → templates/next}/src/components/auth/login-form.tsx +14 -9
- package/templates/next/src/components/auth/password-input.tsx +43 -0
- package/{template → templates/next}/src/components/auth/register-form.tsx +22 -19
- package/templates/next/src/components/auth/reset-password-form.tsx +80 -0
- package/{template → templates/next}/src/components/auth/session-refresher.tsx +1 -1
- package/templates/next/src/components/auth/verify-email-banner.tsx +43 -0
- package/templates/next/src/components/auth/verify-email-status.tsx +44 -0
- package/templates/next/src/components/layout/nav-links.tsx +37 -0
- package/templates/next/src/components/layout/navbar.tsx +27 -0
- package/templates/next/src/components/layout/page-header.tsx +21 -0
- package/templates/next/src/components/layout/sidebar.tsx +83 -0
- package/templates/next/src/components/layout/skeletons.tsx +57 -0
- package/templates/next/src/components/layout/user-menu.tsx +118 -0
- package/{template → templates/next}/src/components/logo.tsx +3 -6
- package/{template → templates/next}/src/components/reports/animated-bar.tsx +1 -5
- package/templates/next/src/components/reports/role-chart.tsx +61 -0
- package/templates/next/src/components/reports/signups-chart.tsx +64 -0
- package/templates/next/src/components/role-badge.tsx +11 -0
- package/templates/next/src/components/settings/password-form.tsx +80 -0
- package/templates/next/src/components/settings/profile-form.tsx +63 -0
- package/templates/next/src/components/settings/sessions-card.tsx +70 -0
- package/templates/next/src/components/theme-toggle.tsx +61 -0
- package/templates/next/src/config/rbac.test.ts +89 -0
- package/{template → templates/next}/src/config/rbac.ts +35 -5
- package/{template → templates/next}/src/lib/api.ts +40 -4
- package/templates/next/src/lib/audit.ts +66 -0
- package/{template → templates/next}/src/lib/auth.ts +10 -2
- package/{template → templates/next}/src/lib/dal.ts +8 -5
- package/templates/next/src/lib/email-templates.ts +55 -0
- package/templates/next/src/lib/email.ts +53 -0
- package/templates/next/src/lib/env.ts +90 -0
- package/templates/next/src/lib/password-strength.test.ts +20 -0
- package/templates/next/src/lib/password-strength.ts +24 -0
- package/templates/next/src/lib/rate-limit.test.ts +47 -0
- package/templates/next/src/lib/rate-limit.ts +70 -0
- package/templates/next/src/lib/redirect.test.ts +23 -0
- package/{template → templates/next}/src/lib/session.ts +5 -1
- package/templates/next/src/lib/theme.ts +32 -0
- package/templates/next/src/lib/tokens.ts +63 -0
- package/{template → templates/next}/src/lib/users.ts +49 -12
- package/templates/next/src/lib/validations/account.ts +20 -0
- package/templates/next/src/lib/validations/auth.test.ts +39 -0
- package/{template → templates/next}/src/lib/validations/auth.ts +24 -2
- package/templates/next/src/models/audit-log.ts +33 -0
- package/templates/next/src/models/auth-token.ts +26 -0
- package/{template → templates/next}/src/models/user.ts +5 -0
- package/{template → templates/next}/src/proxy.ts +1 -1
- package/templates/next/src/styles/app.css +1092 -0
- package/{template → templates/next}/src/types/user.ts +3 -0
- package/templates/next/vitest.config.mts +13 -0
- package/templates/styles/css.css +580 -0
- package/templates/styles/tailwind.css +140 -0
- package/templates/ui/css/components/alert-dialog.tsx +172 -0
- package/templates/ui/css/components/avatar.tsx +17 -0
- package/templates/ui/css/components/badge.tsx +18 -0
- package/templates/ui/css/components/button.tsx +41 -0
- package/templates/ui/css/components/card.tsx +33 -0
- package/templates/ui/css/components/dropdown-menu.tsx +230 -0
- package/templates/ui/css/components/input.tsx +7 -0
- package/templates/ui/css/components/label.tsx +7 -0
- package/templates/ui/css/components/native-select.tsx +23 -0
- package/templates/ui/css/components/skeleton.tsx +7 -0
- package/templates/ui/css/components/slot.tsx +30 -0
- package/templates/ui/css/components/table.tsx +31 -0
- package/templates/ui/css/utils.ts +16 -0
- package/templates/ui/shadcn/components/alert-dialog.tsx +199 -0
- package/templates/ui/shadcn/components/avatar.tsx +111 -0
- package/templates/ui/shadcn/components/dropdown-menu.tsx +268 -0
- package/templates/ui/shadcn/components/table.tsx +115 -0
- package/templates/ui/tailwind/components/alert-dialog.tsx +177 -0
- package/templates/ui/tailwind/components/avatar.tsx +36 -0
- package/templates/ui/tailwind/components/badge.tsx +29 -0
- package/templates/ui/tailwind/components/button.tsx +65 -0
- package/templates/ui/tailwind/components/card.tsx +70 -0
- package/templates/ui/tailwind/components/dropdown-menu.tsx +236 -0
- package/templates/ui/tailwind/components/input.tsx +17 -0
- package/templates/ui/tailwind/components/label.tsx +16 -0
- package/templates/ui/tailwind/components/native-select.tsx +32 -0
- package/templates/ui/tailwind/components/skeleton.tsx +13 -0
- package/templates/ui/tailwind/components/slot.tsx +34 -0
- package/templates/ui/tailwind/components/table.tsx +56 -0
- package/templates/ui/tailwind/utils.ts +1 -0
- package/templates/vite/Dockerfile +29 -0
- package/templates/vite/README.md +142 -0
- package/templates/vite/_dockerignore +14 -0
- package/templates/vite/_env.example +8 -0
- package/templates/vite/_gitignore +37 -0
- package/templates/vite/e2e/app.spec.ts +41 -0
- package/templates/vite/index.html +32 -0
- package/templates/vite/nginx.conf +26 -0
- package/templates/vite/package.json +61 -0
- package/templates/vite/playwright.config.ts +27 -0
- package/templates/vite/public/favicon.svg +1 -0
- package/templates/vite/src/App.test.tsx +28 -0
- package/templates/vite/src/App.tsx +26 -0
- package/templates/vite/src/components/home.tsx +121 -0
- package/templates/vite/src/components/layout/root-layout.tsx +18 -0
- package/templates/vite/src/components/layout/site-footer.tsx +10 -0
- package/templates/vite/src/components/layout/site-header.tsx +49 -0
- package/templates/vite/src/components/theme-toggle.tsx +90 -0
- package/templates/vite/src/env.ts +36 -0
- package/templates/vite/src/lib/theme.ts +26 -0
- package/templates/vite/src/main.tsx +13 -0
- package/templates/vite/src/pages/about.tsx +52 -0
- package/templates/vite/src/pages/not-found.tsx +33 -0
- package/templates/vite/src/router.ts +24 -0
- package/templates/vite/src/styles/app.css +281 -0
- package/templates/vite/src/test/setup.ts +21 -0
- package/templates/vite/tsconfig.app.json +31 -0
- package/templates/vite/tsconfig.json +7 -0
- package/templates/vite/tsconfig.node.json +23 -0
- package/templates/vite/vite.config.ts +30 -0
- package/template/README.md +0 -262
- package/template/_prettierrc +0 -9
- package/template/components.json +0 -25
- package/template/src/app/(app)/admin/page.tsx +0 -53
- package/template/src/app/(app)/reports/page.tsx +0 -109
- package/template/src/app/(auth)/layout.tsx +0 -22
- package/template/src/app/not-found.tsx +0 -18
- package/template/src/app/page.tsx +0 -187
- package/template/src/components/admin/users-table.tsx +0 -162
- package/template/src/components/auth/form-field.tsx +0 -32
- package/template/src/components/layout/logout-button.tsx +0 -34
- package/template/src/components/layout/navbar.tsx +0 -67
- package/template/src/components/layout/page-header.tsx +0 -21
- package/template/src/components/layout/skeletons.tsx +0 -35
- package/template/src/components/role-badge.tsx +0 -17
- package/template/src/components/theme-toggle.tsx +0 -25
- package/template/src/lib/env.ts +0 -36
- package/template/src/lib/theme.ts +0 -8
- /package/{template → templates/next}/AGENTS.md +0 -0
- /package/{template → templates/next}/Dockerfile +0 -0
- /package/{template → templates/next}/_dockerignore +0 -0
- /package/{template → templates/next}/docker-compose.yml +0 -0
- /package/{template → templates/next}/public/robots.txt +0 -0
- /package/{template → templates/next}/src/app/(app)/loading.tsx +0 -0
- /package/{template → templates/next}/src/app/(auth)/login/page.tsx +0 -0
- /package/{template → templates/next}/src/app/api/admin/users/route.ts +0 -0
- /package/{template → templates/next}/src/app/api/auth/logout/route.ts +0 -0
- /package/{template → templates/next}/src/app/api/health/route.ts +0 -0
- /package/{template → templates/next}/src/app/favicon.ico +0 -0
- /package/{template → templates/next}/src/components/auth/can.tsx +0 -0
- /package/{template → templates/next}/src/components/motion/fade-in.tsx +0 -0
- /package/{template → templates/next}/src/components/providers/user-provider.tsx +0 -0
- /package/{template → templates/next}/src/config/site.ts +0 -0
- /package/{template → templates/next}/src/lib/db.ts +0 -0
- /package/{template → templates/next}/src/lib/http.ts +0 -0
- /package/{template → templates/next}/src/lib/redirect.ts +0 -0
- /package/{template → templates/next}/src/lib/validations/user.ts +0 -0
- /package/{template → templates/next}/tsconfig.json +0 -0
- /package/{template/src/app/globals.css → templates/styles/shadcn.css} +0 -0
- /package/{template/src/components/ui → templates/ui/shadcn/components}/badge.tsx +0 -0
- /package/{template/src/components/ui → templates/ui/shadcn/components}/button.tsx +0 -0
- /package/{template/src/components/ui → templates/ui/shadcn/components}/card.tsx +0 -0
- /package/{template/src/components/ui → templates/ui/shadcn/components}/input.tsx +0 -0
- /package/{template/src/components/ui → templates/ui/shadcn/components}/label.tsx +0 -0
- /package/{template/src/components/ui → templates/ui/shadcn/components}/native-select.tsx +0 -0
- /package/{template/src/components/ui → templates/ui/shadcn/components}/skeleton.tsx +0 -0
- /package/{template/src/lib → templates/ui/shadcn}/utils.ts +0 -0
package/template/README.md
DELETED
|
@@ -1,262 +0,0 @@
|
|
|
1
|
-
# {{PROJECT_TITLE}}
|
|
2
|
-
|
|
3
|
-
A full-stack **Next.js 16** + **TypeScript** app with MongoDB, JWT authentication and
|
|
4
|
-
role-based access control (RBAC), scaffolded with `create-mohit-next`.
|
|
5
|
-
|
|
6
|
-
| Area | Stack |
|
|
7
|
-
| ---------- | ----------------------------------------------------------------------- |
|
|
8
|
-
| Framework | Next.js 16 (App Router, Turbopack, Cache Components), React 19 |
|
|
9
|
-
| UI | Tailwind CSS v4, shadcn/ui (Radix), lucide-react, motion, sonner toasts |
|
|
10
|
-
| Data | MongoDB with Mongoose (cached connection) |
|
|
11
|
-
| Auth | bcryptjs (cost 12) + JWT (`jose`, HS256) in an httpOnly cookie |
|
|
12
|
-
| Validation | zod v4 (env vars + request/form schemas shared by client and server) |
|
|
13
|
-
| Forms | react-hook-form + @hookform/resolvers |
|
|
14
|
-
| Tooling | ESLint, Prettier (+ Tailwind class sorting), `typecheck` script |
|
|
15
|
-
|
|
16
|
-
## Getting started
|
|
17
|
-
|
|
18
|
-
**Requirements:** Node.js 20.19+ and a MongoDB database.
|
|
19
|
-
|
|
20
|
-
```bash
|
|
21
|
-
npm install # if you skipped it during scaffolding
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
1. **Environment.** `.env.local` was generated for you with a random `JWT_SECRET`. To recreate it,
|
|
25
|
-
copy `.env.example` to `.env.local` and fill in:
|
|
26
|
-
|
|
27
|
-
| Variable | Description |
|
|
28
|
-
| ------------- | -------------------------------------------------------------- |
|
|
29
|
-
| `MONGODB_URI` | e.g. `mongodb://localhost:27017/my-app` or a MongoDB Atlas URI |
|
|
30
|
-
| `JWT_SECRET` | At least 32 random characters, used to sign session tokens |
|
|
31
|
-
|
|
32
|
-
Variables are validated with zod the first time they are used, so `next build` works without them.
|
|
33
|
-
|
|
34
|
-
2. **Database.** Use any MongoDB 6+ instance (a local install or MongoDB Atlas) and point
|
|
35
|
-
`MONGODB_URI` at it.
|
|
36
|
-
|
|
37
|
-
3. **Run it.**
|
|
38
|
-
|
|
39
|
-
```bash
|
|
40
|
-
npm run dev
|
|
41
|
-
```
|
|
42
|
-
|
|
43
|
-
Open http://localhost:3000 and register. **The first account becomes `admin`**; every account
|
|
44
|
-
after that starts as `user`. Promote people from **/admin**.
|
|
45
|
-
|
|
46
|
-
### Scripts
|
|
47
|
-
|
|
48
|
-
| Script | What it does |
|
|
49
|
-
| ---------------------- | ----------------------------------------- |
|
|
50
|
-
| `npm run dev` | Dev server with Turbopack |
|
|
51
|
-
| `npm run build` | Production build (`.next/standalone`) |
|
|
52
|
-
| `npm run start` | Serve the production build |
|
|
53
|
-
| `npm run lint` | ESLint |
|
|
54
|
-
| `npm run typecheck` | Generate route types, then `tsc --noEmit` |
|
|
55
|
-
| `npm run format` | Prettier (with Tailwind class sorting) |
|
|
56
|
-
| `npm run format:check` | Check formatting (CI) |
|
|
57
|
-
|
|
58
|
-
<!-- docker:start -->
|
|
59
|
-
|
|
60
|
-
## Docker
|
|
61
|
-
|
|
62
|
-
| Script | What it does |
|
|
63
|
-
| --------------------- | --------------------------------------------- |
|
|
64
|
-
| `npm run docker:db` | Start only MongoDB (for `npm run dev`) |
|
|
65
|
-
| `npm run docker:up` | Build the image and start the app + MongoDB |
|
|
66
|
-
| `npm run docker:down` | Stop the containers (the data volume is kept) |
|
|
67
|
-
|
|
68
|
-
- The `Dockerfile` is multi-stage: deps → build → a slim runtime containing only `.next/standalone`,
|
|
69
|
-
running as a non-root user with a `HEALTHCHECK` on `/api/health`.
|
|
70
|
-
- `docker-compose.yml` reads `JWT_SECRET` from `.env.local` and points `MONGODB_URI` at the `mongo`
|
|
71
|
-
service. Data persists in the `mongo-data` volume.
|
|
72
|
-
- In production (`NODE_ENV=production`) the session cookie is `Secure`, so serve the app over HTTPS
|
|
73
|
-
(browsers allow it on `http://localhost`).
|
|
74
|
-
|
|
75
|
-
<!-- docker:end -->
|
|
76
|
-
|
|
77
|
-
## Folder structure
|
|
78
|
-
|
|
79
|
-
```
|
|
80
|
-
src/
|
|
81
|
-
├── app/
|
|
82
|
-
│ ├── (auth)/ # /login, /register (guest-only)
|
|
83
|
-
│ ├── (app)/ # authenticated shell: navbar + pages
|
|
84
|
-
│ │ ├── layout.tsx # loads the user inside <Suspense>, provides UserProvider
|
|
85
|
-
│ │ ├── dashboard/ # any signed-in user
|
|
86
|
-
│ │ ├── reports/ # manager+
|
|
87
|
-
│ │ └── admin/ # manager (read-only) / admin (manage)
|
|
88
|
-
│ ├── api/
|
|
89
|
-
│ │ ├── auth/ # register, login, logout, me
|
|
90
|
-
│ │ ├── admin/users/ # list, PATCH role, DELETE
|
|
91
|
-
│ │ └── health/
|
|
92
|
-
│ ├── forbidden/ # 403 page
|
|
93
|
-
│ ├── layout.tsx # root layout, theme script, toaster
|
|
94
|
-
│ └── page.tsx # landing page
|
|
95
|
-
├── components/
|
|
96
|
-
│ ├── ui/ # shadcn/ui components (add more with `npx shadcn add`)
|
|
97
|
-
│ ├── auth/ # <Can>, login/register forms
|
|
98
|
-
│ ├── providers/ # UserProvider + useUser()
|
|
99
|
-
│ └── ...
|
|
100
|
-
├── config/
|
|
101
|
-
│ ├── rbac.ts # ★ roles, permissions, route rules, nav (single source of truth)
|
|
102
|
-
│ └── site.ts
|
|
103
|
-
├── lib/
|
|
104
|
-
│ ├── api.ts # withAuth(), ApiError, parseBody()
|
|
105
|
-
│ ├── auth.ts # password hashing, session cookie helpers
|
|
106
|
-
│ ├── dal.ts # getCurrentUser(), requireUser(), requirePermission()
|
|
107
|
-
│ ├── db.ts # cached Mongoose connection
|
|
108
|
-
│ ├── env.ts # lazy zod-validated env
|
|
109
|
-
│ ├── session.ts # JWT sign/verify (jose)
|
|
110
|
-
│ ├── users.ts # user queries
|
|
111
|
-
│ └── validations/ # zod schemas shared by client + server
|
|
112
|
-
├── models/user.ts # Mongoose model
|
|
113
|
-
└── proxy.ts # route protection (Next.js 16's replacement for middleware.ts)
|
|
114
|
-
```
|
|
115
|
-
|
|
116
|
-
## How RBAC works
|
|
117
|
-
|
|
118
|
-
Everything starts in [`src/config/rbac.ts`](src/config/rbac.ts):
|
|
119
|
-
|
|
120
|
-
```ts
|
|
121
|
-
ROLES = ["user", "manager", "admin"] // lowest → highest
|
|
122
|
-
ROLE_PERMISSIONS = {
|
|
123
|
-
user: ["dashboard:view"],
|
|
124
|
-
manager: ["dashboard:view", "reports:view", "users:read"],
|
|
125
|
-
admin: [...everything, "users:update-role", "users:delete"],
|
|
126
|
-
}
|
|
127
|
-
ROUTE_RULES = [{ prefix: "/reports", permission: "reports:view" }, ...] // longest prefix wins
|
|
128
|
-
```
|
|
129
|
-
|
|
130
|
-
Each request is checked in **three layers**:
|
|
131
|
-
|
|
132
|
-
| Layer | Where | Checks | Why |
|
|
133
|
-
| ----- | -------------------------------- | ----------------------- | ---------------------------------------------------------------------------------------------------------- |
|
|
134
|
-
| 1 | `src/proxy.ts` | The JWT's role | Fast redirects: signed out → `/login?next=…`, no permission → `/forbidden`, signed in → away from `/login` |
|
|
135
|
-
| 2 | `src/lib/dal.ts` | The user **in MongoDB** | Source of truth: role changes and deleted accounts apply on the next request |
|
|
136
|
-
| 3 | `withAuth()` in `src/lib/api.ts` | The user **in MongoDB** | Route handlers answer `401`/`403` JSON |
|
|
137
|
-
|
|
138
|
-
On the client, `useUser()` and `<Can permission="…">` hide UI the user can't use. That's only
|
|
139
|
-
cosmetic: the server always re-checks.
|
|
140
|
-
|
|
141
|
-
**Stale tokens.** The JWT stores the role so the proxy can stay database-free. After an admin
|
|
142
|
-
changes someone's role, layers 2 and 3 use the new role immediately. The app shell notices that
|
|
143
|
-
the token is out of date and calls `GET /api/auth/me`, which re-issues the cookie. The `/forbidden`
|
|
144
|
-
page does the same, so a freshly promoted user can just retry.
|
|
145
|
-
|
|
146
|
-
### Add a role
|
|
147
|
-
|
|
148
|
-
```ts
|
|
149
|
-
// src/config/rbac.ts
|
|
150
|
-
export const ROLES = ["user", "editor", "manager", "admin"] as const; // keep the order
|
|
151
|
-
|
|
152
|
-
export const ROLE_PERMISSIONS = {
|
|
153
|
-
user: ["dashboard:view"],
|
|
154
|
-
editor: ["dashboard:view", "reports:view"],
|
|
155
|
-
// ...
|
|
156
|
-
};
|
|
157
|
-
// Also add a label to ROLE_LABELS (TypeScript will flag any map you forget).
|
|
158
|
-
```
|
|
159
|
-
|
|
160
|
-
The Mongoose enum, zod schemas and the admin role selector all read from `ROLES`, so nothing else
|
|
161
|
-
needs to change. (Optionally add a color in `src/components/role-badge.tsx`.)
|
|
162
|
-
|
|
163
|
-
### Add a permission
|
|
164
|
-
|
|
165
|
-
```ts
|
|
166
|
-
export const PERMISSIONS = [..., "billing:manage"] as const;
|
|
167
|
-
export const PERMISSION_LABELS = { ..., "billing:manage": "Manage billing" };
|
|
168
|
-
export const ROLE_PERMISSIONS = { ..., admin: [..., "billing:manage"] };
|
|
169
|
-
```
|
|
170
|
-
|
|
171
|
-
### Protect a page
|
|
172
|
-
|
|
173
|
-
1. Add a route rule (layer 1) and, optionally, a nav item:
|
|
174
|
-
|
|
175
|
-
```ts
|
|
176
|
-
ROUTE_RULES = [..., { prefix: "/billing", permission: "billing:manage" }];
|
|
177
|
-
NAV_ITEMS = [..., { href: "/billing", label: "Billing", permission: "billing:manage" }];
|
|
178
|
-
```
|
|
179
|
-
|
|
180
|
-
2. Create `src/app/(app)/billing/page.tsx` and check on the server (layer 2):
|
|
181
|
-
|
|
182
|
-
```tsx
|
|
183
|
-
import { requirePermission } from "@/lib/dal";
|
|
184
|
-
|
|
185
|
-
export default async function BillingPage() {
|
|
186
|
-
const user = await requirePermission("billing:manage"); // redirects to /forbidden otherwise
|
|
187
|
-
return <h1>Billing for {user.name}</h1>;
|
|
188
|
-
}
|
|
189
|
-
```
|
|
190
|
-
|
|
191
|
-
Pages inside `(app)` already render inside a `<Suspense>` boundary (the layout and
|
|
192
|
-
`loading.tsx`), which Cache Components requires for anything that reads cookies or the database.
|
|
193
|
-
If you read the user somewhere else, wrap that component in `<Suspense>` and don't `await` it
|
|
194
|
-
at a layout's top level.
|
|
195
|
-
|
|
196
|
-
3. Hide or show UI with `<Can>`:
|
|
197
|
-
|
|
198
|
-
```tsx
|
|
199
|
-
<Can permission="billing:manage" fallback={<p>Read only</p>}>
|
|
200
|
-
<Button>Edit plan</Button>
|
|
201
|
-
</Can>
|
|
202
|
-
```
|
|
203
|
-
|
|
204
|
-
### Protect an API route
|
|
205
|
-
|
|
206
|
-
```ts
|
|
207
|
-
// src/app/api/invoices/[id]/route.ts
|
|
208
|
-
import { NextResponse } from "next/server";
|
|
209
|
-
import { ApiError, parseBody, withAuth } from "@/lib/api";
|
|
210
|
-
|
|
211
|
-
export const PATCH = withAuth<RouteContext<"/api/invoices/[id]">>(
|
|
212
|
-
async (req, ctx, user) => {
|
|
213
|
-
const { id } = await ctx.params; // params are a Promise in Next.js 16
|
|
214
|
-
const body = await parseBody(req, invoiceSchema); // 400 bad JSON / 422 validation
|
|
215
|
-
if (!invoice) throw ApiError.notFound("Invoice not found");
|
|
216
|
-
return NextResponse.json({ invoice });
|
|
217
|
-
},
|
|
218
|
-
{ permission: "billing:manage" }, // 401 if signed out, 403 without permission
|
|
219
|
-
);
|
|
220
|
-
```
|
|
221
|
-
|
|
222
|
-
Errors always use one JSON shape: `{ "error": { "code": "FORBIDDEN", "message": "...", "details"?: ... } }`.
|
|
223
|
-
|
|
224
|
-
## API
|
|
225
|
-
|
|
226
|
-
| Method | Route | Access | Notes |
|
|
227
|
-
| ------ | ---------------------- | ------------------- | -------------------------------------------------- |
|
|
228
|
-
| POST | `/api/auth/register` | public | `{ name, email, password }`; first user → admin |
|
|
229
|
-
| POST | `/api/auth/login` | public | `{ email, password }`; generic error on failure |
|
|
230
|
-
| POST | `/api/auth/logout` | public | Clears the session cookie |
|
|
231
|
-
| GET | `/api/auth/me` | signed in | Current user + permissions; refreshes stale tokens |
|
|
232
|
-
| GET | `/api/admin/users` | `users:read` | `?page=1&limit=50` |
|
|
233
|
-
| PATCH | `/api/admin/users/:id` | `users:update-role` | `{ role }`; you can't change your own role |
|
|
234
|
-
| DELETE | `/api/admin/users/:id` | `users:delete` | You can't delete yourself |
|
|
235
|
-
| GET | `/api/health` | public | `200` when the database is reachable, else `503` |
|
|
236
|
-
|
|
237
|
-
## Security notes
|
|
238
|
-
|
|
239
|
-
- Passwords are hashed with bcrypt (cost 12). Login always runs one bcrypt comparison (against a
|
|
240
|
-
dummy hash for unknown emails) and returns the same error, so responses don't reveal which emails exist.
|
|
241
|
-
- Session cookie: `httpOnly`, `SameSite=Lax`, `Secure` in production, expires after 7 days.
|
|
242
|
-
- `?next=` redirects only accept same-origin relative paths.
|
|
243
|
-
- Security headers (`X-Frame-Options`, `X-Content-Type-Options`, `Referrer-Policy`,
|
|
244
|
-
`Permissions-Policy`, HSTS) are set in `next.config.ts`. Add a Content-Security-Policy once you
|
|
245
|
-
know which origins you need.
|
|
246
|
-
- The "first user becomes admin" claim is atomic (a unique `_id` in the `app_meta` collection), so
|
|
247
|
-
concurrent sign-ups on an empty database can't create two admins.
|
|
248
|
-
- Before going live, consider adding rate limiting to `/api/auth/*`.
|
|
249
|
-
|
|
250
|
-
## Adding UI components
|
|
251
|
-
|
|
252
|
-
shadcn/ui is configured (`components.json`, style `radix-nova`), so you can add more components:
|
|
253
|
-
|
|
254
|
-
```bash
|
|
255
|
-
npx shadcn@latest add dialog dropdown-menu table
|
|
256
|
-
```
|
|
257
|
-
|
|
258
|
-
## Learn more
|
|
259
|
-
|
|
260
|
-
This project uses Next.js 16, which differs from older versions (for example, `middleware.ts` is
|
|
261
|
-
now `proxy.ts`, and `params` are Promises). Its docs ship with the package in
|
|
262
|
-
`node_modules/next/dist/docs/`; see `AGENTS.md`.
|
package/template/_prettierrc
DELETED
package/template/components.json
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"$schema": "https://ui.shadcn.com/schema.json",
|
|
3
|
-
"style": "radix-nova",
|
|
4
|
-
"rsc": true,
|
|
5
|
-
"tsx": true,
|
|
6
|
-
"tailwind": {
|
|
7
|
-
"config": "",
|
|
8
|
-
"css": "src/app/globals.css",
|
|
9
|
-
"baseColor": "neutral",
|
|
10
|
-
"cssVariables": true,
|
|
11
|
-
"prefix": ""
|
|
12
|
-
},
|
|
13
|
-
"iconLibrary": "lucide",
|
|
14
|
-
"rtl": false,
|
|
15
|
-
"aliases": {
|
|
16
|
-
"components": "@/components",
|
|
17
|
-
"utils": "@/lib/utils",
|
|
18
|
-
"ui": "@/components/ui",
|
|
19
|
-
"lib": "@/lib",
|
|
20
|
-
"hooks": "@/hooks"
|
|
21
|
-
},
|
|
22
|
-
"menuColor": "default",
|
|
23
|
-
"menuAccent": "subtle",
|
|
24
|
-
"registries": {}
|
|
25
|
-
}
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import { Eye } from "lucide-react";
|
|
2
|
-
import type { Metadata } from "next";
|
|
3
|
-
|
|
4
|
-
import { UsersTable } from "@/components/admin/users-table";
|
|
5
|
-
import { Can } from "@/components/auth/can";
|
|
6
|
-
import { PageHeader } from "@/components/layout/page-header";
|
|
7
|
-
import { Badge } from "@/components/ui/badge";
|
|
8
|
-
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
|
|
9
|
-
import { requirePermission } from "@/lib/dal";
|
|
10
|
-
import { listUsers } from "@/lib/users";
|
|
11
|
-
|
|
12
|
-
export const metadata: Metadata = { title: "Admin" };
|
|
13
|
-
|
|
14
|
-
export default async function AdminPage() {
|
|
15
|
-
// Managers (users:read) can view; mutations need users:update-role / users:delete,
|
|
16
|
-
// which are enforced again by the API routes.
|
|
17
|
-
const user = await requirePermission("users:read");
|
|
18
|
-
const { users, total } = await listUsers({ limit: 100 });
|
|
19
|
-
|
|
20
|
-
return (
|
|
21
|
-
<>
|
|
22
|
-
<PageHeader
|
|
23
|
-
title="User management"
|
|
24
|
-
description="Everyone with an account. Role changes apply on the user's next request."
|
|
25
|
-
actions={
|
|
26
|
-
<Can
|
|
27
|
-
permission="users:update-role"
|
|
28
|
-
fallback={
|
|
29
|
-
<Badge variant="outline" className="gap-1">
|
|
30
|
-
<Eye /> Read-only access
|
|
31
|
-
</Badge>
|
|
32
|
-
}
|
|
33
|
-
>
|
|
34
|
-
<Badge variant="outline">Full access</Badge>
|
|
35
|
-
</Can>
|
|
36
|
-
}
|
|
37
|
-
/>
|
|
38
|
-
|
|
39
|
-
<Card>
|
|
40
|
-
<CardHeader>
|
|
41
|
-
<CardTitle>Users</CardTitle>
|
|
42
|
-
<CardDescription>
|
|
43
|
-
{total} {total === 1 ? "account" : "accounts"}
|
|
44
|
-
{total > users.length && ` (showing the ${users.length} most recent)`}
|
|
45
|
-
</CardDescription>
|
|
46
|
-
</CardHeader>
|
|
47
|
-
<CardContent className="px-0">
|
|
48
|
-
<UsersTable users={users} currentUserId={user.id} />
|
|
49
|
-
</CardContent>
|
|
50
|
-
</Card>
|
|
51
|
-
</>
|
|
52
|
-
);
|
|
53
|
-
}
|
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
import { Crown, ShieldCheck, UserPlus, Users } from "lucide-react";
|
|
2
|
-
import type { Metadata } from "next";
|
|
3
|
-
|
|
4
|
-
import { PageHeader } from "@/components/layout/page-header";
|
|
5
|
-
import { FadeIn } from "@/components/motion/fade-in";
|
|
6
|
-
import { AnimatedBar } from "@/components/reports/animated-bar";
|
|
7
|
-
import { RoleBadge } from "@/components/role-badge";
|
|
8
|
-
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
|
|
9
|
-
import { ROLES } from "@/config/rbac";
|
|
10
|
-
import { requirePermission } from "@/lib/dal";
|
|
11
|
-
import { getUserStats } from "@/lib/users";
|
|
12
|
-
|
|
13
|
-
export const metadata: Metadata = { title: "Reports" };
|
|
14
|
-
|
|
15
|
-
const dayFormat = new Intl.DateTimeFormat("en-US", { weekday: "short", timeZone: "UTC" });
|
|
16
|
-
|
|
17
|
-
export default async function ReportsPage() {
|
|
18
|
-
await requirePermission("reports:view");
|
|
19
|
-
const stats = await getUserStats();
|
|
20
|
-
|
|
21
|
-
const cards = [
|
|
22
|
-
{ label: "Total users", value: stats.total, icon: Users },
|
|
23
|
-
{ label: "New in the last 7 days", value: stats.newLast7Days, icon: UserPlus },
|
|
24
|
-
{ label: "Managers", value: stats.byRole.manager, icon: ShieldCheck },
|
|
25
|
-
{ label: "Admins", value: stats.byRole.admin, icon: Crown },
|
|
26
|
-
];
|
|
27
|
-
const maxDaily = Math.max(1, ...stats.signupsByDay.map((d) => d.count));
|
|
28
|
-
|
|
29
|
-
return (
|
|
30
|
-
<>
|
|
31
|
-
<PageHeader title="Reports" description="Sample analytics, visible to managers and admins." />
|
|
32
|
-
|
|
33
|
-
<div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-4">
|
|
34
|
-
{cards.map(({ label, value, icon: Icon }, i) => (
|
|
35
|
-
<FadeIn key={label} delay={i * 0.04}>
|
|
36
|
-
<Card>
|
|
37
|
-
<CardHeader>
|
|
38
|
-
<CardDescription className="flex items-center justify-between">
|
|
39
|
-
{label} <Icon className="size-4" />
|
|
40
|
-
</CardDescription>
|
|
41
|
-
<CardTitle className="text-3xl tabular-nums">{value}</CardTitle>
|
|
42
|
-
</CardHeader>
|
|
43
|
-
</Card>
|
|
44
|
-
</FadeIn>
|
|
45
|
-
))}
|
|
46
|
-
</div>
|
|
47
|
-
|
|
48
|
-
<div className="mt-4 grid gap-4 lg:grid-cols-2">
|
|
49
|
-
<Card>
|
|
50
|
-
<CardHeader>
|
|
51
|
-
<CardTitle>Users by role</CardTitle>
|
|
52
|
-
<CardDescription>Share of all accounts.</CardDescription>
|
|
53
|
-
</CardHeader>
|
|
54
|
-
<CardContent className="space-y-4">
|
|
55
|
-
{ROLES.map((role, i) => {
|
|
56
|
-
const count = stats.byRole[role];
|
|
57
|
-
const pct = stats.total ? (count / stats.total) * 100 : 0;
|
|
58
|
-
return (
|
|
59
|
-
<div key={role} className="space-y-1.5">
|
|
60
|
-
<div className="flex items-center justify-between text-sm">
|
|
61
|
-
<RoleBadge role={role} />
|
|
62
|
-
<span className="text-muted-foreground tabular-nums">
|
|
63
|
-
{count} · {Math.round(pct)}%
|
|
64
|
-
</span>
|
|
65
|
-
</div>
|
|
66
|
-
<div className="h-2 overflow-hidden rounded-md bg-muted">
|
|
67
|
-
<AnimatedBar value={pct} delay={i * 0.1} className="h-full" />
|
|
68
|
-
</div>
|
|
69
|
-
</div>
|
|
70
|
-
);
|
|
71
|
-
})}
|
|
72
|
-
</CardContent>
|
|
73
|
-
</Card>
|
|
74
|
-
|
|
75
|
-
<Card>
|
|
76
|
-
<CardHeader>
|
|
77
|
-
<CardTitle>Sign-ups</CardTitle>
|
|
78
|
-
<CardDescription>New accounts per day (UTC), last 7 days.</CardDescription>
|
|
79
|
-
</CardHeader>
|
|
80
|
-
<CardContent>
|
|
81
|
-
<div className="flex h-40 items-end gap-2" role="list">
|
|
82
|
-
{stats.signupsByDay.map((day, i) => (
|
|
83
|
-
<div
|
|
84
|
-
key={day.date}
|
|
85
|
-
role="listitem"
|
|
86
|
-
aria-label={`${day.date}: ${day.count} sign-ups`}
|
|
87
|
-
className="flex h-full flex-1 flex-col items-center justify-end gap-1"
|
|
88
|
-
>
|
|
89
|
-
<span className="text-xs text-muted-foreground tabular-nums">{day.count}</span>
|
|
90
|
-
<div className="flex w-full flex-1 items-end">
|
|
91
|
-
<AnimatedBar
|
|
92
|
-
vertical
|
|
93
|
-
value={(day.count / maxDaily) * 100}
|
|
94
|
-
delay={i * 0.05}
|
|
95
|
-
className="min-h-0.5 w-full"
|
|
96
|
-
/>
|
|
97
|
-
</div>
|
|
98
|
-
<span className="text-xs text-muted-foreground">
|
|
99
|
-
{dayFormat.format(new Date(`${day.date}T00:00:00Z`))}
|
|
100
|
-
</span>
|
|
101
|
-
</div>
|
|
102
|
-
))}
|
|
103
|
-
</div>
|
|
104
|
-
</CardContent>
|
|
105
|
-
</Card>
|
|
106
|
-
</div>
|
|
107
|
-
</>
|
|
108
|
-
);
|
|
109
|
-
}
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import type { ReactNode } from "react";
|
|
2
|
-
|
|
3
|
-
import { Logo } from "@/components/logo";
|
|
4
|
-
import { ThemeToggle } from "@/components/theme-toggle";
|
|
5
|
-
|
|
6
|
-
export default function AuthLayout({ children }: { children: ReactNode }) {
|
|
7
|
-
return (
|
|
8
|
-
<div className="relative flex flex-1 flex-col">
|
|
9
|
-
<div
|
|
10
|
-
aria-hidden
|
|
11
|
-
className="pointer-events-none absolute inset-0 -z-10 bg-[radial-gradient(ellipse_at_top,var(--color-muted),transparent_60%)]"
|
|
12
|
-
/>
|
|
13
|
-
<header className="mx-auto flex h-14 w-full max-w-6xl items-center justify-between px-4">
|
|
14
|
-
<Logo />
|
|
15
|
-
<ThemeToggle />
|
|
16
|
-
</header>
|
|
17
|
-
<main className="flex flex-1 items-center justify-center px-4 py-10">
|
|
18
|
-
<div className="w-full max-w-sm">{children}</div>
|
|
19
|
-
</main>
|
|
20
|
-
</div>
|
|
21
|
-
);
|
|
22
|
-
}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import Link from "next/link";
|
|
2
|
-
|
|
3
|
-
import { Button } from "@/components/ui/button";
|
|
4
|
-
|
|
5
|
-
export default function NotFound() {
|
|
6
|
-
return (
|
|
7
|
-
<main className="flex flex-1 flex-col items-center justify-center gap-4 px-4 py-24 text-center">
|
|
8
|
-
<p className="font-mono text-sm text-muted-foreground">404</p>
|
|
9
|
-
<h1 className="text-2xl font-semibold tracking-tight">Page not found</h1>
|
|
10
|
-
<p className="max-w-sm text-sm text-muted-foreground">
|
|
11
|
-
The page you're looking for doesn't exist or has been moved.
|
|
12
|
-
</p>
|
|
13
|
-
<Button asChild>
|
|
14
|
-
<Link href="/">Back home</Link>
|
|
15
|
-
</Button>
|
|
16
|
-
</main>
|
|
17
|
-
);
|
|
18
|
-
}
|