create-mohit-next 0.0.0-stage → 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 +139 -2
- package/index.js +399 -0
- package/lib/features.js +358 -0
- package/lib/scaffold.js +471 -0
- package/package.json +40 -4
- 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/templates/common/_prettierignore +15 -0
- package/templates/common/commitlint.config.mjs +10 -0
- package/templates/next/AGENTS.md +9 -0
- package/templates/next/Dockerfile +48 -0
- package/templates/next/README.md +328 -0
- package/templates/next/_dockerignore +13 -0
- package/templates/next/_env.example +20 -0
- package/templates/next/_gitignore +50 -0
- package/templates/next/docker-compose.yml +33 -0
- package/templates/next/e2e/rbac.spec.ts +69 -0
- package/templates/next/e2e/server.mjs +35 -0
- package/templates/next/eslint.config.mjs +33 -0
- package/templates/next/next.config.ts +47 -0
- package/templates/next/package.json +68 -0
- package/templates/next/playwright.config.ts +30 -0
- package/templates/next/public/robots.txt +6 -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/templates/next/src/app/(app)/dashboard/page.tsx +120 -0
- package/templates/next/src/app/(app)/error.tsx +28 -0
- package/templates/next/src/app/(app)/layout.tsx +58 -0
- package/templates/next/src/app/(app)/loading.tsx +5 -0
- 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/templates/next/src/app/(auth)/login/page.tsx +18 -0
- package/templates/next/src/app/(auth)/register/page.tsx +18 -0
- 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/templates/next/src/app/api/admin/users/[id]/route.ts +61 -0
- package/templates/next/src/app/api/admin/users/route.ts +18 -0
- package/templates/next/src/app/api/auth/forgot-password/route.ts +47 -0
- package/templates/next/src/app/api/auth/login/route.ts +43 -0
- package/templates/next/src/app/api/auth/logout/route.ts +21 -0
- package/templates/next/src/app/api/auth/me/route.ts +21 -0
- package/templates/next/src/app/api/auth/register/route.ts +74 -0
- 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/templates/next/src/app/api/health/route.ts +27 -0
- package/templates/next/src/app/favicon.ico +0 -0
- package/templates/next/src/app/forbidden/page.tsx +47 -0
- package/templates/next/src/app/layout.tsx +55 -0
- 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/templates/next/src/components/auth/auth-card.tsx +41 -0
- package/templates/next/src/components/auth/can.tsx +34 -0
- package/templates/next/src/components/auth/forgot-password-form.tsx +76 -0
- package/templates/next/src/components/auth/form-field.tsx +40 -0
- package/templates/next/src/components/auth/login-form.tsx +87 -0
- package/templates/next/src/components/auth/password-input.tsx +43 -0
- package/templates/next/src/components/auth/register-form.tsx +117 -0
- package/templates/next/src/components/auth/reset-password-form.tsx +80 -0
- package/templates/next/src/components/auth/session-refresher.tsx +44 -0
- 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/templates/next/src/components/logo.tsx +16 -0
- package/templates/next/src/components/motion/fade-in.tsx +19 -0
- package/templates/next/src/components/providers/user-provider.tsx +60 -0
- package/templates/next/src/components/reports/animated-bar.tsx +24 -0
- 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/templates/next/src/config/rbac.ts +170 -0
- package/templates/next/src/config/site.ts +4 -0
- package/templates/next/src/lib/api.ts +179 -0
- package/templates/next/src/lib/audit.ts +66 -0
- package/templates/next/src/lib/auth.ts +50 -0
- package/templates/next/src/lib/dal.ts +63 -0
- package/templates/next/src/lib/db.ts +35 -0
- 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/http.ts +57 -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/templates/next/src/lib/redirect.ts +21 -0
- package/templates/next/src/lib/session.ts +71 -0
- package/templates/next/src/lib/theme.ts +32 -0
- package/templates/next/src/lib/tokens.ts +63 -0
- package/templates/next/src/lib/users.ts +196 -0
- package/templates/next/src/lib/validations/account.ts +20 -0
- package/templates/next/src/lib/validations/auth.test.ts +39 -0
- package/templates/next/src/lib/validations/auth.ts +66 -0
- package/templates/next/src/lib/validations/user.ts +13 -0
- package/templates/next/src/models/audit-log.ts +33 -0
- package/templates/next/src/models/auth-token.ts +26 -0
- package/templates/next/src/models/user.ts +35 -0
- package/templates/next/src/proxy.ts +57 -0
- package/templates/next/src/styles/app.css +1092 -0
- package/templates/next/src/types/user.ts +13 -0
- package/templates/next/tsconfig.json +34 -0
- package/templates/next/vitest.config.mts +13 -0
- package/templates/styles/css.css +580 -0
- package/templates/styles/shadcn.css +142 -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/badge.tsx +44 -0
- package/templates/ui/shadcn/components/button.tsx +66 -0
- package/templates/ui/shadcn/components/card.tsx +87 -0
- package/templates/ui/shadcn/components/dropdown-menu.tsx +268 -0
- package/templates/ui/shadcn/components/input.tsx +18 -0
- package/templates/ui/shadcn/components/label.tsx +20 -0
- package/templates/ui/shadcn/components/native-select.tsx +54 -0
- package/templates/ui/shadcn/components/skeleton.tsx +13 -0
- package/templates/ui/shadcn/components/table.tsx +115 -0
- package/templates/ui/shadcn/utils.ts +1 -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
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { LoaderCircle, Trash2 } from "lucide-react";
|
|
4
|
+
import { useRouter } from "next/navigation";
|
|
5
|
+
import { useState, useTransition } from "react";
|
|
6
|
+
import { toast } from "sonner";
|
|
7
|
+
|
|
8
|
+
import {
|
|
9
|
+
AlertDialog,
|
|
10
|
+
AlertDialogAction,
|
|
11
|
+
AlertDialogCancel,
|
|
12
|
+
AlertDialogContent,
|
|
13
|
+
AlertDialogDescription,
|
|
14
|
+
AlertDialogFooter,
|
|
15
|
+
AlertDialogHeader,
|
|
16
|
+
AlertDialogTitle,
|
|
17
|
+
AlertDialogTrigger,
|
|
18
|
+
} from "@/components/ui/alert-dialog";
|
|
19
|
+
import { Button } from "@/components/ui/button";
|
|
20
|
+
import { NativeSelect, NativeSelectOption } from "@/components/ui/native-select";
|
|
21
|
+
import { isRole, ROLE_LABELS, ROLES, type Role } from "@/config/rbac";
|
|
22
|
+
import { apiFetch, getErrorMessage } from "@/lib/http";
|
|
23
|
+
import type { PublicUser } from "@/types/user";
|
|
24
|
+
|
|
25
|
+
// Fixed locale + time zone so server and client render the same string.
|
|
26
|
+
export const joinedFormat = new Intl.DateTimeFormat("en-US", {
|
|
27
|
+
dateStyle: "medium",
|
|
28
|
+
timeZone: "UTC",
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
/** Role dropdown; saves immediately (optimistic) via PATCH /api/admin/users/:id. */
|
|
32
|
+
export function RoleSelect({ user }: { user: PublicUser }) {
|
|
33
|
+
const router = useRouter();
|
|
34
|
+
const [pending, startTransition] = useTransition();
|
|
35
|
+
const [role, setRole] = useState<Role>(user.role);
|
|
36
|
+
|
|
37
|
+
function change(next: string) {
|
|
38
|
+
if (!isRole(next) || next === role) return;
|
|
39
|
+
const previous = role;
|
|
40
|
+
setRole(next);
|
|
41
|
+
startTransition(async () => {
|
|
42
|
+
try {
|
|
43
|
+
await apiFetch(`/api/admin/users/${user.id}`, { method: "PATCH", json: { role: next } });
|
|
44
|
+
toast.success(`${user.name} is now ${ROLE_LABELS[next].toLowerCase()}`);
|
|
45
|
+
router.refresh();
|
|
46
|
+
} catch (error) {
|
|
47
|
+
setRole(previous);
|
|
48
|
+
toast.error(getErrorMessage(error, "Could not update role"));
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return (
|
|
54
|
+
<div className="cluster">
|
|
55
|
+
<NativeSelect
|
|
56
|
+
size="sm"
|
|
57
|
+
value={role}
|
|
58
|
+
disabled={pending}
|
|
59
|
+
onChange={(e) => change(e.target.value)}
|
|
60
|
+
aria-label={`Role for ${user.name}`}
|
|
61
|
+
>
|
|
62
|
+
{ROLES.map((r) => (
|
|
63
|
+
<NativeSelectOption key={r} value={r}>
|
|
64
|
+
{ROLE_LABELS[r]}
|
|
65
|
+
</NativeSelectOption>
|
|
66
|
+
))}
|
|
67
|
+
</NativeSelect>
|
|
68
|
+
{pending && <LoaderCircle size={16} className="spin muted" />}
|
|
69
|
+
</div>
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Delete button with a confirmation dialog; calls DELETE /api/admin/users/:id. */
|
|
74
|
+
export function DeleteUserButton({ user }: { user: PublicUser }) {
|
|
75
|
+
const router = useRouter();
|
|
76
|
+
const [open, setOpen] = useState(false);
|
|
77
|
+
const [pending, startTransition] = useTransition();
|
|
78
|
+
|
|
79
|
+
function remove() {
|
|
80
|
+
startTransition(async () => {
|
|
81
|
+
try {
|
|
82
|
+
await apiFetch(`/api/admin/users/${user.id}`, { method: "DELETE" });
|
|
83
|
+
toast.success(`Deleted ${user.name}`);
|
|
84
|
+
setOpen(false);
|
|
85
|
+
router.refresh();
|
|
86
|
+
} catch (error) {
|
|
87
|
+
toast.error(getErrorMessage(error, "Could not delete user"));
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return (
|
|
93
|
+
<AlertDialog open={open} onOpenChange={setOpen}>
|
|
94
|
+
<AlertDialogTrigger asChild>
|
|
95
|
+
<Button variant="destructive" size="sm" aria-label={`Delete ${user.name}`}>
|
|
96
|
+
<Trash2 /> Delete
|
|
97
|
+
</Button>
|
|
98
|
+
</AlertDialogTrigger>
|
|
99
|
+
<AlertDialogContent>
|
|
100
|
+
<AlertDialogHeader>
|
|
101
|
+
<AlertDialogTitle>Delete {user.name}?</AlertDialogTitle>
|
|
102
|
+
<AlertDialogDescription>
|
|
103
|
+
This permanently deletes the account for {user.email}. They will be signed out
|
|
104
|
+
immediately. This can't be undone.
|
|
105
|
+
</AlertDialogDescription>
|
|
106
|
+
</AlertDialogHeader>
|
|
107
|
+
<AlertDialogFooter>
|
|
108
|
+
<AlertDialogCancel disabled={pending}>Cancel</AlertDialogCancel>
|
|
109
|
+
<AlertDialogAction
|
|
110
|
+
variant="destructive"
|
|
111
|
+
disabled={pending}
|
|
112
|
+
onClick={(e) => {
|
|
113
|
+
e.preventDefault(); // keep the dialog open until the request finishes
|
|
114
|
+
remove();
|
|
115
|
+
}}
|
|
116
|
+
>
|
|
117
|
+
{pending && <LoaderCircle className="spin" />}
|
|
118
|
+
Delete user
|
|
119
|
+
</AlertDialogAction>
|
|
120
|
+
</AlertDialogFooter>
|
|
121
|
+
</AlertDialogContent>
|
|
122
|
+
</AlertDialog>
|
|
123
|
+
);
|
|
124
|
+
}
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { ArrowDown, ArrowUp, ArrowUpDown, ChevronLeft, ChevronRight, Search } from "lucide-react";
|
|
4
|
+
import { useMemo, useState } from "react";
|
|
5
|
+
|
|
6
|
+
import { DeleteUserButton, joinedFormat, RoleSelect } from "@/components/admin/user-actions";
|
|
7
|
+
import { Can } from "@/components/auth/can";
|
|
8
|
+
import { RoleBadge } from "@/components/role-badge";
|
|
9
|
+
import { Badge } from "@/components/ui/badge";
|
|
10
|
+
import { Button } from "@/components/ui/button";
|
|
11
|
+
import { Input } from "@/components/ui/input";
|
|
12
|
+
import { NativeSelect, NativeSelectOption } from "@/components/ui/native-select";
|
|
13
|
+
import {
|
|
14
|
+
Table,
|
|
15
|
+
TableBody,
|
|
16
|
+
TableCell,
|
|
17
|
+
TableHead,
|
|
18
|
+
TableHeader,
|
|
19
|
+
TableRow,
|
|
20
|
+
} from "@/components/ui/table";
|
|
21
|
+
import { ROLE_LABELS, ROLES, roleRank, type Role } from "@/config/rbac";
|
|
22
|
+
import type { PublicUser } from "@/types/user";
|
|
23
|
+
|
|
24
|
+
type SortKey = "name" | "role" | "createdAt";
|
|
25
|
+
type SortDir = "asc" | "desc";
|
|
26
|
+
|
|
27
|
+
const PAGE_SIZES = [10, 25, 50];
|
|
28
|
+
|
|
29
|
+
function compare(a: PublicUser, b: PublicUser, key: SortKey): number {
|
|
30
|
+
if (key === "role") return roleRank(a.role) - roleRank(b.role);
|
|
31
|
+
if (key === "createdAt") return a.createdAt.localeCompare(b.createdAt);
|
|
32
|
+
return a.name.localeCompare(b.name, undefined, { sensitivity: "base" });
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function SortHeader({
|
|
36
|
+
label,
|
|
37
|
+
column,
|
|
38
|
+
sort,
|
|
39
|
+
onSort,
|
|
40
|
+
}: {
|
|
41
|
+
label: string;
|
|
42
|
+
column: SortKey;
|
|
43
|
+
sort: { key: SortKey; dir: SortDir };
|
|
44
|
+
onSort: (column: SortKey) => void;
|
|
45
|
+
}) {
|
|
46
|
+
const active = sort.key === column;
|
|
47
|
+
const Icon = !active ? ArrowUpDown : sort.dir === "asc" ? ArrowUp : ArrowDown;
|
|
48
|
+
return (
|
|
49
|
+
<TableHead aria-sort={active ? (sort.dir === "asc" ? "ascending" : "descending") : "none"}>
|
|
50
|
+
<button type="button" className="sort-button" onClick={() => onSort(column)}>
|
|
51
|
+
{label}
|
|
52
|
+
<Icon size={14} className={active ? undefined : "muted"} />
|
|
53
|
+
</button>
|
|
54
|
+
</TableHead>
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Users table with search, role filter, sortable columns and pagination (all client-side). */
|
|
59
|
+
export function UsersDataTable({
|
|
60
|
+
users,
|
|
61
|
+
currentUserId,
|
|
62
|
+
}: {
|
|
63
|
+
users: PublicUser[];
|
|
64
|
+
currentUserId: string;
|
|
65
|
+
}) {
|
|
66
|
+
const [query, setQuery] = useState("");
|
|
67
|
+
const [roleFilter, setRoleFilter] = useState<Role | "all">("all");
|
|
68
|
+
const [sort, setSort] = useState<{ key: SortKey; dir: SortDir }>({ key: "createdAt", dir: "desc" });
|
|
69
|
+
const [pageSize, setPageSize] = useState(PAGE_SIZES[0]);
|
|
70
|
+
const [page, setPage] = useState(0);
|
|
71
|
+
|
|
72
|
+
const rows = useMemo(() => {
|
|
73
|
+
const q = query.trim().toLowerCase();
|
|
74
|
+
const filtered = users.filter(
|
|
75
|
+
(u) =>
|
|
76
|
+
(roleFilter === "all" || u.role === roleFilter) &&
|
|
77
|
+
(!q || u.name.toLowerCase().includes(q) || u.email.toLowerCase().includes(q)),
|
|
78
|
+
);
|
|
79
|
+
const factor = sort.dir === "asc" ? 1 : -1;
|
|
80
|
+
return filtered.sort((a, b) => compare(a, b, sort.key) * factor);
|
|
81
|
+
}, [users, query, roleFilter, sort]);
|
|
82
|
+
|
|
83
|
+
const pageCount = Math.max(1, Math.ceil(rows.length / pageSize));
|
|
84
|
+
const current = Math.min(page, pageCount - 1);
|
|
85
|
+
const visible = rows.slice(current * pageSize, current * pageSize + pageSize);
|
|
86
|
+
|
|
87
|
+
function toggleSort(key: SortKey) {
|
|
88
|
+
setSort((s) => (s.key === key ? { key, dir: s.dir === "asc" ? "desc" : "asc" } : { key, dir: "asc" }));
|
|
89
|
+
setPage(0);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return (
|
|
93
|
+
<div className="stack">
|
|
94
|
+
<div className="table-toolbar">
|
|
95
|
+
<div className="search-input">
|
|
96
|
+
<Search size={16} className="search-input-icon" />
|
|
97
|
+
<Input
|
|
98
|
+
type="search"
|
|
99
|
+
placeholder="Search name or email..."
|
|
100
|
+
value={query}
|
|
101
|
+
onChange={(e) => {
|
|
102
|
+
setQuery(e.target.value);
|
|
103
|
+
setPage(0);
|
|
104
|
+
}}
|
|
105
|
+
aria-label="Search users"
|
|
106
|
+
/>
|
|
107
|
+
</div>
|
|
108
|
+
<NativeSelect
|
|
109
|
+
value={roleFilter}
|
|
110
|
+
onChange={(e) => {
|
|
111
|
+
setRoleFilter(e.target.value as Role | "all");
|
|
112
|
+
setPage(0);
|
|
113
|
+
}}
|
|
114
|
+
aria-label="Filter by role"
|
|
115
|
+
>
|
|
116
|
+
<NativeSelectOption value="all">All roles</NativeSelectOption>
|
|
117
|
+
{ROLES.map((r) => (
|
|
118
|
+
<NativeSelectOption key={r} value={r}>
|
|
119
|
+
{ROLE_LABELS[r]}
|
|
120
|
+
</NativeSelectOption>
|
|
121
|
+
))}
|
|
122
|
+
</NativeSelect>
|
|
123
|
+
</div>
|
|
124
|
+
|
|
125
|
+
{visible.length === 0 ? (
|
|
126
|
+
<p className="empty-state">No users match your filters.</p>
|
|
127
|
+
) : (
|
|
128
|
+
<Table>
|
|
129
|
+
<TableHeader>
|
|
130
|
+
<TableRow>
|
|
131
|
+
<SortHeader label="User" column="name" sort={sort} onSort={toggleSort} />
|
|
132
|
+
<SortHeader label="Role" column="role" sort={sort} onSort={toggleSort} />
|
|
133
|
+
<SortHeader label="Joined" column="createdAt" sort={sort} onSort={toggleSort} />
|
|
134
|
+
<Can permission="users:delete">
|
|
135
|
+
<TableHead className="align-right">
|
|
136
|
+
<span className="sr-only">Actions</span>
|
|
137
|
+
</TableHead>
|
|
138
|
+
</Can>
|
|
139
|
+
</TableRow>
|
|
140
|
+
</TableHeader>
|
|
141
|
+
<TableBody>
|
|
142
|
+
{visible.map((user) => {
|
|
143
|
+
const isSelf = user.id === currentUserId;
|
|
144
|
+
return (
|
|
145
|
+
// Keyed by role too, so a row resets its local state when fresh data arrives.
|
|
146
|
+
<TableRow key={`${user.id}:${user.role}`}>
|
|
147
|
+
<TableCell>
|
|
148
|
+
<div className="cluster font-medium">
|
|
149
|
+
{user.name}
|
|
150
|
+
{isSelf && <Badge variant="secondary">You</Badge>}
|
|
151
|
+
</div>
|
|
152
|
+
<div className="muted">{user.email}</div>
|
|
153
|
+
</TableCell>
|
|
154
|
+
<TableCell>
|
|
155
|
+
<Can permission="users:update-role" fallback={<RoleBadge role={user.role} />}>
|
|
156
|
+
{isSelf ? <RoleBadge role={user.role} /> : <RoleSelect user={user} />}
|
|
157
|
+
</Can>
|
|
158
|
+
</TableCell>
|
|
159
|
+
<TableCell className="muted tabular">
|
|
160
|
+
{joinedFormat.format(new Date(user.createdAt))}
|
|
161
|
+
</TableCell>
|
|
162
|
+
<Can permission="users:delete">
|
|
163
|
+
<TableCell className="align-right">
|
|
164
|
+
{!isSelf && <DeleteUserButton user={user} />}
|
|
165
|
+
</TableCell>
|
|
166
|
+
</Can>
|
|
167
|
+
</TableRow>
|
|
168
|
+
);
|
|
169
|
+
})}
|
|
170
|
+
</TableBody>
|
|
171
|
+
</Table>
|
|
172
|
+
)}
|
|
173
|
+
|
|
174
|
+
<div className="table-pagination">
|
|
175
|
+
<span className="muted text-sm">
|
|
176
|
+
{rows.length === 0
|
|
177
|
+
? "0 users"
|
|
178
|
+
: `${current * pageSize + 1}-${Math.min(rows.length, (current + 1) * pageSize)} of ${rows.length}`}
|
|
179
|
+
</span>
|
|
180
|
+
<div className="cluster">
|
|
181
|
+
<NativeSelect
|
|
182
|
+
size="sm"
|
|
183
|
+
value={String(pageSize)}
|
|
184
|
+
onChange={(e) => {
|
|
185
|
+
setPageSize(Number(e.target.value));
|
|
186
|
+
setPage(0);
|
|
187
|
+
}}
|
|
188
|
+
aria-label="Rows per page"
|
|
189
|
+
>
|
|
190
|
+
{PAGE_SIZES.map((size) => (
|
|
191
|
+
<NativeSelectOption key={size} value={String(size)}>
|
|
192
|
+
{size} / page
|
|
193
|
+
</NativeSelectOption>
|
|
194
|
+
))}
|
|
195
|
+
</NativeSelect>
|
|
196
|
+
<Button
|
|
197
|
+
variant="outline"
|
|
198
|
+
size="icon-sm"
|
|
199
|
+
onClick={() => setPage(current - 1)}
|
|
200
|
+
disabled={current === 0}
|
|
201
|
+
aria-label="Previous page"
|
|
202
|
+
>
|
|
203
|
+
<ChevronLeft />
|
|
204
|
+
</Button>
|
|
205
|
+
<span className="tabular text-sm">
|
|
206
|
+
{current + 1} / {pageCount}
|
|
207
|
+
</span>
|
|
208
|
+
<Button
|
|
209
|
+
variant="outline"
|
|
210
|
+
size="icon-sm"
|
|
211
|
+
onClick={() => setPage(current + 1)}
|
|
212
|
+
disabled={current >= pageCount - 1}
|
|
213
|
+
aria-label="Next page"
|
|
214
|
+
>
|
|
215
|
+
<ChevronRight />
|
|
216
|
+
</Button>
|
|
217
|
+
</div>
|
|
218
|
+
</div>
|
|
219
|
+
</div>
|
|
220
|
+
);
|
|
221
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { DeleteUserButton, joinedFormat, RoleSelect } from "@/components/admin/user-actions";
|
|
4
|
+
import { Can } from "@/components/auth/can";
|
|
5
|
+
import { RoleBadge } from "@/components/role-badge";
|
|
6
|
+
import { Badge } from "@/components/ui/badge";
|
|
7
|
+
import {
|
|
8
|
+
Table,
|
|
9
|
+
TableBody,
|
|
10
|
+
TableCell,
|
|
11
|
+
TableHead,
|
|
12
|
+
TableHeader,
|
|
13
|
+
TableRow,
|
|
14
|
+
} from "@/components/ui/table";
|
|
15
|
+
import type { PublicUser } from "@/types/user";
|
|
16
|
+
|
|
17
|
+
/** All users; role select and delete button only render for permitted roles (<Can>). */
|
|
18
|
+
export function UsersTable({ users, currentUserId }: { users: PublicUser[]; currentUserId: string }) {
|
|
19
|
+
if (users.length === 0) return <p className="empty-state">No users yet.</p>;
|
|
20
|
+
|
|
21
|
+
return (
|
|
22
|
+
<Table>
|
|
23
|
+
<TableHeader>
|
|
24
|
+
<TableRow>
|
|
25
|
+
<TableHead>User</TableHead>
|
|
26
|
+
<TableHead>Role</TableHead>
|
|
27
|
+
<TableHead>Joined</TableHead>
|
|
28
|
+
<Can permission="users:delete">
|
|
29
|
+
<TableHead className="align-right">
|
|
30
|
+
<span className="sr-only">Actions</span>
|
|
31
|
+
</TableHead>
|
|
32
|
+
</Can>
|
|
33
|
+
</TableRow>
|
|
34
|
+
</TableHeader>
|
|
35
|
+
<TableBody>
|
|
36
|
+
{users.map((user) => {
|
|
37
|
+
const isSelf = user.id === currentUserId;
|
|
38
|
+
return (
|
|
39
|
+
// Keyed by role too, so a row resets its local state when fresh data arrives.
|
|
40
|
+
<TableRow key={`${user.id}:${user.role}`}>
|
|
41
|
+
<TableCell>
|
|
42
|
+
<div className="cluster font-medium">
|
|
43
|
+
{user.name}
|
|
44
|
+
{isSelf && <Badge variant="secondary">You</Badge>}
|
|
45
|
+
</div>
|
|
46
|
+
<div className="muted">{user.email}</div>
|
|
47
|
+
</TableCell>
|
|
48
|
+
<TableCell>
|
|
49
|
+
<Can permission="users:update-role" fallback={<RoleBadge role={user.role} />}>
|
|
50
|
+
{isSelf ? <RoleBadge role={user.role} /> : <RoleSelect user={user} />}
|
|
51
|
+
</Can>
|
|
52
|
+
</TableCell>
|
|
53
|
+
<TableCell className="muted tabular">
|
|
54
|
+
{joinedFormat.format(new Date(user.createdAt))}
|
|
55
|
+
</TableCell>
|
|
56
|
+
<Can permission="users:delete">
|
|
57
|
+
<TableCell className="align-right">
|
|
58
|
+
{!isSelf && <DeleteUserButton user={user} />}
|
|
59
|
+
</TableCell>
|
|
60
|
+
</Can>
|
|
61
|
+
</TableRow>
|
|
62
|
+
);
|
|
63
|
+
})}
|
|
64
|
+
</TableBody>
|
|
65
|
+
</Table>
|
|
66
|
+
);
|
|
67
|
+
}
|
|
@@ -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: ReactNode;
|
|
14
|
+
children: ReactNode;
|
|
15
|
+
}) {
|
|
16
|
+
return (
|
|
17
|
+
<FadeIn>
|
|
18
|
+
<Card className="auth-card">
|
|
19
|
+
<CardHeader>
|
|
20
|
+
<CardTitle className="auth-card-title">{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="form">
|
|
32
|
+
{Array.from({ length: fields }, (_, i) => (
|
|
33
|
+
<div key={i} className="field">
|
|
34
|
+
<Skeleton className="skeleton-label" />
|
|
35
|
+
<Skeleton className="skeleton-input" />
|
|
36
|
+
</div>
|
|
37
|
+
))}
|
|
38
|
+
<Skeleton className="skeleton-button" />
|
|
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,76 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { zodResolver } from "@hookform/resolvers/zod";
|
|
4
|
+
import { LoaderCircle, MailCheck } from "lucide-react";
|
|
5
|
+
import Link from "next/link";
|
|
6
|
+
import { useState } from "react";
|
|
7
|
+
import { useForm } from "react-hook-form";
|
|
8
|
+
import { toast } from "sonner";
|
|
9
|
+
|
|
10
|
+
import { FormField } from "@/components/auth/form-field";
|
|
11
|
+
import { Button } from "@/components/ui/button";
|
|
12
|
+
import { apiFetch, getErrorMessage } from "@/lib/http";
|
|
13
|
+
import { forgotPasswordSchema } from "@/lib/validations/auth";
|
|
14
|
+
|
|
15
|
+
export function ForgotPasswordForm() {
|
|
16
|
+
const [sentTo, setSentTo] = useState<string | null>(null);
|
|
17
|
+
const {
|
|
18
|
+
register,
|
|
19
|
+
handleSubmit,
|
|
20
|
+
formState: { errors, isSubmitting },
|
|
21
|
+
} = useForm({
|
|
22
|
+
resolver: zodResolver(forgotPasswordSchema),
|
|
23
|
+
defaultValues: { email: "" },
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
const onSubmit = handleSubmit(async (values) => {
|
|
27
|
+
try {
|
|
28
|
+
await apiFetch("/api/auth/forgot-password", { method: "POST", json: values });
|
|
29
|
+
setSentTo(values.email);
|
|
30
|
+
} catch (error) {
|
|
31
|
+
toast.error(getErrorMessage(error, "Could not send the reset link"));
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
if (sentTo) {
|
|
36
|
+
return (
|
|
37
|
+
<div className="notice">
|
|
38
|
+
<MailCheck size={20} />
|
|
39
|
+
<div className="stack-sm">
|
|
40
|
+
<p className="notice-title">Check your inbox</p>
|
|
41
|
+
<p className="muted text-sm">
|
|
42
|
+
If an account exists for <strong>{sentTo}</strong>, we've sent a link to reset the
|
|
43
|
+
password. It expires in 1 hour.
|
|
44
|
+
</p>
|
|
45
|
+
<Link href="/login" className="text-link text-sm">
|
|
46
|
+
Back to sign in
|
|
47
|
+
</Link>
|
|
48
|
+
</div>
|
|
49
|
+
</div>
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return (
|
|
54
|
+
<form onSubmit={onSubmit} className="form" noValidate>
|
|
55
|
+
<FormField
|
|
56
|
+
id="email"
|
|
57
|
+
label="Email"
|
|
58
|
+
type="email"
|
|
59
|
+
autoComplete="email"
|
|
60
|
+
placeholder="you@example.com"
|
|
61
|
+
error={errors.email}
|
|
62
|
+
{...register("email")}
|
|
63
|
+
/>
|
|
64
|
+
<Button type="submit" size="lg" disabled={isSubmitting} className="full-width">
|
|
65
|
+
{isSubmitting && <LoaderCircle className="spin" />}
|
|
66
|
+
Send reset link
|
|
67
|
+
</Button>
|
|
68
|
+
<p className="form-footer">
|
|
69
|
+
Remembered it?{" "}
|
|
70
|
+
<Link href="/login" className="text-link">
|
|
71
|
+
Sign in
|
|
72
|
+
</Link>
|
|
73
|
+
</p>
|
|
74
|
+
</form>
|
|
75
|
+
);
|
|
76
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { ComponentProps, ReactNode } 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
|
+
/** Rendered on the right of the label (e.g. a "Forgot password?" link). */
|
|
12
|
+
labelAside?: ReactNode;
|
|
13
|
+
/** Replaces the default <Input> (e.g. a PasswordInput). */
|
|
14
|
+
render?: (props: ComponentProps<typeof Input>) => ReactNode;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/** Label + input + error message, wired up for accessibility. */
|
|
18
|
+
export function FormField({ id, label, error, labelAside, render, ...inputProps }: FormFieldProps) {
|
|
19
|
+
const errorId = `${id}-error`;
|
|
20
|
+
const props = {
|
|
21
|
+
id,
|
|
22
|
+
"aria-invalid": error ? true : undefined,
|
|
23
|
+
"aria-describedby": error ? errorId : undefined,
|
|
24
|
+
...inputProps,
|
|
25
|
+
};
|
|
26
|
+
return (
|
|
27
|
+
<div className="field">
|
|
28
|
+
<div className="field-label-row">
|
|
29
|
+
<Label htmlFor={id}>{label}</Label>
|
|
30
|
+
{labelAside}
|
|
31
|
+
</div>
|
|
32
|
+
{render ? render(props) : <Input {...props} />}
|
|
33
|
+
{error?.message && (
|
|
34
|
+
<p id={errorId} className="field-error" role="alert">
|
|
35
|
+
{error.message}
|
|
36
|
+
</p>
|
|
37
|
+
)}
|
|
38
|
+
</div>
|
|
39
|
+
);
|
|
40
|
+
}
|