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,82 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { zodResolver } from "@hookform/resolvers/zod";
|
|
4
|
+
import { LoaderCircle } from "lucide-react";
|
|
5
|
+
import Link from "next/link";
|
|
6
|
+
import { useRouter, useSearchParams } from "next/navigation";
|
|
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 { safeRedirectPath } from "@/lib/redirect";
|
|
14
|
+
import { loginSchema } from "@/lib/validations/auth";
|
|
15
|
+
import type { PublicUser } from "@/types/user";
|
|
16
|
+
|
|
17
|
+
export function LoginForm() {
|
|
18
|
+
const router = useRouter();
|
|
19
|
+
const searchParams = useSearchParams();
|
|
20
|
+
const next = safeRedirectPath(searchParams.get("next"));
|
|
21
|
+
|
|
22
|
+
const {
|
|
23
|
+
register,
|
|
24
|
+
handleSubmit,
|
|
25
|
+
formState: { errors, isSubmitting },
|
|
26
|
+
} = useForm({
|
|
27
|
+
resolver: zodResolver(loginSchema),
|
|
28
|
+
defaultValues: { email: "", password: "" },
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
const onSubmit = handleSubmit(async (values) => {
|
|
32
|
+
try {
|
|
33
|
+
const { user } = await apiFetch<{ user: PublicUser }>("/api/auth/login", {
|
|
34
|
+
method: "POST",
|
|
35
|
+
json: values,
|
|
36
|
+
});
|
|
37
|
+
toast.success(`Welcome back, ${user.name}!`);
|
|
38
|
+
router.replace(next);
|
|
39
|
+
router.refresh();
|
|
40
|
+
} catch (error) {
|
|
41
|
+
toast.error(getErrorMessage(error, "Could not sign in"));
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
const registerHref =
|
|
46
|
+
next === "/dashboard" ? "/register" : `/register?next=${encodeURIComponent(next)}`;
|
|
47
|
+
|
|
48
|
+
return (
|
|
49
|
+
<form onSubmit={onSubmit} className="grid gap-4" noValidate>
|
|
50
|
+
<FormField
|
|
51
|
+
id="email"
|
|
52
|
+
label="Email"
|
|
53
|
+
type="email"
|
|
54
|
+
autoComplete="email"
|
|
55
|
+
placeholder="you@example.com"
|
|
56
|
+
error={errors.email}
|
|
57
|
+
{...register("email")}
|
|
58
|
+
/>
|
|
59
|
+
<FormField
|
|
60
|
+
id="password"
|
|
61
|
+
label="Password"
|
|
62
|
+
type="password"
|
|
63
|
+
autoComplete="current-password"
|
|
64
|
+
error={errors.password}
|
|
65
|
+
{...register("password")}
|
|
66
|
+
/>
|
|
67
|
+
<Button type="submit" size="lg" disabled={isSubmitting} className="w-full">
|
|
68
|
+
{isSubmitting && <LoaderCircle className="animate-spin" />}
|
|
69
|
+
Sign in
|
|
70
|
+
</Button>
|
|
71
|
+
<p className="text-center text-sm text-muted-foreground">
|
|
72
|
+
Don't have an account?{" "}
|
|
73
|
+
<Link
|
|
74
|
+
href={registerHref}
|
|
75
|
+
className="font-medium text-foreground underline-offset-4 hover:underline"
|
|
76
|
+
>
|
|
77
|
+
Create one
|
|
78
|
+
</Link>
|
|
79
|
+
</p>
|
|
80
|
+
</form>
|
|
81
|
+
);
|
|
82
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { zodResolver } from "@hookform/resolvers/zod";
|
|
4
|
+
import { LoaderCircle } from "lucide-react";
|
|
5
|
+
import Link from "next/link";
|
|
6
|
+
import { useRouter, useSearchParams } from "next/navigation";
|
|
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 { ROLE_LABELS } from "@/config/rbac";
|
|
13
|
+
import { apiFetch, getErrorMessage, HttpError } from "@/lib/http";
|
|
14
|
+
import { safeRedirectPath } from "@/lib/redirect";
|
|
15
|
+
import { registerFormSchema } from "@/lib/validations/auth";
|
|
16
|
+
import type { PublicUser } from "@/types/user";
|
|
17
|
+
|
|
18
|
+
const FIELDS = ["name", "email", "password"] as const;
|
|
19
|
+
|
|
20
|
+
export function RegisterForm() {
|
|
21
|
+
const router = useRouter();
|
|
22
|
+
const searchParams = useSearchParams();
|
|
23
|
+
const next = safeRedirectPath(searchParams.get("next"));
|
|
24
|
+
|
|
25
|
+
const {
|
|
26
|
+
register,
|
|
27
|
+
handleSubmit,
|
|
28
|
+
setError,
|
|
29
|
+
formState: { errors, isSubmitting },
|
|
30
|
+
} = useForm({
|
|
31
|
+
resolver: zodResolver(registerFormSchema),
|
|
32
|
+
defaultValues: { name: "", email: "", password: "", confirmPassword: "" },
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
const onSubmit = handleSubmit(async ({ name, email, password }) => {
|
|
36
|
+
try {
|
|
37
|
+
const { user } = await apiFetch<{ user: PublicUser }>("/api/auth/register", {
|
|
38
|
+
method: "POST",
|
|
39
|
+
json: { name, email, password },
|
|
40
|
+
});
|
|
41
|
+
toast.success(
|
|
42
|
+
user.role === "admin"
|
|
43
|
+
? `Welcome, ${user.name}! As the first user, you are an ${ROLE_LABELS.admin}.`
|
|
44
|
+
: `Welcome, ${user.name}! Your account is ready.`,
|
|
45
|
+
);
|
|
46
|
+
router.replace(next);
|
|
47
|
+
router.refresh();
|
|
48
|
+
} catch (error) {
|
|
49
|
+
// Surface server-side validation errors on the matching fields.
|
|
50
|
+
if (error instanceof HttpError && error.fieldErrors) {
|
|
51
|
+
for (const field of FIELDS) {
|
|
52
|
+
const message = error.fieldErrors[field]?.[0];
|
|
53
|
+
if (message) setError(field, { message });
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
if (error instanceof HttpError && error.status === 409) {
|
|
57
|
+
setError("email", { message: error.message });
|
|
58
|
+
}
|
|
59
|
+
toast.error(getErrorMessage(error, "Could not create your account"));
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
return (
|
|
64
|
+
<form onSubmit={onSubmit} className="grid gap-4" noValidate>
|
|
65
|
+
<FormField
|
|
66
|
+
id="name"
|
|
67
|
+
label="Name"
|
|
68
|
+
autoComplete="name"
|
|
69
|
+
placeholder="Ada Lovelace"
|
|
70
|
+
error={errors.name}
|
|
71
|
+
{...register("name")}
|
|
72
|
+
/>
|
|
73
|
+
<FormField
|
|
74
|
+
id="email"
|
|
75
|
+
label="Email"
|
|
76
|
+
type="email"
|
|
77
|
+
autoComplete="email"
|
|
78
|
+
placeholder="you@example.com"
|
|
79
|
+
error={errors.email}
|
|
80
|
+
{...register("email")}
|
|
81
|
+
/>
|
|
82
|
+
<FormField
|
|
83
|
+
id="password"
|
|
84
|
+
label="Password"
|
|
85
|
+
type="password"
|
|
86
|
+
autoComplete="new-password"
|
|
87
|
+
placeholder="At least 8 characters"
|
|
88
|
+
error={errors.password}
|
|
89
|
+
{...register("password")}
|
|
90
|
+
/>
|
|
91
|
+
<FormField
|
|
92
|
+
id="confirmPassword"
|
|
93
|
+
label="Confirm password"
|
|
94
|
+
type="password"
|
|
95
|
+
autoComplete="new-password"
|
|
96
|
+
error={errors.confirmPassword}
|
|
97
|
+
{...register("confirmPassword")}
|
|
98
|
+
/>
|
|
99
|
+
<Button type="submit" size="lg" disabled={isSubmitting} className="w-full">
|
|
100
|
+
{isSubmitting && <LoaderCircle className="animate-spin" />}
|
|
101
|
+
Create account
|
|
102
|
+
</Button>
|
|
103
|
+
<p className="text-center text-sm text-muted-foreground">
|
|
104
|
+
Already have an account?{" "}
|
|
105
|
+
<Link
|
|
106
|
+
href="/login"
|
|
107
|
+
className="font-medium text-foreground underline-offset-4 hover:underline"
|
|
108
|
+
>
|
|
109
|
+
Sign in
|
|
110
|
+
</Link>
|
|
111
|
+
</p>
|
|
112
|
+
</form>
|
|
113
|
+
);
|
|
114
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { RefreshCw } from "lucide-react";
|
|
4
|
+
import { useRouter, useSearchParams } from "next/navigation";
|
|
5
|
+
import { useEffect, useState } from "react";
|
|
6
|
+
|
|
7
|
+
import { Button } from "@/components/ui/button";
|
|
8
|
+
import { safeRedirectPath } from "@/lib/redirect";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Shown on /forbidden. The proxy decides using the role inside the JWT, which
|
|
12
|
+
* can be out of date right after an admin promotes you. This asks the server
|
|
13
|
+
* to re-issue the token and, if the role changed, offers to retry the page.
|
|
14
|
+
*/
|
|
15
|
+
export function SessionRefresher() {
|
|
16
|
+
const router = useRouter();
|
|
17
|
+
const searchParams = useSearchParams();
|
|
18
|
+
const target = safeRedirectPath(searchParams.get("from"));
|
|
19
|
+
const [refreshed, setRefreshed] = useState(false);
|
|
20
|
+
|
|
21
|
+
useEffect(() => {
|
|
22
|
+
let cancelled = false;
|
|
23
|
+
fetch("/api/auth/me", { cache: "no-store" })
|
|
24
|
+
.then((res) => (res.ok ? res.json() : null))
|
|
25
|
+
.then((data: { refreshed?: boolean } | null) => {
|
|
26
|
+
if (!cancelled && data?.refreshed) setRefreshed(true);
|
|
27
|
+
})
|
|
28
|
+
.catch(() => {});
|
|
29
|
+
return () => {
|
|
30
|
+
cancelled = true;
|
|
31
|
+
};
|
|
32
|
+
}, []);
|
|
33
|
+
|
|
34
|
+
if (!refreshed) return null;
|
|
35
|
+
|
|
36
|
+
return (
|
|
37
|
+
<div className="flex flex-col items-center gap-3 rounded-lg border bg-muted/40 p-4 text-sm">
|
|
38
|
+
<p>Your role was just updated. Try the page again.</p>
|
|
39
|
+
<Button size="sm" onClick={() => router.push(target)}>
|
|
40
|
+
<RefreshCw /> Try again
|
|
41
|
+
</Button>
|
|
42
|
+
</div>
|
|
43
|
+
);
|
|
44
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { LoaderCircle, LogOut } from "lucide-react";
|
|
4
|
+
import { useRouter } from "next/navigation";
|
|
5
|
+
import { useState } from "react";
|
|
6
|
+
import { toast } from "sonner";
|
|
7
|
+
|
|
8
|
+
import { Button } from "@/components/ui/button";
|
|
9
|
+
import { apiFetch, getErrorMessage } from "@/lib/http";
|
|
10
|
+
|
|
11
|
+
export function LogoutButton() {
|
|
12
|
+
const router = useRouter();
|
|
13
|
+
const [pending, setPending] = useState(false);
|
|
14
|
+
|
|
15
|
+
async function logout() {
|
|
16
|
+
setPending(true);
|
|
17
|
+
try {
|
|
18
|
+
await apiFetch("/api/auth/logout", { method: "POST" });
|
|
19
|
+
toast.success("Signed out");
|
|
20
|
+
router.replace("/login");
|
|
21
|
+
router.refresh();
|
|
22
|
+
} catch (error) {
|
|
23
|
+
toast.error(getErrorMessage(error, "Could not sign out"));
|
|
24
|
+
setPending(false);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return (
|
|
29
|
+
<Button variant="outline" size="sm" onClick={logout} disabled={pending}>
|
|
30
|
+
{pending ? <LoaderCircle className="animate-spin" /> : <LogOut />}
|
|
31
|
+
<span className="hidden sm:inline">Sign out</span>
|
|
32
|
+
</Button>
|
|
33
|
+
);
|
|
34
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { ChartColumn, LayoutDashboard, Users, type LucideIcon } from "lucide-react";
|
|
4
|
+
import Link from "next/link";
|
|
5
|
+
import { usePathname } from "next/navigation";
|
|
6
|
+
|
|
7
|
+
import { LogoutButton } from "@/components/layout/logout-button";
|
|
8
|
+
import { Logo } from "@/components/logo";
|
|
9
|
+
import { useUser } from "@/components/providers/user-provider";
|
|
10
|
+
import { RoleBadge } from "@/components/role-badge";
|
|
11
|
+
import { ThemeToggle } from "@/components/theme-toggle";
|
|
12
|
+
import { getNavItems } from "@/config/rbac";
|
|
13
|
+
import { cn } from "@/lib/utils";
|
|
14
|
+
|
|
15
|
+
const ICONS: Record<string, LucideIcon> = {
|
|
16
|
+
"/dashboard": LayoutDashboard,
|
|
17
|
+
"/reports": ChartColumn,
|
|
18
|
+
"/admin": Users,
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/** App navbar: only shows links the current role is permitted to open. */
|
|
22
|
+
export function Navbar() {
|
|
23
|
+
const user = useUser();
|
|
24
|
+
const pathname = usePathname();
|
|
25
|
+
const items = getNavItems(user.role);
|
|
26
|
+
|
|
27
|
+
const links = items.map((item) => {
|
|
28
|
+
const Icon = ICONS[item.href];
|
|
29
|
+
const active = pathname === item.href || pathname.startsWith(`${item.href}/`);
|
|
30
|
+
return (
|
|
31
|
+
<Link
|
|
32
|
+
key={item.href}
|
|
33
|
+
href={item.href}
|
|
34
|
+
aria-current={active ? "page" : undefined}
|
|
35
|
+
className={cn(
|
|
36
|
+
"flex shrink-0 items-center gap-1.5 rounded-md px-3 py-1.5 text-sm font-medium transition-colors",
|
|
37
|
+
active
|
|
38
|
+
? "bg-secondary text-foreground"
|
|
39
|
+
: "text-muted-foreground hover:bg-secondary/60 hover:text-foreground",
|
|
40
|
+
)}
|
|
41
|
+
>
|
|
42
|
+
{Icon && <Icon className="size-4" />}
|
|
43
|
+
{item.label}
|
|
44
|
+
</Link>
|
|
45
|
+
);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
return (
|
|
49
|
+
<header className="sticky top-0 z-40 border-b bg-background/80 backdrop-blur supports-backdrop-filter:bg-background/60">
|
|
50
|
+
<div className="mx-auto flex h-14 w-full max-w-6xl items-center gap-4 px-4">
|
|
51
|
+
<Logo href="/dashboard" />
|
|
52
|
+
<nav className="hidden items-center gap-1 md:flex">{links}</nav>
|
|
53
|
+
<div className="ml-auto flex items-center gap-2">
|
|
54
|
+
<div className="hidden min-w-0 items-center gap-2 sm:flex">
|
|
55
|
+
<span className="max-w-40 truncate text-sm font-medium">{user.name}</span>
|
|
56
|
+
<RoleBadge role={user.role} />
|
|
57
|
+
</div>
|
|
58
|
+
<ThemeToggle />
|
|
59
|
+
<LogoutButton />
|
|
60
|
+
</div>
|
|
61
|
+
</div>
|
|
62
|
+
<nav className="no-scrollbar flex items-center gap-1 overflow-x-auto border-t px-4 py-2 md:hidden">
|
|
63
|
+
{links}
|
|
64
|
+
</nav>
|
|
65
|
+
</header>
|
|
66
|
+
);
|
|
67
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { ReactNode } from "react";
|
|
2
|
+
|
|
3
|
+
export function PageHeader({
|
|
4
|
+
title,
|
|
5
|
+
description,
|
|
6
|
+
actions,
|
|
7
|
+
}: {
|
|
8
|
+
title: string;
|
|
9
|
+
description?: ReactNode;
|
|
10
|
+
actions?: ReactNode;
|
|
11
|
+
}) {
|
|
12
|
+
return (
|
|
13
|
+
<div className="mb-8 flex flex-col gap-4 sm:flex-row sm:items-end sm:justify-between">
|
|
14
|
+
<div className="space-y-1">
|
|
15
|
+
<h1 className="text-2xl font-semibold tracking-tight">{title}</h1>
|
|
16
|
+
{description && <p className="text-sm text-muted-foreground">{description}</p>}
|
|
17
|
+
</div>
|
|
18
|
+
{actions && <div className="flex items-center gap-2">{actions}</div>}
|
|
19
|
+
</div>
|
|
20
|
+
);
|
|
21
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Skeleton } from "@/components/ui/skeleton";
|
|
2
|
+
|
|
3
|
+
export function PageSkeleton() {
|
|
4
|
+
return (
|
|
5
|
+
<div className="space-y-6" aria-busy="true" aria-label="Loading">
|
|
6
|
+
<div className="space-y-2">
|
|
7
|
+
<Skeleton className="h-8 w-56" />
|
|
8
|
+
<Skeleton className="h-4 w-80 max-w-full" />
|
|
9
|
+
</div>
|
|
10
|
+
<div className="grid gap-4 md:grid-cols-3">
|
|
11
|
+
<Skeleton className="h-32" />
|
|
12
|
+
<Skeleton className="h-32" />
|
|
13
|
+
<Skeleton className="h-32" />
|
|
14
|
+
</div>
|
|
15
|
+
<Skeleton className="h-64" />
|
|
16
|
+
</div>
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function ShellSkeleton() {
|
|
21
|
+
return (
|
|
22
|
+
<>
|
|
23
|
+
<div className="border-b">
|
|
24
|
+
<div className="mx-auto flex h-14 w-full max-w-6xl items-center gap-4 px-4">
|
|
25
|
+
<Skeleton className="h-7 w-32" />
|
|
26
|
+
<Skeleton className="hidden h-7 w-64 md:block" />
|
|
27
|
+
<Skeleton className="ml-auto h-7 w-40" />
|
|
28
|
+
</div>
|
|
29
|
+
</div>
|
|
30
|
+
<div className="mx-auto w-full max-w-6xl flex-1 px-4 py-8">
|
|
31
|
+
<PageSkeleton />
|
|
32
|
+
</div>
|
|
33
|
+
</>
|
|
34
|
+
);
|
|
35
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { ShieldCheck } from "lucide-react";
|
|
2
|
+
import Link from "next/link";
|
|
3
|
+
|
|
4
|
+
import { siteConfig } from "@/config/site";
|
|
5
|
+
import { cn } from "@/lib/utils";
|
|
6
|
+
|
|
7
|
+
export function Logo({ href = "/", className }: { href?: string; className?: string }) {
|
|
8
|
+
return (
|
|
9
|
+
<Link
|
|
10
|
+
href={href}
|
|
11
|
+
className={cn("flex items-center gap-2 font-semibold tracking-tight", className)}
|
|
12
|
+
>
|
|
13
|
+
<span className="flex size-7 items-center justify-center rounded-lg bg-primary text-primary-foreground">
|
|
14
|
+
<ShieldCheck className="size-4" />
|
|
15
|
+
</span>
|
|
16
|
+
<span>{siteConfig.name}</span>
|
|
17
|
+
</Link>
|
|
18
|
+
);
|
|
19
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { motion, type HTMLMotionProps } from "motion/react";
|
|
4
|
+
|
|
5
|
+
type FadeInProps = HTMLMotionProps<"div"> & { delay?: number; y?: number };
|
|
6
|
+
|
|
7
|
+
/** Fades and slides children in on mount. Respects prefers-reduced-motion via MotionConfig defaults. */
|
|
8
|
+
export function FadeIn({ delay = 0, y = 12, children, ...props }: FadeInProps) {
|
|
9
|
+
return (
|
|
10
|
+
<motion.div
|
|
11
|
+
initial={{ opacity: 0, y }}
|
|
12
|
+
animate={{ opacity: 1, y: 0 }}
|
|
13
|
+
transition={{ duration: 0.45, delay, ease: [0.21, 0.47, 0.32, 0.98] }}
|
|
14
|
+
{...props}
|
|
15
|
+
>
|
|
16
|
+
{children}
|
|
17
|
+
</motion.div>
|
|
18
|
+
);
|
|
19
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useRouter } from "next/navigation";
|
|
4
|
+
import { createContext, use, useEffect, type ReactNode } from "react";
|
|
5
|
+
|
|
6
|
+
import { hasPermission, type Permission } from "@/config/rbac";
|
|
7
|
+
import type { PublicUser } from "@/types/user";
|
|
8
|
+
|
|
9
|
+
const UserContext = createContext<PublicUser | null>(null);
|
|
10
|
+
|
|
11
|
+
type UserProviderProps = {
|
|
12
|
+
user: PublicUser;
|
|
13
|
+
/** True when the JWT's role differs from the DB; triggers a token refresh. */
|
|
14
|
+
sessionStale?: boolean;
|
|
15
|
+
children: ReactNode;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Makes the signed-in user (loaded from the DB on the server) available to
|
|
20
|
+
* Client Components via useUser() and <Can>.
|
|
21
|
+
*/
|
|
22
|
+
export function UserProvider({ user, sessionStale = false, children }: UserProviderProps) {
|
|
23
|
+
const router = useRouter();
|
|
24
|
+
|
|
25
|
+
// If an admin changed this user's role, re-issue the JWT so the proxy's
|
|
26
|
+
// optimistic route checks match the database again.
|
|
27
|
+
useEffect(() => {
|
|
28
|
+
if (!sessionStale) return;
|
|
29
|
+
let cancelled = false;
|
|
30
|
+
fetch("/api/auth/me", { cache: "no-store" })
|
|
31
|
+
.then((res) => (res.ok ? res.json() : null))
|
|
32
|
+
.then((data: { refreshed?: boolean } | null) => {
|
|
33
|
+
if (!cancelled && data?.refreshed) router.refresh();
|
|
34
|
+
})
|
|
35
|
+
.catch(() => {});
|
|
36
|
+
return () => {
|
|
37
|
+
cancelled = true;
|
|
38
|
+
};
|
|
39
|
+
}, [sessionStale, router]);
|
|
40
|
+
|
|
41
|
+
return <UserContext value={user}>{children}</UserContext>;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** The signed-in user. Must be used inside the authenticated (app) layout. */
|
|
45
|
+
export function useUser(): PublicUser {
|
|
46
|
+
const user = use(UserContext);
|
|
47
|
+
if (!user) throw new Error("useUser() must be used within <UserProvider>");
|
|
48
|
+
return user;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** The signed-in user, or null outside the authenticated layout. */
|
|
52
|
+
export function useOptionalUser(): PublicUser | null {
|
|
53
|
+
return use(UserContext);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Whether the current user has `permission` (UI only; the server re-checks). */
|
|
57
|
+
export function usePermission(permission: Permission): boolean {
|
|
58
|
+
const user = useOptionalUser();
|
|
59
|
+
return hasPermission(user?.role, permission);
|
|
60
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { motion } from "motion/react";
|
|
4
|
+
|
|
5
|
+
import { cn } from "@/lib/utils";
|
|
6
|
+
|
|
7
|
+
/** A bar that grows to `value` percent (0-100). Horizontal by default. */
|
|
8
|
+
export function AnimatedBar({
|
|
9
|
+
value,
|
|
10
|
+
vertical = false,
|
|
11
|
+
delay = 0,
|
|
12
|
+
className,
|
|
13
|
+
}: {
|
|
14
|
+
value: number;
|
|
15
|
+
vertical?: boolean;
|
|
16
|
+
delay?: number;
|
|
17
|
+
className?: string;
|
|
18
|
+
}) {
|
|
19
|
+
const size = `${Math.max(0, Math.min(100, value))}%`;
|
|
20
|
+
return (
|
|
21
|
+
<motion.div
|
|
22
|
+
className={cn("rounded-md bg-primary", className)}
|
|
23
|
+
initial={vertical ? { height: 0 } : { width: 0 }}
|
|
24
|
+
animate={vertical ? { height: size } : { width: size }}
|
|
25
|
+
transition={{ duration: 0.7, delay, ease: "easeOut" }}
|
|
26
|
+
/>
|
|
27
|
+
);
|
|
28
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { ROLE_LABELS, type Role } from "@/config/rbac";
|
|
2
|
+
import { Badge } from "@/components/ui/badge";
|
|
3
|
+
import { cn } from "@/lib/utils";
|
|
4
|
+
|
|
5
|
+
const ROLE_STYLES: Record<Role, string> = {
|
|
6
|
+
user: "border-border bg-secondary text-secondary-foreground",
|
|
7
|
+
manager: "border-sky-500/30 bg-sky-500/10 text-sky-700 dark:text-sky-300",
|
|
8
|
+
admin: "border-violet-500/30 bg-violet-500/10 text-violet-700 dark:text-violet-300",
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export function RoleBadge({ role, className }: { role: Role; className?: string }) {
|
|
12
|
+
return (
|
|
13
|
+
<Badge variant="outline" className={cn(ROLE_STYLES[role], className)}>
|
|
14
|
+
{ROLE_LABELS[role]}
|
|
15
|
+
</Badge>
|
|
16
|
+
);
|
|
17
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { Moon, Sun } from "lucide-react";
|
|
4
|
+
|
|
5
|
+
import { Button } from "@/components/ui/button";
|
|
6
|
+
import { THEME_STORAGE_KEY } from "@/lib/theme";
|
|
7
|
+
|
|
8
|
+
/** Toggles light/dark. Stateless: the icons are swapped purely with CSS, so there's no hydration mismatch. */
|
|
9
|
+
export function ThemeToggle() {
|
|
10
|
+
function toggle() {
|
|
11
|
+
const isDark = document.documentElement.classList.toggle("dark");
|
|
12
|
+
try {
|
|
13
|
+
localStorage.setItem(THEME_STORAGE_KEY, isDark ? "dark" : "light");
|
|
14
|
+
} catch {
|
|
15
|
+
// Storage unavailable (private mode); the choice just won't persist.
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return (
|
|
20
|
+
<Button variant="ghost" size="icon" onClick={toggle} aria-label="Toggle theme">
|
|
21
|
+
<Sun className="hidden dark:block" />
|
|
22
|
+
<Moon className="dark:hidden" />
|
|
23
|
+
</Button>
|
|
24
|
+
);
|
|
25
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import { cva, type VariantProps } from "class-variance-authority";
|
|
3
|
+
import { cn } from "@/lib/utils";
|
|
4
|
+
import { Slot } from "radix-ui";
|
|
5
|
+
|
|
6
|
+
const badgeVariants = cva(
|
|
7
|
+
"group/badge inline-flex h-5 w-fit shrink-0 items-center justify-center gap-1 overflow-hidden rounded-4xl border border-transparent px-2 py-0.5 text-xs font-medium whitespace-nowrap transition-all focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 has-data-[icon=inline-end]:pr-1.5 has-data-[icon=inline-start]:pl-1.5 aria-invalid:border-destructive aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 [&>svg]:pointer-events-none [&>svg]:size-3!",
|
|
8
|
+
{
|
|
9
|
+
variants: {
|
|
10
|
+
variant: {
|
|
11
|
+
default: "bg-primary text-primary-foreground [a]:hover:bg-primary/80",
|
|
12
|
+
secondary: "bg-secondary text-secondary-foreground [a]:hover:bg-secondary/80",
|
|
13
|
+
destructive:
|
|
14
|
+
"bg-destructive/10 text-destructive focus-visible:ring-destructive/20 dark:bg-destructive/20 dark:focus-visible:ring-destructive/40 [a]:hover:bg-destructive/20",
|
|
15
|
+
outline: "border-border text-foreground [a]:hover:bg-muted [a]:hover:text-muted-foreground",
|
|
16
|
+
ghost: "hover:bg-muted hover:text-muted-foreground dark:hover:bg-muted/50",
|
|
17
|
+
link: "text-primary underline-offset-4 hover:underline",
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
defaultVariants: {
|
|
21
|
+
variant: "default",
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
function Badge({
|
|
27
|
+
className,
|
|
28
|
+
variant = "default",
|
|
29
|
+
asChild = false,
|
|
30
|
+
...props
|
|
31
|
+
}: React.ComponentProps<"span"> & VariantProps<typeof badgeVariants> & { asChild?: boolean }) {
|
|
32
|
+
const Comp = asChild ? Slot.Root : "span";
|
|
33
|
+
|
|
34
|
+
return (
|
|
35
|
+
<Comp
|
|
36
|
+
data-slot="badge"
|
|
37
|
+
data-variant={variant}
|
|
38
|
+
className={cn(badgeVariants({ variant }), className)}
|
|
39
|
+
{...props}
|
|
40
|
+
/>
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export { Badge, badgeVariants };
|