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.
- package/README.md +108 -2
- package/index.js +399 -0
- package/package.json +36 -4
- package/template/AGENTS.md +9 -0
- package/template/Dockerfile +48 -0
- package/template/README.md +262 -0
- package/template/_dockerignore +13 -0
- package/template/_env.example +11 -0
- package/template/_gitignore +42 -0
- package/template/_prettierignore +11 -0
- package/template/_prettierrc +9 -0
- package/template/components.json +25 -0
- package/template/docker-compose.yml +33 -0
- package/template/eslint.config.mjs +21 -0
- package/template/next.config.ts +45 -0
- package/template/package.json +53 -0
- package/template/public/robots.txt +6 -0
- package/template/src/app/(app)/admin/page.tsx +53 -0
- package/template/src/app/(app)/dashboard/page.tsx +127 -0
- package/template/src/app/(app)/error.tsx +28 -0
- package/template/src/app/(app)/layout.tsx +35 -0
- package/template/src/app/(app)/loading.tsx +5 -0
- package/template/src/app/(app)/reports/page.tsx +109 -0
- package/template/src/app/(auth)/layout.tsx +22 -0
- package/template/src/app/(auth)/login/page.tsx +18 -0
- package/template/src/app/(auth)/register/page.tsx +20 -0
- package/template/src/app/api/admin/users/[id]/route.ts +40 -0
- package/template/src/app/api/admin/users/route.ts +18 -0
- package/template/src/app/api/auth/login/route.ts +26 -0
- package/template/src/app/api/auth/logout/route.ts +21 -0
- package/template/src/app/api/auth/me/route.ts +19 -0
- package/template/src/app/api/auth/register/route.ts +40 -0
- package/template/src/app/api/health/route.ts +27 -0
- package/template/src/app/favicon.ico +0 -0
- package/template/src/app/forbidden/page.tsx +49 -0
- package/template/src/app/globals.css +142 -0
- package/template/src/app/layout.tsx +52 -0
- package/template/src/app/not-found.tsx +18 -0
- package/template/src/app/page.tsx +187 -0
- package/template/src/components/admin/users-table.tsx +162 -0
- package/template/src/components/auth/auth-card.tsx +41 -0
- package/template/src/components/auth/can.tsx +34 -0
- package/template/src/components/auth/form-field.tsx +32 -0
- package/template/src/components/auth/login-form.tsx +82 -0
- package/template/src/components/auth/register-form.tsx +114 -0
- package/template/src/components/auth/session-refresher.tsx +44 -0
- package/template/src/components/layout/logout-button.tsx +34 -0
- package/template/src/components/layout/navbar.tsx +67 -0
- package/template/src/components/layout/page-header.tsx +21 -0
- package/template/src/components/layout/skeletons.tsx +35 -0
- package/template/src/components/logo.tsx +19 -0
- package/template/src/components/motion/fade-in.tsx +19 -0
- package/template/src/components/providers/user-provider.tsx +60 -0
- package/template/src/components/reports/animated-bar.tsx +28 -0
- package/template/src/components/role-badge.tsx +17 -0
- package/template/src/components/theme-toggle.tsx +25 -0
- package/template/src/components/ui/badge.tsx +44 -0
- package/template/src/components/ui/button.tsx +66 -0
- package/template/src/components/ui/card.tsx +87 -0
- package/template/src/components/ui/input.tsx +18 -0
- package/template/src/components/ui/label.tsx +20 -0
- package/template/src/components/ui/native-select.tsx +54 -0
- package/template/src/components/ui/skeleton.tsx +13 -0
- package/template/src/config/rbac.ts +140 -0
- package/template/src/config/site.ts +4 -0
- package/template/src/lib/api.ts +143 -0
- package/template/src/lib/auth.ts +42 -0
- package/template/src/lib/dal.ts +60 -0
- package/template/src/lib/db.ts +35 -0
- package/template/src/lib/env.ts +36 -0
- package/template/src/lib/http.ts +57 -0
- package/template/src/lib/redirect.ts +21 -0
- package/template/src/lib/session.ts +67 -0
- package/template/src/lib/theme.ts +8 -0
- package/template/src/lib/users.ts +159 -0
- package/template/src/lib/utils.ts +1 -0
- package/template/src/lib/validations/auth.ts +44 -0
- package/template/src/lib/validations/user.ts +13 -0
- package/template/src/models/user.ts +30 -0
- package/template/src/proxy.ts +57 -0
- package/template/src/types/user.ts +10 -0
- package/template/tsconfig.json +34 -0
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { ArrowRight, Check, Lock } from "lucide-react";
|
|
2
|
+
import type { Metadata } from "next";
|
|
3
|
+
import Link from "next/link";
|
|
4
|
+
|
|
5
|
+
import { PageHeader } from "@/components/layout/page-header";
|
|
6
|
+
import { FadeIn } from "@/components/motion/fade-in";
|
|
7
|
+
import { RoleBadge } from "@/components/role-badge";
|
|
8
|
+
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
|
|
9
|
+
import {
|
|
10
|
+
getNavItems,
|
|
11
|
+
hasPermission,
|
|
12
|
+
PERMISSION_LABELS,
|
|
13
|
+
PERMISSIONS,
|
|
14
|
+
ROLE_LABELS,
|
|
15
|
+
ROLES,
|
|
16
|
+
} from "@/config/rbac";
|
|
17
|
+
import { requirePermission } from "@/lib/dal";
|
|
18
|
+
import { cn } from "@/lib/utils";
|
|
19
|
+
|
|
20
|
+
export const metadata: Metadata = { title: "Dashboard" };
|
|
21
|
+
|
|
22
|
+
const ROLE_DESCRIPTIONS = {
|
|
23
|
+
user: "You can use the core app. Ask an admin if you need more access.",
|
|
24
|
+
manager: "You can view reports and the user directory (read-only).",
|
|
25
|
+
admin: "You have full access, including managing roles and deleting users.",
|
|
26
|
+
} as const;
|
|
27
|
+
|
|
28
|
+
export default async function DashboardPage() {
|
|
29
|
+
// Layer 2: re-checked against the database on every request.
|
|
30
|
+
const user = await requirePermission("dashboard:view");
|
|
31
|
+
const sections = getNavItems(user.role).filter((item) => item.href !== "/dashboard");
|
|
32
|
+
|
|
33
|
+
return (
|
|
34
|
+
<>
|
|
35
|
+
<PageHeader
|
|
36
|
+
title={`Hi, ${user.name.split(" ")[0]}`}
|
|
37
|
+
description="Here's what your role lets you do in this app."
|
|
38
|
+
/>
|
|
39
|
+
|
|
40
|
+
<div className="grid gap-4 lg:grid-cols-3">
|
|
41
|
+
<FadeIn>
|
|
42
|
+
<Card className="h-full">
|
|
43
|
+
<CardHeader>
|
|
44
|
+
<CardDescription>Your role</CardDescription>
|
|
45
|
+
<CardTitle className="flex items-center gap-2 text-2xl">
|
|
46
|
+
{ROLE_LABELS[user.role]} <RoleBadge role={user.role} />
|
|
47
|
+
</CardTitle>
|
|
48
|
+
</CardHeader>
|
|
49
|
+
<CardContent className="space-y-4 text-sm text-muted-foreground">
|
|
50
|
+
<p>{ROLE_DESCRIPTIONS[user.role]}</p>
|
|
51
|
+
<ol className="flex items-center gap-1 text-xs" aria-label="Role hierarchy">
|
|
52
|
+
{ROLES.map((role, i) => (
|
|
53
|
+
<li key={role} className="flex items-center gap-1">
|
|
54
|
+
{i > 0 && <span aria-hidden>›</span>}
|
|
55
|
+
<span className={cn(role === user.role && "font-semibold text-foreground")}>
|
|
56
|
+
{ROLE_LABELS[role]}
|
|
57
|
+
</span>
|
|
58
|
+
</li>
|
|
59
|
+
))}
|
|
60
|
+
</ol>
|
|
61
|
+
</CardContent>
|
|
62
|
+
</Card>
|
|
63
|
+
</FadeIn>
|
|
64
|
+
|
|
65
|
+
<FadeIn delay={0.05} className="lg:col-span-2">
|
|
66
|
+
<Card className="h-full">
|
|
67
|
+
<CardHeader>
|
|
68
|
+
<CardTitle>Permissions</CardTitle>
|
|
69
|
+
<CardDescription>
|
|
70
|
+
Granted to the {ROLE_LABELS[user.role].toLowerCase()} role.
|
|
71
|
+
</CardDescription>
|
|
72
|
+
</CardHeader>
|
|
73
|
+
<CardContent>
|
|
74
|
+
<ul className="grid gap-2 sm:grid-cols-2">
|
|
75
|
+
{PERMISSIONS.map((permission) => {
|
|
76
|
+
const granted = hasPermission(user.role, permission);
|
|
77
|
+
return (
|
|
78
|
+
<li
|
|
79
|
+
key={permission}
|
|
80
|
+
className={cn(
|
|
81
|
+
"flex items-center gap-3 rounded-lg border px-3 py-2",
|
|
82
|
+
!granted && "opacity-50",
|
|
83
|
+
)}
|
|
84
|
+
>
|
|
85
|
+
{granted ? (
|
|
86
|
+
<Check className="size-4 shrink-0 text-emerald-600 dark:text-emerald-400" />
|
|
87
|
+
) : (
|
|
88
|
+
<Lock className="size-4 shrink-0 text-muted-foreground" />
|
|
89
|
+
)}
|
|
90
|
+
<div className="min-w-0">
|
|
91
|
+
<p className="text-sm font-medium">{PERMISSION_LABELS[permission]}</p>
|
|
92
|
+
<code className="font-mono text-xs text-muted-foreground">
|
|
93
|
+
{permission}
|
|
94
|
+
</code>
|
|
95
|
+
</div>
|
|
96
|
+
<span className="sr-only">{granted ? "granted" : "not granted"}</span>
|
|
97
|
+
</li>
|
|
98
|
+
);
|
|
99
|
+
})}
|
|
100
|
+
</ul>
|
|
101
|
+
</CardContent>
|
|
102
|
+
</Card>
|
|
103
|
+
</FadeIn>
|
|
104
|
+
</div>
|
|
105
|
+
|
|
106
|
+
{sections.length > 0 && (
|
|
107
|
+
<FadeIn delay={0.1} className="mt-4 grid gap-4 sm:grid-cols-2">
|
|
108
|
+
{sections.map((item) => (
|
|
109
|
+
<Link key={item.href} href={item.href} className="group">
|
|
110
|
+
<Card className="transition-colors group-hover:bg-muted/50">
|
|
111
|
+
<CardHeader>
|
|
112
|
+
<CardTitle className="flex items-center justify-between">
|
|
113
|
+
{item.label}
|
|
114
|
+
<ArrowRight className="size-4 transition-transform group-hover:translate-x-0.5" />
|
|
115
|
+
</CardTitle>
|
|
116
|
+
<CardDescription>
|
|
117
|
+
Requires <code className="font-mono text-xs">{item.permission}</code>
|
|
118
|
+
</CardDescription>
|
|
119
|
+
</CardHeader>
|
|
120
|
+
</Card>
|
|
121
|
+
</Link>
|
|
122
|
+
))}
|
|
123
|
+
</FadeIn>
|
|
124
|
+
)}
|
|
125
|
+
</>
|
|
126
|
+
);
|
|
127
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use client"; // Error boundaries must be Client Components
|
|
2
|
+
|
|
3
|
+
import { TriangleAlert } from "lucide-react";
|
|
4
|
+
import { useEffect } from "react";
|
|
5
|
+
|
|
6
|
+
import { Button } from "@/components/ui/button";
|
|
7
|
+
|
|
8
|
+
export default function AppError({ error, retry }: { error: unknown; retry: () => void }) {
|
|
9
|
+
useEffect(() => {
|
|
10
|
+
console.error(error);
|
|
11
|
+
}, [error]);
|
|
12
|
+
|
|
13
|
+
return (
|
|
14
|
+
<div className="mx-auto flex max-w-md flex-col items-center gap-4 py-16 text-center">
|
|
15
|
+
<div className="flex size-12 items-center justify-center rounded-full bg-destructive/10 text-destructive">
|
|
16
|
+
<TriangleAlert className="size-6" />
|
|
17
|
+
</div>
|
|
18
|
+
<div className="space-y-1">
|
|
19
|
+
<h2 className="text-lg font-semibold">Something went wrong</h2>
|
|
20
|
+
<p className="text-sm text-muted-foreground">
|
|
21
|
+
We couldn't load this page. If it keeps happening, check that MongoDB is running and
|
|
22
|
+
that <code className="font-mono">MONGODB_URI</code> is set.
|
|
23
|
+
</p>
|
|
24
|
+
</div>
|
|
25
|
+
<Button onClick={() => retry()}>Try again</Button>
|
|
26
|
+
</div>
|
|
27
|
+
);
|
|
28
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
import { Suspense } from "react";
|
|
3
|
+
|
|
4
|
+
import { Navbar } from "@/components/layout/navbar";
|
|
5
|
+
import { ShellSkeleton } from "@/components/layout/skeletons";
|
|
6
|
+
import { UserProvider } from "@/components/providers/user-provider";
|
|
7
|
+
import { isSessionStale, requireUser } from "@/lib/dal";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Authenticated shell for every page in the (app) route group.
|
|
11
|
+
*
|
|
12
|
+
* With Cache Components, reading the session (cookies + DB) is request-time
|
|
13
|
+
* work, so it must happen inside a <Suspense> boundary and never at the
|
|
14
|
+
* layout's top level. The static fallback is prerendered and shown instantly;
|
|
15
|
+
* the real navbar and page stream in once the user is loaded.
|
|
16
|
+
*/
|
|
17
|
+
export default function AppLayout({ children }: { children: ReactNode }) {
|
|
18
|
+
return (
|
|
19
|
+
<Suspense fallback={<ShellSkeleton />}>
|
|
20
|
+
<AuthenticatedShell>{children}</AuthenticatedShell>
|
|
21
|
+
</Suspense>
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async function AuthenticatedShell({ children }: { children: ReactNode }) {
|
|
26
|
+
const user = await requireUser();
|
|
27
|
+
const sessionStale = await isSessionStale(user);
|
|
28
|
+
|
|
29
|
+
return (
|
|
30
|
+
<UserProvider user={user} sessionStale={sessionStale}>
|
|
31
|
+
<Navbar />
|
|
32
|
+
<main className="mx-auto w-full max-w-6xl flex-1 px-4 py-8">{children}</main>
|
|
33
|
+
</UserProvider>
|
|
34
|
+
);
|
|
35
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { Crown, ShieldCheck, UserPlus, Users } from "lucide-react";
|
|
2
|
+
import type { Metadata } from "next";
|
|
3
|
+
|
|
4
|
+
import { PageHeader } from "@/components/layout/page-header";
|
|
5
|
+
import { FadeIn } from "@/components/motion/fade-in";
|
|
6
|
+
import { AnimatedBar } from "@/components/reports/animated-bar";
|
|
7
|
+
import { RoleBadge } from "@/components/role-badge";
|
|
8
|
+
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card";
|
|
9
|
+
import { ROLES } from "@/config/rbac";
|
|
10
|
+
import { requirePermission } from "@/lib/dal";
|
|
11
|
+
import { getUserStats } from "@/lib/users";
|
|
12
|
+
|
|
13
|
+
export const metadata: Metadata = { title: "Reports" };
|
|
14
|
+
|
|
15
|
+
const dayFormat = new Intl.DateTimeFormat("en-US", { weekday: "short", timeZone: "UTC" });
|
|
16
|
+
|
|
17
|
+
export default async function ReportsPage() {
|
|
18
|
+
await requirePermission("reports:view");
|
|
19
|
+
const stats = await getUserStats();
|
|
20
|
+
|
|
21
|
+
const cards = [
|
|
22
|
+
{ label: "Total users", value: stats.total, icon: Users },
|
|
23
|
+
{ label: "New in the last 7 days", value: stats.newLast7Days, icon: UserPlus },
|
|
24
|
+
{ label: "Managers", value: stats.byRole.manager, icon: ShieldCheck },
|
|
25
|
+
{ label: "Admins", value: stats.byRole.admin, icon: Crown },
|
|
26
|
+
];
|
|
27
|
+
const maxDaily = Math.max(1, ...stats.signupsByDay.map((d) => d.count));
|
|
28
|
+
|
|
29
|
+
return (
|
|
30
|
+
<>
|
|
31
|
+
<PageHeader title="Reports" description="Sample analytics, visible to managers and admins." />
|
|
32
|
+
|
|
33
|
+
<div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-4">
|
|
34
|
+
{cards.map(({ label, value, icon: Icon }, i) => (
|
|
35
|
+
<FadeIn key={label} delay={i * 0.04}>
|
|
36
|
+
<Card>
|
|
37
|
+
<CardHeader>
|
|
38
|
+
<CardDescription className="flex items-center justify-between">
|
|
39
|
+
{label} <Icon className="size-4" />
|
|
40
|
+
</CardDescription>
|
|
41
|
+
<CardTitle className="text-3xl tabular-nums">{value}</CardTitle>
|
|
42
|
+
</CardHeader>
|
|
43
|
+
</Card>
|
|
44
|
+
</FadeIn>
|
|
45
|
+
))}
|
|
46
|
+
</div>
|
|
47
|
+
|
|
48
|
+
<div className="mt-4 grid gap-4 lg:grid-cols-2">
|
|
49
|
+
<Card>
|
|
50
|
+
<CardHeader>
|
|
51
|
+
<CardTitle>Users by role</CardTitle>
|
|
52
|
+
<CardDescription>Share of all accounts.</CardDescription>
|
|
53
|
+
</CardHeader>
|
|
54
|
+
<CardContent className="space-y-4">
|
|
55
|
+
{ROLES.map((role, i) => {
|
|
56
|
+
const count = stats.byRole[role];
|
|
57
|
+
const pct = stats.total ? (count / stats.total) * 100 : 0;
|
|
58
|
+
return (
|
|
59
|
+
<div key={role} className="space-y-1.5">
|
|
60
|
+
<div className="flex items-center justify-between text-sm">
|
|
61
|
+
<RoleBadge role={role} />
|
|
62
|
+
<span className="text-muted-foreground tabular-nums">
|
|
63
|
+
{count} · {Math.round(pct)}%
|
|
64
|
+
</span>
|
|
65
|
+
</div>
|
|
66
|
+
<div className="h-2 overflow-hidden rounded-md bg-muted">
|
|
67
|
+
<AnimatedBar value={pct} delay={i * 0.1} className="h-full" />
|
|
68
|
+
</div>
|
|
69
|
+
</div>
|
|
70
|
+
);
|
|
71
|
+
})}
|
|
72
|
+
</CardContent>
|
|
73
|
+
</Card>
|
|
74
|
+
|
|
75
|
+
<Card>
|
|
76
|
+
<CardHeader>
|
|
77
|
+
<CardTitle>Sign-ups</CardTitle>
|
|
78
|
+
<CardDescription>New accounts per day (UTC), last 7 days.</CardDescription>
|
|
79
|
+
</CardHeader>
|
|
80
|
+
<CardContent>
|
|
81
|
+
<div className="flex h-40 items-end gap-2" role="list">
|
|
82
|
+
{stats.signupsByDay.map((day, i) => (
|
|
83
|
+
<div
|
|
84
|
+
key={day.date}
|
|
85
|
+
role="listitem"
|
|
86
|
+
aria-label={`${day.date}: ${day.count} sign-ups`}
|
|
87
|
+
className="flex h-full flex-1 flex-col items-center justify-end gap-1"
|
|
88
|
+
>
|
|
89
|
+
<span className="text-xs text-muted-foreground tabular-nums">{day.count}</span>
|
|
90
|
+
<div className="flex w-full flex-1 items-end">
|
|
91
|
+
<AnimatedBar
|
|
92
|
+
vertical
|
|
93
|
+
value={(day.count / maxDaily) * 100}
|
|
94
|
+
delay={i * 0.05}
|
|
95
|
+
className="min-h-0.5 w-full"
|
|
96
|
+
/>
|
|
97
|
+
</div>
|
|
98
|
+
<span className="text-xs text-muted-foreground">
|
|
99
|
+
{dayFormat.format(new Date(`${day.date}T00:00:00Z`))}
|
|
100
|
+
</span>
|
|
101
|
+
</div>
|
|
102
|
+
))}
|
|
103
|
+
</div>
|
|
104
|
+
</CardContent>
|
|
105
|
+
</Card>
|
|
106
|
+
</div>
|
|
107
|
+
</>
|
|
108
|
+
);
|
|
109
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
|
|
3
|
+
import { Logo } from "@/components/logo";
|
|
4
|
+
import { ThemeToggle } from "@/components/theme-toggle";
|
|
5
|
+
|
|
6
|
+
export default function AuthLayout({ children }: { children: ReactNode }) {
|
|
7
|
+
return (
|
|
8
|
+
<div className="relative flex flex-1 flex-col">
|
|
9
|
+
<div
|
|
10
|
+
aria-hidden
|
|
11
|
+
className="pointer-events-none absolute inset-0 -z-10 bg-[radial-gradient(ellipse_at_top,var(--color-muted),transparent_60%)]"
|
|
12
|
+
/>
|
|
13
|
+
<header className="mx-auto flex h-14 w-full max-w-6xl items-center justify-between px-4">
|
|
14
|
+
<Logo />
|
|
15
|
+
<ThemeToggle />
|
|
16
|
+
</header>
|
|
17
|
+
<main className="flex flex-1 items-center justify-center px-4 py-10">
|
|
18
|
+
<div className="w-full max-w-sm">{children}</div>
|
|
19
|
+
</main>
|
|
20
|
+
</div>
|
|
21
|
+
);
|
|
22
|
+
}
|
|
@@ -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,20 @@
|
|
|
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
|
|
12
|
+
title="Create an account"
|
|
13
|
+
description="The first account registered becomes the admin."
|
|
14
|
+
>
|
|
15
|
+
<Suspense fallback={<AuthFormSkeleton fields={4} />}>
|
|
16
|
+
<RegisterForm />
|
|
17
|
+
</Suspense>
|
|
18
|
+
</AuthCard>
|
|
19
|
+
);
|
|
20
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { NextResponse } from "next/server";
|
|
2
|
+
|
|
3
|
+
import { ApiError, parseBody, withAuth } from "@/lib/api";
|
|
4
|
+
import { deleteUser, updateUserRole } from "@/lib/users";
|
|
5
|
+
import { updateRoleSchema } from "@/lib/validations/user";
|
|
6
|
+
|
|
7
|
+
type Ctx = RouteContext<"/api/admin/users/[id]">;
|
|
8
|
+
|
|
9
|
+
/** PATCH /api/admin/users/:id { "role": "manager" } (requires users:update-role) */
|
|
10
|
+
export const PATCH = withAuth<Ctx>(
|
|
11
|
+
async (req, ctx, actor) => {
|
|
12
|
+
const { id } = await ctx.params;
|
|
13
|
+
const { role } = await parseBody(req, updateRoleSchema);
|
|
14
|
+
|
|
15
|
+
if (id === actor.id) {
|
|
16
|
+
throw ApiError.badRequest("You can't change your own role", "CANNOT_MODIFY_SELF");
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const user = await updateUserRole(id, role);
|
|
20
|
+
if (!user) throw ApiError.notFound("User not found");
|
|
21
|
+
return NextResponse.json({ user });
|
|
22
|
+
},
|
|
23
|
+
{ permission: "users:update-role" },
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
/** DELETE /api/admin/users/:id (requires users:delete) */
|
|
27
|
+
export const DELETE = withAuth<Ctx>(
|
|
28
|
+
async (_req, ctx, actor) => {
|
|
29
|
+
const { id } = await ctx.params;
|
|
30
|
+
|
|
31
|
+
if (id === actor.id) {
|
|
32
|
+
throw ApiError.badRequest("You can't delete your own account", "CANNOT_MODIFY_SELF");
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const deleted = await deleteUser(id);
|
|
36
|
+
if (!deleted) throw ApiError.notFound("User not found");
|
|
37
|
+
return NextResponse.json({ ok: true, id });
|
|
38
|
+
},
|
|
39
|
+
{ permission: "users:delete" },
|
|
40
|
+
);
|
|
@@ -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,26 @@
|
|
|
1
|
+
import { NextResponse } from "next/server";
|
|
2
|
+
|
|
3
|
+
import { ApiError, apiHandler, parseBody } from "@/lib/api";
|
|
4
|
+
import { setSessionCookie, verifyPassword } from "@/lib/auth";
|
|
5
|
+
import { connectDB } from "@/lib/db";
|
|
6
|
+
import { toPublicUser } from "@/lib/users";
|
|
7
|
+
import { loginSchema } from "@/lib/validations/auth";
|
|
8
|
+
import { User } from "@/models/user";
|
|
9
|
+
|
|
10
|
+
export const POST = apiHandler(async (req) => {
|
|
11
|
+
const { email, password } = await parseBody(req, loginSchema);
|
|
12
|
+
|
|
13
|
+
await connectDB();
|
|
14
|
+
const user = await User.findOne({ email }).select("+password").lean();
|
|
15
|
+
|
|
16
|
+
// Always run bcrypt (against a dummy hash if the user doesn't exist) and
|
|
17
|
+
// return the same generic error, so attackers can't tell which emails exist.
|
|
18
|
+
const valid = await verifyPassword(password, user?.password);
|
|
19
|
+
if (!user || !valid) {
|
|
20
|
+
throw new ApiError(401, "Invalid email or password", "INVALID_CREDENTIALS");
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const publicUser = toPublicUser(user);
|
|
24
|
+
await setSessionCookie({ sub: publicUser.id, role: publicUser.role, name: publicUser.name });
|
|
25
|
+
return NextResponse.json({ user: publicUser });
|
|
26
|
+
});
|
|
@@ -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,19 @@
|
|
|
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 { 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
|
+
await setSessionCookie({ sub: user.id, role: user.role, name: user.name });
|
|
17
|
+
}
|
|
18
|
+
return NextResponse.json({ user, permissions: getPermissions(user.role), refreshed });
|
|
19
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { NextResponse } from "next/server";
|
|
2
|
+
|
|
3
|
+
import { DEFAULT_ROLE, FIRST_USER_ROLE } from "@/config/rbac";
|
|
4
|
+
import { ApiError, apiHandler, parseBody } from "@/lib/api";
|
|
5
|
+
import { hashPassword, setSessionCookie } from "@/lib/auth";
|
|
6
|
+
import { connectDB } from "@/lib/db";
|
|
7
|
+
import { claimFirstAdmin, isDuplicateKeyError, releaseFirstAdmin, toPublicUser } from "@/lib/users";
|
|
8
|
+
import { registerSchema } from "@/lib/validations/auth";
|
|
9
|
+
import { User } from "@/models/user";
|
|
10
|
+
|
|
11
|
+
const EMAIL_TAKEN = "An account with this email already exists";
|
|
12
|
+
|
|
13
|
+
export const POST = apiHandler(async (req) => {
|
|
14
|
+
const { name, email, password } = await parseBody(req, registerSchema);
|
|
15
|
+
|
|
16
|
+
await connectDB();
|
|
17
|
+
if (await User.exists({ email })) throw ApiError.conflict(EMAIL_TAKEN);
|
|
18
|
+
|
|
19
|
+
const passwordHash = await hashPassword(password);
|
|
20
|
+
// The very first account becomes admin; everyone after that is a regular user.
|
|
21
|
+
const isFirstUser = await claimFirstAdmin();
|
|
22
|
+
|
|
23
|
+
let created;
|
|
24
|
+
try {
|
|
25
|
+
created = await User.create({
|
|
26
|
+
name,
|
|
27
|
+
email,
|
|
28
|
+
password: passwordHash,
|
|
29
|
+
role: isFirstUser ? FIRST_USER_ROLE : DEFAULT_ROLE,
|
|
30
|
+
});
|
|
31
|
+
} catch (error) {
|
|
32
|
+
if (isFirstUser) await releaseFirstAdmin();
|
|
33
|
+
if (isDuplicateKeyError(error)) throw ApiError.conflict(EMAIL_TAKEN);
|
|
34
|
+
throw error;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const user = toPublicUser(created);
|
|
38
|
+
await setSessionCookie({ sub: user.id, role: user.role, name: user.name });
|
|
39
|
+
return NextResponse.json({ user }, { status: 201 });
|
|
40
|
+
});
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { connection, NextResponse } from "next/server";
|
|
2
|
+
|
|
3
|
+
import { connectDB } from "@/lib/db";
|
|
4
|
+
|
|
5
|
+
/** GET /api/health: liveness + database check (used by the Docker HEALTHCHECK). */
|
|
6
|
+
export async function GET() {
|
|
7
|
+
await connection(); // always run at request time, never prerender
|
|
8
|
+
|
|
9
|
+
const started = Date.now();
|
|
10
|
+
const base = { uptime: Math.round(process.uptime()), timestamp: new Date().toISOString() };
|
|
11
|
+
const headers = { "Cache-Control": "no-store" };
|
|
12
|
+
|
|
13
|
+
try {
|
|
14
|
+
const mongoose = await connectDB();
|
|
15
|
+
await mongoose.connection.db?.admin().ping();
|
|
16
|
+
return NextResponse.json(
|
|
17
|
+
{ status: "ok", database: "up", latencyMs: Date.now() - started, ...base },
|
|
18
|
+
{ headers },
|
|
19
|
+
);
|
|
20
|
+
} catch (error) {
|
|
21
|
+
console.error("[health] Database check failed:", error);
|
|
22
|
+
return NextResponse.json(
|
|
23
|
+
{ status: "error", database: "down", ...base },
|
|
24
|
+
{ status: 503, headers },
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
Binary file
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { ShieldAlert } from "lucide-react";
|
|
2
|
+
import type { Metadata } from "next";
|
|
3
|
+
import Link from "next/link";
|
|
4
|
+
import { Suspense } from "react";
|
|
5
|
+
|
|
6
|
+
import { SessionRefresher } from "@/components/auth/session-refresher";
|
|
7
|
+
import { Logo } from "@/components/logo";
|
|
8
|
+
import { Button } from "@/components/ui/button";
|
|
9
|
+
|
|
10
|
+
export const metadata: Metadata = { title: "Access denied" };
|
|
11
|
+
|
|
12
|
+
export default function ForbiddenPage() {
|
|
13
|
+
return (
|
|
14
|
+
<div className="flex flex-1 flex-col">
|
|
15
|
+
<header className="mx-auto flex h-14 w-full max-w-6xl items-center px-4">
|
|
16
|
+
<Logo />
|
|
17
|
+
</header>
|
|
18
|
+
<main className="flex flex-1 items-center justify-center px-4 py-16">
|
|
19
|
+
<div className="flex max-w-md flex-col items-center gap-6 text-center">
|
|
20
|
+
<div className="flex size-14 items-center justify-center rounded-full bg-destructive/10 text-destructive">
|
|
21
|
+
<ShieldAlert className="size-7" />
|
|
22
|
+
</div>
|
|
23
|
+
<div className="space-y-2">
|
|
24
|
+
<p className="font-mono text-sm text-muted-foreground">403</p>
|
|
25
|
+
<h1 className="text-2xl font-semibold tracking-tight">
|
|
26
|
+
You don't have access to this page
|
|
27
|
+
</h1>
|
|
28
|
+
<p className="text-sm text-muted-foreground">
|
|
29
|
+
Your role doesn't include the permission this page requires. If you think this is
|
|
30
|
+
a mistake, ask an admin to update your role.
|
|
31
|
+
</p>
|
|
32
|
+
</div>
|
|
33
|
+
{/* Reads ?from= with useSearchParams, so it needs a Suspense boundary. */}
|
|
34
|
+
<Suspense fallback={null}>
|
|
35
|
+
<SessionRefresher />
|
|
36
|
+
</Suspense>
|
|
37
|
+
<div className="flex gap-2">
|
|
38
|
+
<Button asChild>
|
|
39
|
+
<Link href="/dashboard">Go to dashboard</Link>
|
|
40
|
+
</Button>
|
|
41
|
+
<Button asChild variant="outline">
|
|
42
|
+
<Link href="/">Home</Link>
|
|
43
|
+
</Button>
|
|
44
|
+
</div>
|
|
45
|
+
</div>
|
|
46
|
+
</main>
|
|
47
|
+
</div>
|
|
48
|
+
);
|
|
49
|
+
}
|