create-mohit-next 0.0.0-stage → 1.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.
Files changed (82) hide show
  1. package/README.md +108 -2
  2. package/index.js +399 -0
  3. package/package.json +36 -4
  4. package/template/AGENTS.md +9 -0
  5. package/template/Dockerfile +48 -0
  6. package/template/README.md +262 -0
  7. package/template/_dockerignore +13 -0
  8. package/template/_env.example +11 -0
  9. package/template/_gitignore +42 -0
  10. package/template/_prettierignore +11 -0
  11. package/template/_prettierrc +9 -0
  12. package/template/components.json +25 -0
  13. package/template/docker-compose.yml +33 -0
  14. package/template/eslint.config.mjs +21 -0
  15. package/template/next.config.ts +45 -0
  16. package/template/package.json +53 -0
  17. package/template/public/robots.txt +6 -0
  18. package/template/src/app/(app)/admin/page.tsx +53 -0
  19. package/template/src/app/(app)/dashboard/page.tsx +127 -0
  20. package/template/src/app/(app)/error.tsx +28 -0
  21. package/template/src/app/(app)/layout.tsx +35 -0
  22. package/template/src/app/(app)/loading.tsx +5 -0
  23. package/template/src/app/(app)/reports/page.tsx +109 -0
  24. package/template/src/app/(auth)/layout.tsx +22 -0
  25. package/template/src/app/(auth)/login/page.tsx +18 -0
  26. package/template/src/app/(auth)/register/page.tsx +20 -0
  27. package/template/src/app/api/admin/users/[id]/route.ts +40 -0
  28. package/template/src/app/api/admin/users/route.ts +18 -0
  29. package/template/src/app/api/auth/login/route.ts +26 -0
  30. package/template/src/app/api/auth/logout/route.ts +21 -0
  31. package/template/src/app/api/auth/me/route.ts +19 -0
  32. package/template/src/app/api/auth/register/route.ts +40 -0
  33. package/template/src/app/api/health/route.ts +27 -0
  34. package/template/src/app/favicon.ico +0 -0
  35. package/template/src/app/forbidden/page.tsx +49 -0
  36. package/template/src/app/globals.css +142 -0
  37. package/template/src/app/layout.tsx +52 -0
  38. package/template/src/app/not-found.tsx +18 -0
  39. package/template/src/app/page.tsx +187 -0
  40. package/template/src/components/admin/users-table.tsx +162 -0
  41. package/template/src/components/auth/auth-card.tsx +41 -0
  42. package/template/src/components/auth/can.tsx +34 -0
  43. package/template/src/components/auth/form-field.tsx +32 -0
  44. package/template/src/components/auth/login-form.tsx +82 -0
  45. package/template/src/components/auth/register-form.tsx +114 -0
  46. package/template/src/components/auth/session-refresher.tsx +44 -0
  47. package/template/src/components/layout/logout-button.tsx +34 -0
  48. package/template/src/components/layout/navbar.tsx +67 -0
  49. package/template/src/components/layout/page-header.tsx +21 -0
  50. package/template/src/components/layout/skeletons.tsx +35 -0
  51. package/template/src/components/logo.tsx +19 -0
  52. package/template/src/components/motion/fade-in.tsx +19 -0
  53. package/template/src/components/providers/user-provider.tsx +60 -0
  54. package/template/src/components/reports/animated-bar.tsx +28 -0
  55. package/template/src/components/role-badge.tsx +17 -0
  56. package/template/src/components/theme-toggle.tsx +25 -0
  57. package/template/src/components/ui/badge.tsx +44 -0
  58. package/template/src/components/ui/button.tsx +66 -0
  59. package/template/src/components/ui/card.tsx +87 -0
  60. package/template/src/components/ui/input.tsx +18 -0
  61. package/template/src/components/ui/label.tsx +20 -0
  62. package/template/src/components/ui/native-select.tsx +54 -0
  63. package/template/src/components/ui/skeleton.tsx +13 -0
  64. package/template/src/config/rbac.ts +140 -0
  65. package/template/src/config/site.ts +4 -0
  66. package/template/src/lib/api.ts +143 -0
  67. package/template/src/lib/auth.ts +42 -0
  68. package/template/src/lib/dal.ts +60 -0
  69. package/template/src/lib/db.ts +35 -0
  70. package/template/src/lib/env.ts +36 -0
  71. package/template/src/lib/http.ts +57 -0
  72. package/template/src/lib/redirect.ts +21 -0
  73. package/template/src/lib/session.ts +67 -0
  74. package/template/src/lib/theme.ts +8 -0
  75. package/template/src/lib/users.ts +159 -0
  76. package/template/src/lib/utils.ts +1 -0
  77. package/template/src/lib/validations/auth.ts +44 -0
  78. package/template/src/lib/validations/user.ts +13 -0
  79. package/template/src/models/user.ts +30 -0
  80. package/template/src/proxy.ts +57 -0
  81. package/template/src/types/user.ts +10 -0
  82. package/template/tsconfig.json +34 -0
@@ -0,0 +1,142 @@
1
+ @import "tailwindcss";
2
+ @import "tw-animate-css";
3
+ @import "shadcn/tailwind.css";
4
+
5
+ @custom-variant dark (&:is(.dark *));
6
+
7
+ @theme inline {
8
+ --color-background: var(--background);
9
+ --color-foreground: var(--foreground);
10
+ --font-sans: var(--font-geist-sans);
11
+ --font-mono: var(--font-geist-mono);
12
+ --font-heading: var(--font-geist-sans);
13
+ --color-sidebar-ring: var(--sidebar-ring);
14
+ --color-sidebar-border: var(--sidebar-border);
15
+ --color-sidebar-accent-foreground: var(--sidebar-accent-foreground);
16
+ --color-sidebar-accent: var(--sidebar-accent);
17
+ --color-sidebar-primary-foreground: var(--sidebar-primary-foreground);
18
+ --color-sidebar-primary: var(--sidebar-primary);
19
+ --color-sidebar-foreground: var(--sidebar-foreground);
20
+ --color-sidebar: var(--sidebar);
21
+ --color-chart-5: var(--chart-5);
22
+ --color-chart-4: var(--chart-4);
23
+ --color-chart-3: var(--chart-3);
24
+ --color-chart-2: var(--chart-2);
25
+ --color-chart-1: var(--chart-1);
26
+ --color-ring: var(--ring);
27
+ --color-input: var(--input);
28
+ --color-border: var(--border);
29
+ --color-destructive: var(--destructive);
30
+ --color-accent-foreground: var(--accent-foreground);
31
+ --color-accent: var(--accent);
32
+ --color-muted-foreground: var(--muted-foreground);
33
+ --color-muted: var(--muted);
34
+ --color-secondary-foreground: var(--secondary-foreground);
35
+ --color-secondary: var(--secondary);
36
+ --color-primary-foreground: var(--primary-foreground);
37
+ --color-primary: var(--primary);
38
+ --color-popover-foreground: var(--popover-foreground);
39
+ --color-popover: var(--popover);
40
+ --color-card-foreground: var(--card-foreground);
41
+ --color-card: var(--card);
42
+ --radius-sm: calc(var(--radius) * 0.6);
43
+ --radius-md: calc(var(--radius) * 0.8);
44
+ --radius-lg: var(--radius);
45
+ --radius-xl: calc(var(--radius) * 1.4);
46
+ --radius-2xl: calc(var(--radius) * 1.8);
47
+ --radius-3xl: calc(var(--radius) * 2.2);
48
+ --radius-4xl: calc(var(--radius) * 2.6);
49
+ }
50
+
51
+ :root {
52
+ --background: oklch(1 0 0);
53
+ --foreground: oklch(0.145 0 0);
54
+ --card: oklch(1 0 0);
55
+ --card-foreground: oklch(0.145 0 0);
56
+ --popover: oklch(1 0 0);
57
+ --popover-foreground: oklch(0.145 0 0);
58
+ --primary: oklch(0.205 0 0);
59
+ --primary-foreground: oklch(0.985 0 0);
60
+ --secondary: oklch(0.97 0 0);
61
+ --secondary-foreground: oklch(0.205 0 0);
62
+ --muted: oklch(0.97 0 0);
63
+ --muted-foreground: oklch(0.556 0 0);
64
+ --accent: oklch(0.97 0 0);
65
+ --accent-foreground: oklch(0.205 0 0);
66
+ --destructive: oklch(0.577 0.245 27.325);
67
+ --border: oklch(0.922 0 0);
68
+ --input: oklch(0.922 0 0);
69
+ --ring: oklch(0.708 0 0);
70
+ --chart-1: oklch(0.87 0 0);
71
+ --chart-2: oklch(0.556 0 0);
72
+ --chart-3: oklch(0.439 0 0);
73
+ --chart-4: oklch(0.371 0 0);
74
+ --chart-5: oklch(0.269 0 0);
75
+ --radius: 0.625rem;
76
+ --sidebar: oklch(0.985 0 0);
77
+ --sidebar-foreground: oklch(0.145 0 0);
78
+ --sidebar-primary: oklch(0.205 0 0);
79
+ --sidebar-primary-foreground: oklch(0.985 0 0);
80
+ --sidebar-accent: oklch(0.97 0 0);
81
+ --sidebar-accent-foreground: oklch(0.205 0 0);
82
+ --sidebar-border: oklch(0.922 0 0);
83
+ --sidebar-ring: oklch(0.708 0 0);
84
+ }
85
+
86
+ .dark {
87
+ --background: oklch(0.145 0 0);
88
+ --foreground: oklch(0.985 0 0);
89
+ --card: oklch(0.205 0 0);
90
+ --card-foreground: oklch(0.985 0 0);
91
+ --popover: oklch(0.205 0 0);
92
+ --popover-foreground: oklch(0.985 0 0);
93
+ --primary: oklch(0.922 0 0);
94
+ --primary-foreground: oklch(0.205 0 0);
95
+ --secondary: oklch(0.269 0 0);
96
+ --secondary-foreground: oklch(0.985 0 0);
97
+ --muted: oklch(0.269 0 0);
98
+ --muted-foreground: oklch(0.708 0 0);
99
+ --accent: oklch(0.269 0 0);
100
+ --accent-foreground: oklch(0.985 0 0);
101
+ --destructive: oklch(0.704 0.191 22.216);
102
+ --border: oklch(1 0 0 / 10%);
103
+ --input: oklch(1 0 0 / 15%);
104
+ --ring: oklch(0.556 0 0);
105
+ --chart-1: oklch(0.87 0 0);
106
+ --chart-2: oklch(0.556 0 0);
107
+ --chart-3: oklch(0.439 0 0);
108
+ --chart-4: oklch(0.371 0 0);
109
+ --chart-5: oklch(0.269 0 0);
110
+ --sidebar: oklch(0.205 0 0);
111
+ --sidebar-foreground: oklch(0.985 0 0);
112
+ --sidebar-primary: oklch(0.488 0.243 264.376);
113
+ --sidebar-primary-foreground: oklch(0.985 0 0);
114
+ --sidebar-accent: oklch(0.269 0 0);
115
+ --sidebar-accent-foreground: oklch(0.985 0 0);
116
+ --sidebar-border: oklch(1 0 0 / 10%);
117
+ --sidebar-ring: oklch(0.556 0 0);
118
+ }
119
+
120
+ @layer base {
121
+ * {
122
+ @apply border-border outline-ring/50;
123
+ }
124
+ body {
125
+ @apply bg-background text-foreground;
126
+ }
127
+ html {
128
+ @apply font-sans;
129
+ }
130
+ }
131
+ @layer base {
132
+ html {
133
+ color-scheme: light;
134
+ }
135
+ html.dark {
136
+ color-scheme: dark;
137
+ }
138
+ button:not(:disabled),
139
+ [role="button"]:not(:disabled) {
140
+ cursor: pointer;
141
+ }
142
+ }
@@ -0,0 +1,52 @@
1
+ import type { Metadata } from "next";
2
+ import { Geist, Geist_Mono } from "next/font/google";
3
+ import type { CSSProperties } from "react";
4
+ import { Toaster } from "sonner";
5
+
6
+ import { siteConfig } from "@/config/site";
7
+ import { themeScript } from "@/lib/theme";
8
+
9
+ import "./globals.css";
10
+
11
+ const geistSans = Geist({
12
+ variable: "--font-geist-sans",
13
+ subsets: ["latin"],
14
+ });
15
+
16
+ const geistMono = Geist_Mono({
17
+ variable: "--font-geist-mono",
18
+ subsets: ["latin"],
19
+ });
20
+
21
+ export const metadata: Metadata = {
22
+ title: { default: siteConfig.name, template: `%s · ${siteConfig.name}` },
23
+ description: siteConfig.description,
24
+ };
25
+
26
+ export default function RootLayout({ children }: LayoutProps<"/">) {
27
+ return (
28
+ <html
29
+ lang="en"
30
+ className={`${geistSans.variable} ${geistMono.variable} h-full antialiased`}
31
+ suppressHydrationWarning
32
+ >
33
+ <head>
34
+ <script dangerouslySetInnerHTML={{ __html: themeScript }} />
35
+ </head>
36
+ <body className="flex min-h-full flex-col">
37
+ {children}
38
+ <Toaster
39
+ position="top-right"
40
+ closeButton
41
+ style={
42
+ {
43
+ "--normal-bg": "var(--popover)",
44
+ "--normal-text": "var(--popover-foreground)",
45
+ "--normal-border": "var(--border)",
46
+ } as CSSProperties
47
+ }
48
+ />
49
+ </body>
50
+ </html>
51
+ );
52
+ }
@@ -0,0 +1,18 @@
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&apos;re looking for doesn&apos;t exist or has been moved.
12
+ </p>
13
+ <Button asChild>
14
+ <Link href="/">Back home</Link>
15
+ </Button>
16
+ </main>
17
+ );
18
+ }
@@ -0,0 +1,187 @@
1
+ import {
2
+ ArrowRight,
3
+ Container,
4
+ Database,
5
+ KeyRound,
6
+ Layers,
7
+ ShieldCheck,
8
+ Sparkles,
9
+ } from "lucide-react";
10
+ import Link from "next/link";
11
+
12
+ import { Logo } from "@/components/logo";
13
+ import { FadeIn } from "@/components/motion/fade-in";
14
+ import { ThemeToggle } from "@/components/theme-toggle";
15
+ import { Badge } from "@/components/ui/badge";
16
+ import { Button } from "@/components/ui/button";
17
+ import { Card, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
18
+ import { siteConfig } from "@/config/site";
19
+
20
+ const FEATURES = [
21
+ {
22
+ icon: ShieldCheck,
23
+ title: "Role-based access control",
24
+ description:
25
+ "User, manager and admin roles with permissions defined in one file and enforced in three layers.",
26
+ },
27
+ {
28
+ icon: KeyRound,
29
+ title: "JWT authentication",
30
+ description:
31
+ "bcrypt-hashed passwords and HS256 tokens in httpOnly cookies. No auth vendor required.",
32
+ },
33
+ {
34
+ icon: Database,
35
+ title: "MongoDB + Mongoose",
36
+ description: "A cached connection that survives hot reloads, plus typed models.",
37
+ },
38
+ {
39
+ icon: Layers,
40
+ title: "shadcn/ui + Tailwind v4",
41
+ description:
42
+ "Accessible components, CSS-variable theming with dark mode, and motion animations.",
43
+ },
44
+ {
45
+ icon: Sparkles,
46
+ title: "Type-safe end to end",
47
+ description: "zod schemas shared by forms and API routes, plus a strict TypeScript setup.",
48
+ },
49
+ {
50
+ icon: Container,
51
+ title: "Ready to ship",
52
+ description:
53
+ "A multi-stage Docker build, docker-compose with MongoDB, security headers and a health check.",
54
+ },
55
+ ];
56
+
57
+ const LAYERS = [
58
+ {
59
+ step: "1",
60
+ title: "Proxy",
61
+ file: "src/proxy.ts",
62
+ text: "Fast redirects based on the signed JWT.",
63
+ },
64
+ {
65
+ step: "2",
66
+ title: "Data Access Layer",
67
+ file: "src/lib/dal.ts",
68
+ text: "Re-reads the user from MongoDB on every request.",
69
+ },
70
+ {
71
+ step: "3",
72
+ title: "API guard",
73
+ file: "src/lib/api.ts",
74
+ text: "withAuth() returns 401/403 JSON for route handlers.",
75
+ },
76
+ ];
77
+
78
+ export default function HomePage() {
79
+ return (
80
+ <div className="flex flex-1 flex-col">
81
+ <header className="mx-auto flex h-14 w-full max-w-6xl items-center gap-2 px-4">
82
+ <Logo />
83
+ <div className="ml-auto flex items-center gap-2">
84
+ <ThemeToggle />
85
+ <Button asChild variant="ghost" size="sm">
86
+ <Link href="/login">Sign in</Link>
87
+ </Button>
88
+ <Button asChild size="sm">
89
+ <Link href="/register">Get started</Link>
90
+ </Button>
91
+ </div>
92
+ </header>
93
+
94
+ <main className="flex-1">
95
+ <section className="relative overflow-hidden">
96
+ <div
97
+ aria-hidden
98
+ className="pointer-events-none absolute inset-0 -z-10 bg-[radial-gradient(ellipse_at_top,var(--color-muted),transparent_65%)]"
99
+ />
100
+ <div className="mx-auto flex max-w-3xl flex-col items-center gap-6 px-4 pt-20 pb-16 text-center sm:pt-28">
101
+ <FadeIn>
102
+ <Badge variant="outline" className="gap-1.5">
103
+ <Sparkles /> Next.js 16 · React 19 · Tailwind v4
104
+ </Badge>
105
+ </FadeIn>
106
+ <FadeIn delay={0.05}>
107
+ <h1 className="text-4xl font-semibold tracking-tight text-balance sm:text-6xl">
108
+ Ship your full-stack app with auth and roles built in
109
+ </h1>
110
+ </FadeIn>
111
+ <FadeIn delay={0.1}>
112
+ <p className="max-w-xl text-lg text-pretty text-muted-foreground">
113
+ {siteConfig.description}
114
+ </p>
115
+ </FadeIn>
116
+ <FadeIn delay={0.15} className="flex flex-wrap justify-center gap-3">
117
+ <Button asChild size="lg">
118
+ <Link href="/register">
119
+ Create an account <ArrowRight />
120
+ </Link>
121
+ </Button>
122
+ <Button asChild size="lg" variant="outline">
123
+ <Link href="/dashboard">Open dashboard</Link>
124
+ </Button>
125
+ </FadeIn>
126
+ <p className="text-xs text-muted-foreground">
127
+ The first account you register becomes the admin.
128
+ </p>
129
+ </div>
130
+ </section>
131
+
132
+ <section className="mx-auto max-w-6xl px-4 pb-20">
133
+ <div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-3">
134
+ {FEATURES.map(({ icon: Icon, title, description }, i) => (
135
+ <FadeIn key={title} delay={0.05 * i}>
136
+ <Card className="h-full">
137
+ <CardHeader>
138
+ <div className="mb-2 flex size-9 items-center justify-center rounded-lg bg-secondary">
139
+ <Icon className="size-4" />
140
+ </div>
141
+ <CardTitle>{title}</CardTitle>
142
+ <CardDescription>{description}</CardDescription>
143
+ </CardHeader>
144
+ </Card>
145
+ </FadeIn>
146
+ ))}
147
+ </div>
148
+ </section>
149
+
150
+ <section className="border-t bg-muted/30">
151
+ <div className="mx-auto max-w-6xl px-4 py-20">
152
+ <div className="mb-10 max-w-2xl space-y-2">
153
+ <h2 className="text-2xl font-semibold tracking-tight">
154
+ Defense in depth, by default
155
+ </h2>
156
+ <p className="text-muted-foreground">
157
+ Every request is checked at three layers, all driven by{" "}
158
+ <code className="font-mono text-sm">src/config/rbac.ts</code>.
159
+ </p>
160
+ </div>
161
+ <ol className="grid gap-4 md:grid-cols-3">
162
+ {LAYERS.map((layer) => (
163
+ <li key={layer.step} className="rounded-xl border bg-background p-5">
164
+ <span className="font-mono text-xs text-muted-foreground">
165
+ Layer {layer.step}
166
+ </span>
167
+ <h3 className="mt-1 font-medium">{layer.title}</h3>
168
+ <p className="mt-1 text-sm text-muted-foreground">{layer.text}</p>
169
+ <code className="mt-3 inline-block rounded bg-muted px-1.5 py-0.5 font-mono text-xs">
170
+ {layer.file}
171
+ </code>
172
+ </li>
173
+ ))}
174
+ </ol>
175
+ </div>
176
+ </section>
177
+ </main>
178
+
179
+ <footer className="border-t">
180
+ <div className="mx-auto flex max-w-6xl items-center justify-between px-4 py-6 text-sm text-muted-foreground">
181
+ <span>{siteConfig.name}</span>
182
+ <span>Built with create-mohit-next</span>
183
+ </div>
184
+ </footer>
185
+ </div>
186
+ );
187
+ }
@@ -0,0 +1,162 @@
1
+ "use client";
2
+
3
+ import { LoaderCircle, Trash2 } from "lucide-react";
4
+ import { useRouter } from "next/navigation";
5
+ import { useEffect, useState, useTransition } from "react";
6
+ import { toast } from "sonner";
7
+
8
+ import { Can } from "@/components/auth/can";
9
+ import { RoleBadge } from "@/components/role-badge";
10
+ import { Badge } from "@/components/ui/badge";
11
+ import { Button } from "@/components/ui/button";
12
+ import { NativeSelect, NativeSelectOption } from "@/components/ui/native-select";
13
+ import { isRole, ROLE_LABELS, ROLES, type Role } from "@/config/rbac";
14
+ import { apiFetch, getErrorMessage } from "@/lib/http";
15
+ import type { PublicUser } from "@/types/user";
16
+
17
+ // Fixed locale + time zone so server and client render the same string.
18
+ const dateFormat = new Intl.DateTimeFormat("en-US", { dateStyle: "medium", timeZone: "UTC" });
19
+
20
+ export function UsersTable({
21
+ users,
22
+ currentUserId,
23
+ }: {
24
+ users: PublicUser[];
25
+ currentUserId: string;
26
+ }) {
27
+ if (users.length === 0) {
28
+ return <p className="py-10 text-center text-sm text-muted-foreground">No users yet.</p>;
29
+ }
30
+
31
+ return (
32
+ <div className="overflow-x-auto">
33
+ <table className="w-full min-w-[640px] text-sm">
34
+ <thead>
35
+ <tr className="border-b text-left text-muted-foreground">
36
+ <th className="px-4 py-3 font-medium">User</th>
37
+ <th className="px-4 py-3 font-medium">Role</th>
38
+ <th className="px-4 py-3 font-medium">Joined</th>
39
+ <Can permission="users:delete">
40
+ <th className="px-4 py-3 text-right font-medium">
41
+ <span className="sr-only">Actions</span>
42
+ </th>
43
+ </Can>
44
+ </tr>
45
+ </thead>
46
+ <tbody>
47
+ {users.map((user) => (
48
+ // Keyed by role too, so a row resets its local state when fresh data arrives.
49
+ <UserRow
50
+ key={`${user.id}:${user.role}`}
51
+ user={user}
52
+ isSelf={user.id === currentUserId}
53
+ />
54
+ ))}
55
+ </tbody>
56
+ </table>
57
+ </div>
58
+ );
59
+ }
60
+
61
+ function UserRow({ user, isSelf }: { user: PublicUser; isSelf: boolean }) {
62
+ const router = useRouter();
63
+ const [pending, startTransition] = useTransition();
64
+ const [role, setRole] = useState<Role>(user.role);
65
+ const [confirming, setConfirming] = useState(false);
66
+
67
+ // Two-step delete: the confirmation resets after a few seconds.
68
+ useEffect(() => {
69
+ if (!confirming) return;
70
+ const timeout = setTimeout(() => setConfirming(false), 4000);
71
+ return () => clearTimeout(timeout);
72
+ }, [confirming]);
73
+
74
+ function changeRole(next: string) {
75
+ if (!isRole(next) || next === role) return;
76
+ const previous = role;
77
+ setRole(next); // optimistic
78
+ startTransition(async () => {
79
+ try {
80
+ await apiFetch(`/api/admin/users/${user.id}`, { method: "PATCH", json: { role: next } });
81
+ toast.success(`${user.name} is now ${ROLE_LABELS[next].toLowerCase()}`);
82
+ router.refresh();
83
+ } catch (error) {
84
+ setRole(previous);
85
+ toast.error(getErrorMessage(error, "Could not update role"));
86
+ }
87
+ });
88
+ }
89
+
90
+ function remove() {
91
+ if (!confirming) {
92
+ setConfirming(true);
93
+ return;
94
+ }
95
+ startTransition(async () => {
96
+ try {
97
+ await apiFetch(`/api/admin/users/${user.id}`, { method: "DELETE" });
98
+ toast.success(`Deleted ${user.name}`);
99
+ router.refresh();
100
+ } catch (error) {
101
+ toast.error(getErrorMessage(error, "Could not delete user"));
102
+ } finally {
103
+ setConfirming(false);
104
+ }
105
+ });
106
+ }
107
+
108
+ return (
109
+ <tr className="border-b last:border-0">
110
+ <td className="px-4 py-3">
111
+ <div className="flex items-center gap-2 font-medium">
112
+ {user.name}
113
+ {isSelf && <Badge variant="secondary">You</Badge>}
114
+ </div>
115
+ <div className="text-muted-foreground">{user.email}</div>
116
+ </td>
117
+ <td className="px-4 py-3">
118
+ <Can permission="users:update-role" fallback={<RoleBadge role={user.role} />}>
119
+ {isSelf ? (
120
+ <RoleBadge role={user.role} />
121
+ ) : (
122
+ <div className="flex items-center gap-2">
123
+ <NativeSelect
124
+ size="sm"
125
+ value={role}
126
+ disabled={pending}
127
+ onChange={(e) => changeRole(e.target.value)}
128
+ aria-label={`Role for ${user.name}`}
129
+ >
130
+ {ROLES.map((r) => (
131
+ <NativeSelectOption key={r} value={r}>
132
+ {ROLE_LABELS[r]}
133
+ </NativeSelectOption>
134
+ ))}
135
+ </NativeSelect>
136
+ {pending && <LoaderCircle className="size-4 animate-spin text-muted-foreground" />}
137
+ </div>
138
+ )}
139
+ </Can>
140
+ </td>
141
+ <td className="px-4 py-3 text-muted-foreground tabular-nums">
142
+ {dateFormat.format(new Date(user.createdAt))}
143
+ </td>
144
+ <Can permission="users:delete">
145
+ <td className="px-4 py-3 text-right">
146
+ {!isSelf && (
147
+ <Button
148
+ variant="destructive"
149
+ size="sm"
150
+ onClick={remove}
151
+ disabled={pending}
152
+ aria-label={confirming ? `Confirm deleting ${user.name}` : `Delete ${user.name}`}
153
+ >
154
+ <Trash2 />
155
+ {confirming ? "Confirm" : "Delete"}
156
+ </Button>
157
+ )}
158
+ </td>
159
+ </Can>
160
+ </tr>
161
+ );
162
+ }
@@ -0,0 +1,41 @@
1
+ import type { ReactNode } from "react";
2
+
3
+ import { FadeIn } from "@/components/motion/fade-in";
4
+ import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
5
+ import { Skeleton } from "@/components/ui/skeleton";
6
+
7
+ export function AuthCard({
8
+ title,
9
+ description,
10
+ children,
11
+ }: {
12
+ title: string;
13
+ description: string;
14
+ children: ReactNode;
15
+ }) {
16
+ return (
17
+ <FadeIn>
18
+ <Card className="shadow-sm">
19
+ <CardHeader>
20
+ <CardTitle className="text-xl">{title}</CardTitle>
21
+ <CardDescription>{description}</CardDescription>
22
+ </CardHeader>
23
+ <CardContent>{children}</CardContent>
24
+ </Card>
25
+ </FadeIn>
26
+ );
27
+ }
28
+
29
+ export function AuthFormSkeleton({ fields = 2 }: { fields?: number }) {
30
+ return (
31
+ <div className="grid gap-4">
32
+ {Array.from({ length: fields }, (_, i) => (
33
+ <div key={i} className="grid gap-2">
34
+ <Skeleton className="h-4 w-20" />
35
+ <Skeleton className="h-8 w-full" />
36
+ </div>
37
+ ))}
38
+ <Skeleton className="h-9 w-full" />
39
+ </div>
40
+ );
41
+ }
@@ -0,0 +1,34 @@
1
+ "use client";
2
+
3
+ import type { ReactNode } from "react";
4
+
5
+ import { hasPermission, type Permission } from "@/config/rbac";
6
+ import { useOptionalUser } from "@/components/providers/user-provider";
7
+
8
+ type CanProps = {
9
+ /** Required permission. Pass an array to require ANY of them (or ALL with `all`). */
10
+ permission: Permission | Permission[];
11
+ all?: boolean;
12
+ children: ReactNode;
13
+ /** Rendered when the user lacks the permission. */
14
+ fallback?: ReactNode;
15
+ };
16
+
17
+ /**
18
+ * Conditionally renders UI based on the current user's permissions.
19
+ *
20
+ * This only hides UI. Always enforce the same permission on the server
21
+ * (requirePermission() in pages, withAuth() in API routes).
22
+ *
23
+ * @example
24
+ * <Can permission="users:delete" fallback={<span>Read only</span>}>
25
+ * <DeleteButton />
26
+ * </Can>
27
+ */
28
+ export function Can({ permission, all = false, children, fallback = null }: CanProps) {
29
+ const user = useOptionalUser();
30
+ const required = Array.isArray(permission) ? permission : [permission];
31
+ const check = (p: Permission) => hasPermission(user?.role, p);
32
+ const allowed = all ? required.every(check) : required.some(check);
33
+ return <>{allowed ? children : fallback}</>;
34
+ }
@@ -0,0 +1,32 @@
1
+ import type { ComponentProps } from "react";
2
+ import type { FieldError } from "react-hook-form";
3
+
4
+ import { Input } from "@/components/ui/input";
5
+ import { Label } from "@/components/ui/label";
6
+
7
+ type FormFieldProps = ComponentProps<typeof Input> & {
8
+ id: string;
9
+ label: string;
10
+ error?: FieldError;
11
+ };
12
+
13
+ /** Label + input + error message, wired up for accessibility. */
14
+ export function FormField({ id, label, error, ...inputProps }: FormFieldProps) {
15
+ const errorId = `${id}-error`;
16
+ return (
17
+ <div className="grid gap-2">
18
+ <Label htmlFor={id}>{label}</Label>
19
+ <Input
20
+ id={id}
21
+ aria-invalid={error ? true : undefined}
22
+ aria-describedby={error ? errorId : undefined}
23
+ {...inputProps}
24
+ />
25
+ {error?.message && (
26
+ <p id={errorId} className="text-sm text-destructive">
27
+ {error.message}
28
+ </p>
29
+ )}
30
+ </div>
31
+ );
32
+ }