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,128 @@
|
|
|
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
|
+
// @feature !charts
|
|
7
|
+
import { AnimatedBar } from "@/components/reports/animated-bar";
|
|
8
|
+
import { RoleBadge } from "@/components/role-badge";
|
|
9
|
+
// @end !charts
|
|
10
|
+
// @feature charts
|
|
11
|
+
import { RoleChart } from "@/components/reports/role-chart";
|
|
12
|
+
import { SignupsChart } from "@/components/reports/signups-chart";
|
|
13
|
+
// @end charts
|
|
14
|
+
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
|
|
15
|
+
// @feature !charts
|
|
16
|
+
import { ROLES } from "@/config/rbac";
|
|
17
|
+
// @end !charts
|
|
18
|
+
import { requirePermission } from "@/lib/dal";
|
|
19
|
+
import { getUserStats, SIGNUP_DAYS } from "@/lib/users";
|
|
20
|
+
|
|
21
|
+
export const metadata: Metadata = { title: "Reports" };
|
|
22
|
+
|
|
23
|
+
// @feature !charts
|
|
24
|
+
const dayFormat = new Intl.DateTimeFormat("en-US", { day: "numeric", timeZone: "UTC" });
|
|
25
|
+
// @end !charts
|
|
26
|
+
|
|
27
|
+
export default async function ReportsPage() {
|
|
28
|
+
await requirePermission("reports:view");
|
|
29
|
+
const stats = await getUserStats();
|
|
30
|
+
|
|
31
|
+
const cards = [
|
|
32
|
+
{ label: "Total users", value: stats.total, icon: Users },
|
|
33
|
+
{ label: `New in the last ${SIGNUP_DAYS} days`, value: stats.newInPeriod, icon: UserPlus },
|
|
34
|
+
{ label: "Managers", value: stats.byRole.manager, icon: ShieldCheck },
|
|
35
|
+
{ label: "Admins", value: stats.byRole.admin, icon: Crown },
|
|
36
|
+
];
|
|
37
|
+
// @feature !charts
|
|
38
|
+
const maxDaily = Math.max(1, ...stats.signupsByDay.map((d) => d.count));
|
|
39
|
+
// @end !charts
|
|
40
|
+
|
|
41
|
+
return (
|
|
42
|
+
<>
|
|
43
|
+
<PageHeader title="Reports" description="Sample analytics, visible to managers and admins." />
|
|
44
|
+
|
|
45
|
+
<div className="cards-grid cards-grid--4">
|
|
46
|
+
{cards.map(({ label, value, icon: Icon }, i) => (
|
|
47
|
+
<FadeIn key={label} delay={i * 0.04}>
|
|
48
|
+
<Card>
|
|
49
|
+
<CardHeader>
|
|
50
|
+
<CardDescription className="stat-label">
|
|
51
|
+
{label} <Icon size={16} />
|
|
52
|
+
</CardDescription>
|
|
53
|
+
<CardTitle className="stat-value">{value}</CardTitle>
|
|
54
|
+
</CardHeader>
|
|
55
|
+
</Card>
|
|
56
|
+
</FadeIn>
|
|
57
|
+
))}
|
|
58
|
+
</div>
|
|
59
|
+
|
|
60
|
+
<div className="cards-grid cards-grid--2 section-gap">
|
|
61
|
+
<Card>
|
|
62
|
+
<CardHeader>
|
|
63
|
+
<CardTitle>Users by role</CardTitle>
|
|
64
|
+
<CardDescription>Share of all accounts.</CardDescription>
|
|
65
|
+
</CardHeader>
|
|
66
|
+
<CardContent>
|
|
67
|
+
{/* @feature charts */}
|
|
68
|
+
<RoleChart byRole={stats.byRole} />
|
|
69
|
+
{/* @end charts */}
|
|
70
|
+
{/* @feature !charts */}
|
|
71
|
+
<div className="stack">
|
|
72
|
+
{ROLES.map((role, i) => {
|
|
73
|
+
const count = stats.byRole[role];
|
|
74
|
+
const pct = stats.total ? (count / stats.total) * 100 : 0;
|
|
75
|
+
return (
|
|
76
|
+
<div key={role} className="stack-sm">
|
|
77
|
+
<div className="row-between text-sm">
|
|
78
|
+
<RoleBadge role={role} />
|
|
79
|
+
<span className="muted tabular">
|
|
80
|
+
{count} · {Math.round(pct)}%
|
|
81
|
+
</span>
|
|
82
|
+
</div>
|
|
83
|
+
<div className="bar-track">
|
|
84
|
+
<AnimatedBar value={pct} delay={i * 0.1} />
|
|
85
|
+
</div>
|
|
86
|
+
</div>
|
|
87
|
+
);
|
|
88
|
+
})}
|
|
89
|
+
</div>
|
|
90
|
+
{/* @end !charts */}
|
|
91
|
+
</CardContent>
|
|
92
|
+
</Card>
|
|
93
|
+
|
|
94
|
+
<Card>
|
|
95
|
+
<CardHeader>
|
|
96
|
+
<CardTitle>Sign-ups</CardTitle>
|
|
97
|
+
<CardDescription>New accounts per day (UTC), last {SIGNUP_DAYS} days.</CardDescription>
|
|
98
|
+
</CardHeader>
|
|
99
|
+
<CardContent>
|
|
100
|
+
{/* @feature charts */}
|
|
101
|
+
<SignupsChart data={stats.signupsByDay} />
|
|
102
|
+
{/* @end charts */}
|
|
103
|
+
{/* @feature !charts */}
|
|
104
|
+
<div className="column-chart" role="list">
|
|
105
|
+
{stats.signupsByDay.map((day, i) => (
|
|
106
|
+
<div
|
|
107
|
+
key={day.date}
|
|
108
|
+
role="listitem"
|
|
109
|
+
aria-label={`${day.date}: ${day.count} sign-ups`}
|
|
110
|
+
className="column-chart-item"
|
|
111
|
+
>
|
|
112
|
+
<span className="muted tabular text-xs">{day.count}</span>
|
|
113
|
+
<div className="column-chart-bar">
|
|
114
|
+
<AnimatedBar vertical value={(day.count / maxDaily) * 100} delay={i * 0.03} />
|
|
115
|
+
</div>
|
|
116
|
+
<span className="muted text-xs">
|
|
117
|
+
{dayFormat.format(new Date(`${day.date}T00:00:00Z`))}
|
|
118
|
+
</span>
|
|
119
|
+
</div>
|
|
120
|
+
))}
|
|
121
|
+
</div>
|
|
122
|
+
{/* @end !charts */}
|
|
123
|
+
</CardContent>
|
|
124
|
+
</Card>
|
|
125
|
+
</div>
|
|
126
|
+
</>
|
|
127
|
+
);
|
|
128
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { Metadata } from "next";
|
|
2
|
+
|
|
3
|
+
import { PageHeader } from "@/components/layout/page-header";
|
|
4
|
+
import { RoleBadge } from "@/components/role-badge";
|
|
5
|
+
import { PasswordForm } from "@/components/settings/password-form";
|
|
6
|
+
import { ProfileForm } from "@/components/settings/profile-form";
|
|
7
|
+
import { SignOutOtherSessions } from "@/components/settings/sessions-card";
|
|
8
|
+
// @feature email
|
|
9
|
+
import { Badge } from "@/components/ui/badge";
|
|
10
|
+
// @end email
|
|
11
|
+
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
|
|
12
|
+
import { requireUser } from "@/lib/dal";
|
|
13
|
+
|
|
14
|
+
export const metadata: Metadata = { title: "Settings" };
|
|
15
|
+
|
|
16
|
+
export default async function SettingsPage() {
|
|
17
|
+
const user = await requireUser();
|
|
18
|
+
|
|
19
|
+
return (
|
|
20
|
+
<>
|
|
21
|
+
<PageHeader title="Settings" description="Manage your profile, password and sessions." />
|
|
22
|
+
<div className="settings-grid">
|
|
23
|
+
<Card>
|
|
24
|
+
<CardHeader>
|
|
25
|
+
<CardTitle>Profile</CardTitle>
|
|
26
|
+
<CardDescription className="cluster">
|
|
27
|
+
Signed in as {user.email} <RoleBadge role={user.role} />
|
|
28
|
+
{/* @feature email */}
|
|
29
|
+
{user.emailVerified ? (
|
|
30
|
+
<Badge variant="secondary">Verified</Badge>
|
|
31
|
+
) : (
|
|
32
|
+
<Badge variant="destructive">Unverified</Badge>
|
|
33
|
+
)}
|
|
34
|
+
{/* @end email */}
|
|
35
|
+
</CardDescription>
|
|
36
|
+
</CardHeader>
|
|
37
|
+
<CardContent>
|
|
38
|
+
<ProfileForm />
|
|
39
|
+
</CardContent>
|
|
40
|
+
</Card>
|
|
41
|
+
|
|
42
|
+
<Card>
|
|
43
|
+
<CardHeader>
|
|
44
|
+
<CardTitle>Password</CardTitle>
|
|
45
|
+
<CardDescription>Changing it signs you out on every other device.</CardDescription>
|
|
46
|
+
</CardHeader>
|
|
47
|
+
<CardContent>
|
|
48
|
+
<PasswordForm />
|
|
49
|
+
</CardContent>
|
|
50
|
+
</Card>
|
|
51
|
+
|
|
52
|
+
<Card>
|
|
53
|
+
<CardHeader>
|
|
54
|
+
<CardTitle>Sessions</CardTitle>
|
|
55
|
+
<CardDescription>
|
|
56
|
+
Lost a device or signed in somewhere public? Sign out everywhere except here.
|
|
57
|
+
</CardDescription>
|
|
58
|
+
</CardHeader>
|
|
59
|
+
<CardContent>
|
|
60
|
+
<SignOutOtherSessions />
|
|
61
|
+
</CardContent>
|
|
62
|
+
</Card>
|
|
63
|
+
</div>
|
|
64
|
+
</>
|
|
65
|
+
);
|
|
66
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Metadata } from "next";
|
|
2
|
+
|
|
3
|
+
import { AuthCard } from "@/components/auth/auth-card";
|
|
4
|
+
import { ForgotPasswordForm } from "@/components/auth/forgot-password-form";
|
|
5
|
+
|
|
6
|
+
export const metadata: Metadata = { title: "Forgot password" };
|
|
7
|
+
|
|
8
|
+
export default function ForgotPasswordPage() {
|
|
9
|
+
return (
|
|
10
|
+
<AuthCard
|
|
11
|
+
title="Forgot your password?"
|
|
12
|
+
description="Enter your email and we'll send you a link to reset it."
|
|
13
|
+
>
|
|
14
|
+
<ForgotPasswordForm />
|
|
15
|
+
</AuthCard>
|
|
16
|
+
);
|
|
17
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
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="auth-shell">
|
|
9
|
+
<header className="site-header">
|
|
10
|
+
<Logo />
|
|
11
|
+
<ThemeToggle />
|
|
12
|
+
</header>
|
|
13
|
+
<main className="auth-main">
|
|
14
|
+
<div className="auth-column">{children}</div>
|
|
15
|
+
</main>
|
|
16
|
+
</div>
|
|
17
|
+
);
|
|
18
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Metadata } from "next";
|
|
2
|
+
import { Suspense } from "react";
|
|
3
|
+
|
|
4
|
+
import { AuthCard, AuthFormSkeleton } from "@/components/auth/auth-card";
|
|
5
|
+
import { LoginForm } from "@/components/auth/login-form";
|
|
6
|
+
|
|
7
|
+
export const metadata: Metadata = { title: "Sign in" };
|
|
8
|
+
|
|
9
|
+
export default function LoginPage() {
|
|
10
|
+
return (
|
|
11
|
+
<AuthCard title="Welcome back" description="Sign in to your account to continue.">
|
|
12
|
+
{/* LoginForm reads ?next= with useSearchParams, so it needs a Suspense boundary. */}
|
|
13
|
+
<Suspense fallback={<AuthFormSkeleton fields={2} />}>
|
|
14
|
+
<LoginForm />
|
|
15
|
+
</Suspense>
|
|
16
|
+
</AuthCard>
|
|
17
|
+
);
|
|
18
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Metadata } from "next";
|
|
2
|
+
import { Suspense } from "react";
|
|
3
|
+
|
|
4
|
+
import { AuthCard, AuthFormSkeleton } from "@/components/auth/auth-card";
|
|
5
|
+
import { RegisterForm } from "@/components/auth/register-form";
|
|
6
|
+
|
|
7
|
+
export const metadata: Metadata = { title: "Create account" };
|
|
8
|
+
|
|
9
|
+
export default function RegisterPage() {
|
|
10
|
+
return (
|
|
11
|
+
<AuthCard title="Create an account" description="The first account registered becomes the admin.">
|
|
12
|
+
{/* RegisterForm reads ?next= with useSearchParams, so it needs a Suspense boundary. */}
|
|
13
|
+
<Suspense fallback={<AuthFormSkeleton fields={4} />}>
|
|
14
|
+
<RegisterForm />
|
|
15
|
+
</Suspense>
|
|
16
|
+
</AuthCard>
|
|
17
|
+
);
|
|
18
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Metadata } from "next";
|
|
2
|
+
import { Suspense } from "react";
|
|
3
|
+
|
|
4
|
+
import { AuthCard, AuthFormSkeleton } from "@/components/auth/auth-card";
|
|
5
|
+
import { ResetPasswordForm } from "@/components/auth/reset-password-form";
|
|
6
|
+
|
|
7
|
+
export const metadata: Metadata = { title: "Reset password" };
|
|
8
|
+
|
|
9
|
+
export default function ResetPasswordPage() {
|
|
10
|
+
return (
|
|
11
|
+
<AuthCard title="Choose a new password" description="This signs you out on every other device.">
|
|
12
|
+
{/* The form reads ?token= with useSearchParams, so it needs a Suspense boundary. */}
|
|
13
|
+
<Suspense fallback={<AuthFormSkeleton fields={2} />}>
|
|
14
|
+
<ResetPasswordForm />
|
|
15
|
+
</Suspense>
|
|
16
|
+
</AuthCard>
|
|
17
|
+
);
|
|
18
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { NextResponse } from "next/server";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
ApiError,
|
|
5
|
+
// @feature rate-limit
|
|
6
|
+
enforceRateLimit,
|
|
7
|
+
// @end rate-limit
|
|
8
|
+
parseBody,
|
|
9
|
+
withAuth,
|
|
10
|
+
} from "@/lib/api";
|
|
11
|
+
import { hashPassword, setSessionCookie, verifyPassword } from "@/lib/auth";
|
|
12
|
+
import { connectDB } from "@/lib/db";
|
|
13
|
+
// @feature rate-limit
|
|
14
|
+
import { RATE_LIMITS } from "@/lib/rate-limit";
|
|
15
|
+
// @end rate-limit
|
|
16
|
+
import { changePasswordSchema } from "@/lib/validations/account";
|
|
17
|
+
import { User } from "@/models/user";
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* POST /api/account/password { currentPassword, newPassword }
|
|
21
|
+
* Changes the password and signs out every other session.
|
|
22
|
+
*/
|
|
23
|
+
export const POST = withAuth(async (req, _ctx, user) => {
|
|
24
|
+
// @feature rate-limit
|
|
25
|
+
enforceRateLimit(`change-password:${user.id}`, RATE_LIMITS.sensitive);
|
|
26
|
+
// @end rate-limit
|
|
27
|
+
const { currentPassword, newPassword } = await parseBody(req, changePasswordSchema);
|
|
28
|
+
|
|
29
|
+
await connectDB();
|
|
30
|
+
const doc = await User.findById(user.id).select("+password").lean();
|
|
31
|
+
if (!doc || !(await verifyPassword(currentPassword, doc.password))) {
|
|
32
|
+
throw new ApiError(400, "Your current password is incorrect", "VALIDATION_ERROR", {
|
|
33
|
+
currentPassword: ["Your current password is incorrect"],
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const updated = await User.findByIdAndUpdate(
|
|
38
|
+
user.id,
|
|
39
|
+
{ password: await hashPassword(newPassword), $inc: { tokenVersion: 1 } },
|
|
40
|
+
{ returnDocument: "after" },
|
|
41
|
+
).lean();
|
|
42
|
+
// Keep this browser signed in with a token for the new version.
|
|
43
|
+
await setSessionCookie({ ...user, tokenVersion: updated?.tokenVersion ?? 0 });
|
|
44
|
+
return NextResponse.json({ ok: true });
|
|
45
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { NextResponse } from "next/server";
|
|
2
|
+
|
|
3
|
+
import { parseBody, withAuth } from "@/lib/api";
|
|
4
|
+
import { setSessionCookie } from "@/lib/auth";
|
|
5
|
+
import { connectDB } from "@/lib/db";
|
|
6
|
+
import { toPublicUser } from "@/lib/users";
|
|
7
|
+
import { updateProfileSchema } from "@/lib/validations/account";
|
|
8
|
+
import { User } from "@/models/user";
|
|
9
|
+
|
|
10
|
+
/** PATCH /api/account { name }: update the signed-in user's profile. */
|
|
11
|
+
export const PATCH = withAuth(async (req, _ctx, user) => {
|
|
12
|
+
const { name } = await parseBody(req, updateProfileSchema);
|
|
13
|
+
await connectDB();
|
|
14
|
+
const updated = await User.findByIdAndUpdate(
|
|
15
|
+
user.id,
|
|
16
|
+
{ name },
|
|
17
|
+
{ returnDocument: "after", runValidators: true },
|
|
18
|
+
).lean();
|
|
19
|
+
if (!updated) throw new Error("Signed-in user disappeared");
|
|
20
|
+
|
|
21
|
+
const publicUser = toPublicUser(updated);
|
|
22
|
+
// The JWT carries the name, so re-issue it.
|
|
23
|
+
await setSessionCookie({ ...publicUser, tokenVersion: updated.tokenVersion ?? 0 });
|
|
24
|
+
return NextResponse.json({ user: publicUser });
|
|
25
|
+
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { NextResponse } from "next/server";
|
|
2
|
+
|
|
3
|
+
import { withAuth } from "@/lib/api";
|
|
4
|
+
import { setSessionCookie } from "@/lib/auth";
|
|
5
|
+
import { bumpTokenVersion } from "@/lib/users";
|
|
6
|
+
|
|
7
|
+
/** DELETE /api/account/sessions: signs out every other device (this one stays signed in). */
|
|
8
|
+
export const DELETE = withAuth(async (_req, _ctx, user) => {
|
|
9
|
+
const tokenVersion = await bumpTokenVersion(user.id);
|
|
10
|
+
await setSessionCookie({ ...user, tokenVersion });
|
|
11
|
+
return NextResponse.json({ ok: true });
|
|
12
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { NextResponse } from "next/server";
|
|
2
|
+
|
|
3
|
+
import { withAuth } from "@/lib/api";
|
|
4
|
+
import { listAuditEntries } from "@/lib/audit";
|
|
5
|
+
|
|
6
|
+
/** GET /api/admin/audit?limit=100 (requires audit:read) */
|
|
7
|
+
export const GET = withAuth(
|
|
8
|
+
async (req) => {
|
|
9
|
+
const limit = Math.min(500, Math.max(1, Number(req.nextUrl.searchParams.get("limit")) || 100));
|
|
10
|
+
return NextResponse.json({ entries: await listAuditEntries(limit) });
|
|
11
|
+
},
|
|
12
|
+
{ permission: "audit:read" },
|
|
13
|
+
);
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { NextResponse } from "next/server";
|
|
2
|
+
|
|
3
|
+
import { ApiError, parseBody, withAuth } from "@/lib/api";
|
|
4
|
+
// @feature audit-log
|
|
5
|
+
import { recordAudit } from "@/lib/audit";
|
|
6
|
+
// @end audit-log
|
|
7
|
+
import { deleteUser, updateUserRole } from "@/lib/users";
|
|
8
|
+
import { updateRoleSchema } from "@/lib/validations/user";
|
|
9
|
+
|
|
10
|
+
type Ctx = RouteContext<"/api/admin/users/[id]">;
|
|
11
|
+
|
|
12
|
+
/** PATCH /api/admin/users/:id { "role": "manager" } (requires users:update-role) */
|
|
13
|
+
export const PATCH = withAuth<Ctx>(
|
|
14
|
+
async (req, ctx, actor) => {
|
|
15
|
+
const { id } = await ctx.params;
|
|
16
|
+
const { role } = await parseBody(req, updateRoleSchema);
|
|
17
|
+
|
|
18
|
+
if (id === actor.id) {
|
|
19
|
+
throw ApiError.badRequest("You can't change your own role", "CANNOT_MODIFY_SELF");
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const result = await updateUserRole(id, role);
|
|
23
|
+
if (!result) throw ApiError.notFound("User not found");
|
|
24
|
+
// @feature audit-log
|
|
25
|
+
if (result.previousRole !== role) {
|
|
26
|
+
await recordAudit("user.role_changed", {
|
|
27
|
+
actor,
|
|
28
|
+
target: result.user,
|
|
29
|
+
details: { from: result.previousRole, to: role },
|
|
30
|
+
req,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
// @end audit-log
|
|
34
|
+
return NextResponse.json({ user: result.user });
|
|
35
|
+
},
|
|
36
|
+
{ permission: "users:update-role" },
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
/** DELETE /api/admin/users/:id (requires users:delete) */
|
|
40
|
+
export const DELETE = withAuth<Ctx>(
|
|
41
|
+
async (req, ctx, actor) => {
|
|
42
|
+
const { id } = await ctx.params;
|
|
43
|
+
|
|
44
|
+
if (id === actor.id) {
|
|
45
|
+
throw ApiError.badRequest("You can't delete your own account", "CANNOT_MODIFY_SELF");
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const deleted = await deleteUser(id);
|
|
49
|
+
if (!deleted) throw ApiError.notFound("User not found");
|
|
50
|
+
// @feature audit-log
|
|
51
|
+
await recordAudit("user.deleted", {
|
|
52
|
+
actor,
|
|
53
|
+
target: deleted,
|
|
54
|
+
details: { role: deleted.role },
|
|
55
|
+
req,
|
|
56
|
+
});
|
|
57
|
+
// @end audit-log
|
|
58
|
+
return NextResponse.json({ ok: true, id });
|
|
59
|
+
},
|
|
60
|
+
{ permission: "users:delete" },
|
|
61
|
+
);
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { NextResponse } from "next/server";
|
|
2
|
+
|
|
3
|
+
import { ApiError, withAuth } from "@/lib/api";
|
|
4
|
+
import { listUsers } from "@/lib/users";
|
|
5
|
+
import { listUsersQuerySchema } from "@/lib/validations/user";
|
|
6
|
+
|
|
7
|
+
/** GET /api/admin/users?page=1&limit=50 (requires users:read) */
|
|
8
|
+
export const GET = withAuth(
|
|
9
|
+
async (req) => {
|
|
10
|
+
const query = listUsersQuerySchema.safeParse(Object.fromEntries(req.nextUrl.searchParams));
|
|
11
|
+
if (!query.success) throw ApiError.badRequest("Invalid query parameters", "INVALID_QUERY");
|
|
12
|
+
|
|
13
|
+
const { page, limit } = query.data;
|
|
14
|
+
const { users, total } = await listUsers({ page, limit });
|
|
15
|
+
return NextResponse.json({ users, total, page, limit });
|
|
16
|
+
},
|
|
17
|
+
{ permission: "users:read" },
|
|
18
|
+
);
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { after, NextResponse } from "next/server";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
apiHandler,
|
|
5
|
+
// @feature rate-limit
|
|
6
|
+
enforceRateLimit,
|
|
7
|
+
// @end rate-limit
|
|
8
|
+
parseBody,
|
|
9
|
+
} from "@/lib/api";
|
|
10
|
+
import { connectDB } from "@/lib/db";
|
|
11
|
+
// @feature rate-limit
|
|
12
|
+
import { getClientIp, RATE_LIMITS } from "@/lib/rate-limit";
|
|
13
|
+
// @end rate-limit
|
|
14
|
+
import { sendPasswordResetEmail } from "@/lib/tokens";
|
|
15
|
+
import { forgotPasswordSchema } from "@/lib/validations/auth";
|
|
16
|
+
import { User } from "@/models/user";
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* POST /api/auth/forgot-password { email }
|
|
20
|
+
*
|
|
21
|
+
* Always answers with the same message, and does the lookup + email after
|
|
22
|
+
* the response is sent, so neither the body nor the timing reveals whether
|
|
23
|
+
* an account exists.
|
|
24
|
+
*/
|
|
25
|
+
export const POST = apiHandler(async (req) => {
|
|
26
|
+
// @feature rate-limit
|
|
27
|
+
enforceRateLimit(`forgot:${getClientIp(req)}`, RATE_LIMITS.passwordReset);
|
|
28
|
+
// @end rate-limit
|
|
29
|
+
const { email } = await parseBody(req, forgotPasswordSchema);
|
|
30
|
+
|
|
31
|
+
after(async () => {
|
|
32
|
+
try {
|
|
33
|
+
await connectDB();
|
|
34
|
+
const user = await User.findOne({ email }).lean();
|
|
35
|
+
if (user) {
|
|
36
|
+
await sendPasswordResetEmail({ id: String(user._id), name: user.name, email: user.email });
|
|
37
|
+
}
|
|
38
|
+
} catch (error) {
|
|
39
|
+
console.error("[forgot-password] Failed to send reset email:", error);
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
return NextResponse.json({
|
|
44
|
+
ok: true,
|
|
45
|
+
message: "If an account exists for that email, we've sent a link to reset the password.",
|
|
46
|
+
});
|
|
47
|
+
});
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { NextResponse } from "next/server";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
ApiError,
|
|
5
|
+
apiHandler,
|
|
6
|
+
// @feature rate-limit
|
|
7
|
+
enforceRateLimit,
|
|
8
|
+
// @end rate-limit
|
|
9
|
+
parseBody,
|
|
10
|
+
} from "@/lib/api";
|
|
11
|
+
import { setSessionCookie, verifyPassword } from "@/lib/auth";
|
|
12
|
+
import { connectDB } from "@/lib/db";
|
|
13
|
+
// @feature rate-limit
|
|
14
|
+
import { getClientIp, RATE_LIMITS } from "@/lib/rate-limit";
|
|
15
|
+
// @end rate-limit
|
|
16
|
+
import { toPublicUser } from "@/lib/users";
|
|
17
|
+
import { loginSchema } from "@/lib/validations/auth";
|
|
18
|
+
import { User } from "@/models/user";
|
|
19
|
+
|
|
20
|
+
export const POST = apiHandler(async (req) => {
|
|
21
|
+
// @feature rate-limit
|
|
22
|
+
enforceRateLimit(`login:${getClientIp(req)}`, RATE_LIMITS.login);
|
|
23
|
+
// @end rate-limit
|
|
24
|
+
const { email, password } = await parseBody(req, loginSchema);
|
|
25
|
+
// @feature rate-limit
|
|
26
|
+
// Also limit per account, so a distributed attack on one email is slowed down too.
|
|
27
|
+
enforceRateLimit(`login-email:${email}`, RATE_LIMITS.loginPerEmail);
|
|
28
|
+
// @end rate-limit
|
|
29
|
+
|
|
30
|
+
await connectDB();
|
|
31
|
+
const user = await User.findOne({ email }).select("+password").lean();
|
|
32
|
+
|
|
33
|
+
// Always run bcrypt (against a dummy hash if the user doesn't exist) and
|
|
34
|
+
// return the same generic error, so attackers can't tell which emails exist.
|
|
35
|
+
const valid = await verifyPassword(password, user?.password);
|
|
36
|
+
if (!user || !valid) {
|
|
37
|
+
throw new ApiError(401, "Invalid email or password", "INVALID_CREDENTIALS");
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const publicUser = toPublicUser(user);
|
|
41
|
+
await setSessionCookie({ ...publicUser, tokenVersion: user.tokenVersion ?? 0 });
|
|
42
|
+
return NextResponse.json({ user: publicUser });
|
|
43
|
+
});
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { NextResponse, type NextRequest } from "next/server";
|
|
2
|
+
|
|
3
|
+
import { LOGIN_PATH } from "@/config/rbac";
|
|
4
|
+
import { apiHandler } from "@/lib/api";
|
|
5
|
+
import { clearSessionCookie } from "@/lib/auth";
|
|
6
|
+
|
|
7
|
+
/** Sign out (used by the logout button). */
|
|
8
|
+
export const POST = apiHandler(async () => {
|
|
9
|
+
await clearSessionCookie();
|
|
10
|
+
return NextResponse.json({ ok: true });
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Clears a stale session and sends the browser to /login. The DAL redirects
|
|
15
|
+
* here when a token is valid but its user was deleted, because Server
|
|
16
|
+
* Components can't modify cookies themselves.
|
|
17
|
+
*/
|
|
18
|
+
export async function GET(request: NextRequest) {
|
|
19
|
+
await clearSessionCookie();
|
|
20
|
+
return NextResponse.redirect(new URL(LOGIN_PATH, request.url));
|
|
21
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { NextResponse } from "next/server";
|
|
2
|
+
|
|
3
|
+
import { getPermissions } from "@/config/rbac";
|
|
4
|
+
import { withAuth } from "@/lib/api";
|
|
5
|
+
import { setSessionCookie } from "@/lib/auth";
|
|
6
|
+
import { getSession, isSessionStale } from "@/lib/dal";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Returns the current user (from the DB) and their permissions. If the role
|
|
10
|
+
* or name in the JWT is out of date, a fresh token is issued so the proxy's
|
|
11
|
+
* optimistic checks match the database again.
|
|
12
|
+
*/
|
|
13
|
+
export const GET = withAuth(async (_req, _ctx, user) => {
|
|
14
|
+
const refreshed = await isSessionStale(user);
|
|
15
|
+
if (refreshed) {
|
|
16
|
+
// withAuth already verified the token version, so reuse it.
|
|
17
|
+
const session = await getSession();
|
|
18
|
+
await setSessionCookie({ ...user, tokenVersion: session?.ver ?? 0 });
|
|
19
|
+
}
|
|
20
|
+
return NextResponse.json({ user, permissions: getPermissions(user.role), refreshed });
|
|
21
|
+
});
|