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,87 @@
|
|
|
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 { PasswordInput } from "@/components/auth/password-input";
|
|
12
|
+
import { Button } from "@/components/ui/button";
|
|
13
|
+
import { apiFetch, getErrorMessage } from "@/lib/http";
|
|
14
|
+
import { safeRedirectPath } from "@/lib/redirect";
|
|
15
|
+
import { loginSchema } from "@/lib/validations/auth";
|
|
16
|
+
import type { PublicUser } from "@/types/user";
|
|
17
|
+
|
|
18
|
+
export function LoginForm() {
|
|
19
|
+
const router = useRouter();
|
|
20
|
+
const searchParams = useSearchParams();
|
|
21
|
+
const next = safeRedirectPath(searchParams.get("next"));
|
|
22
|
+
|
|
23
|
+
const {
|
|
24
|
+
register,
|
|
25
|
+
handleSubmit,
|
|
26
|
+
formState: { errors, isSubmitting },
|
|
27
|
+
} = useForm({
|
|
28
|
+
resolver: zodResolver(loginSchema),
|
|
29
|
+
defaultValues: { email: "", password: "" },
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const onSubmit = handleSubmit(async (values) => {
|
|
33
|
+
try {
|
|
34
|
+
const { user } = await apiFetch<{ user: PublicUser }>("/api/auth/login", {
|
|
35
|
+
method: "POST",
|
|
36
|
+
json: values,
|
|
37
|
+
});
|
|
38
|
+
toast.success(`Welcome back, ${user.name}!`);
|
|
39
|
+
router.replace(next);
|
|
40
|
+
router.refresh();
|
|
41
|
+
} catch (error) {
|
|
42
|
+
toast.error(getErrorMessage(error, "Could not sign in"));
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
const registerHref =
|
|
47
|
+
next === "/dashboard" ? "/register" : `/register?next=${encodeURIComponent(next)}`;
|
|
48
|
+
|
|
49
|
+
return (
|
|
50
|
+
<form onSubmit={onSubmit} className="form" noValidate>
|
|
51
|
+
<FormField
|
|
52
|
+
id="email"
|
|
53
|
+
label="Email"
|
|
54
|
+
type="email"
|
|
55
|
+
autoComplete="email"
|
|
56
|
+
placeholder="you@example.com"
|
|
57
|
+
error={errors.email}
|
|
58
|
+
{...register("email")}
|
|
59
|
+
/>
|
|
60
|
+
<FormField
|
|
61
|
+
id="password"
|
|
62
|
+
label="Password"
|
|
63
|
+
autoComplete="current-password"
|
|
64
|
+
error={errors.password}
|
|
65
|
+
// @feature email
|
|
66
|
+
labelAside={
|
|
67
|
+
<Link href="/forgot-password" className="text-link text-sm">
|
|
68
|
+
Forgot password?
|
|
69
|
+
</Link>
|
|
70
|
+
}
|
|
71
|
+
// @end email
|
|
72
|
+
render={(props) => <PasswordInput {...props} />}
|
|
73
|
+
{...register("password")}
|
|
74
|
+
/>
|
|
75
|
+
<Button type="submit" size="lg" disabled={isSubmitting} className="full-width">
|
|
76
|
+
{isSubmitting && <LoaderCircle className="spin" />}
|
|
77
|
+
Sign in
|
|
78
|
+
</Button>
|
|
79
|
+
<p className="form-footer">
|
|
80
|
+
Don't have an account?{" "}
|
|
81
|
+
<Link href={registerHref} className="text-link">
|
|
82
|
+
Create one
|
|
83
|
+
</Link>
|
|
84
|
+
</p>
|
|
85
|
+
</form>
|
|
86
|
+
);
|
|
87
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { Eye, EyeOff } from "lucide-react";
|
|
4
|
+
import { useState, type ComponentProps } from "react";
|
|
5
|
+
|
|
6
|
+
import { Input } from "@/components/ui/input";
|
|
7
|
+
import { getPasswordStrength } from "@/lib/password-strength";
|
|
8
|
+
import { cn } from "@/lib/utils";
|
|
9
|
+
|
|
10
|
+
/** Password input with a show/hide button. */
|
|
11
|
+
export function PasswordInput({ className, ...props }: ComponentProps<typeof Input>) {
|
|
12
|
+
const [visible, setVisible] = useState(false);
|
|
13
|
+
return (
|
|
14
|
+
<div className="password-input">
|
|
15
|
+
<Input {...props} type={visible ? "text" : "password"} className={cn("password-input-field", className)} />
|
|
16
|
+
<button
|
|
17
|
+
type="button"
|
|
18
|
+
className="password-toggle"
|
|
19
|
+
onClick={() => setVisible((v) => !v)}
|
|
20
|
+
aria-label={visible ? "Hide password" : "Show password"}
|
|
21
|
+
aria-pressed={visible}
|
|
22
|
+
>
|
|
23
|
+
{visible ? <EyeOff size={16} /> : <Eye size={16} />}
|
|
24
|
+
</button>
|
|
25
|
+
</div>
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Four-segment strength meter shown under new-password fields. */
|
|
30
|
+
export function PasswordStrengthMeter({ password }: { password: string }) {
|
|
31
|
+
const { score, label } = getPasswordStrength(password);
|
|
32
|
+
if (!password) return null;
|
|
33
|
+
return (
|
|
34
|
+
<div className="strength" data-score={score} aria-live="polite">
|
|
35
|
+
<div className="strength-bars" aria-hidden>
|
|
36
|
+
{[1, 2, 3, 4].map((i) => (
|
|
37
|
+
<span key={i} className={cn("strength-bar", i <= score && "strength-bar--on")} />
|
|
38
|
+
))}
|
|
39
|
+
</div>
|
|
40
|
+
<span className="strength-label">Password strength: {label}</span>
|
|
41
|
+
</div>
|
|
42
|
+
);
|
|
43
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
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, useWatch } from "react-hook-form";
|
|
8
|
+
import { toast } from "sonner";
|
|
9
|
+
|
|
10
|
+
import { FormField } from "@/components/auth/form-field";
|
|
11
|
+
import { PasswordInput, PasswordStrengthMeter } from "@/components/auth/password-input";
|
|
12
|
+
import { Button } from "@/components/ui/button";
|
|
13
|
+
import { ROLE_LABELS } from "@/config/rbac";
|
|
14
|
+
import { apiFetch, getErrorMessage, HttpError } from "@/lib/http";
|
|
15
|
+
import { safeRedirectPath } from "@/lib/redirect";
|
|
16
|
+
import { registerFormSchema } from "@/lib/validations/auth";
|
|
17
|
+
import type { PublicUser } from "@/types/user";
|
|
18
|
+
|
|
19
|
+
const FIELDS = ["name", "email", "password"] as const;
|
|
20
|
+
|
|
21
|
+
export function RegisterForm() {
|
|
22
|
+
const router = useRouter();
|
|
23
|
+
const searchParams = useSearchParams();
|
|
24
|
+
const next = safeRedirectPath(searchParams.get("next"));
|
|
25
|
+
|
|
26
|
+
const {
|
|
27
|
+
register,
|
|
28
|
+
handleSubmit,
|
|
29
|
+
setError,
|
|
30
|
+
control,
|
|
31
|
+
formState: { errors, isSubmitting },
|
|
32
|
+
} = useForm({
|
|
33
|
+
resolver: zodResolver(registerFormSchema),
|
|
34
|
+
defaultValues: { name: "", email: "", password: "", confirmPassword: "" },
|
|
35
|
+
});
|
|
36
|
+
const password = useWatch({ control, name: "password" });
|
|
37
|
+
|
|
38
|
+
const onSubmit = handleSubmit(async ({ name, email, password }) => {
|
|
39
|
+
try {
|
|
40
|
+
const { user } = await apiFetch<{ user: PublicUser }>("/api/auth/register", {
|
|
41
|
+
method: "POST",
|
|
42
|
+
json: { name, email, password },
|
|
43
|
+
});
|
|
44
|
+
toast.success(
|
|
45
|
+
user.role === "admin"
|
|
46
|
+
? `Welcome, ${user.name}! As the first user, you are an ${ROLE_LABELS.admin}.`
|
|
47
|
+
: `Welcome, ${user.name}! Your account is ready.`,
|
|
48
|
+
);
|
|
49
|
+
router.replace(next);
|
|
50
|
+
router.refresh();
|
|
51
|
+
} catch (error) {
|
|
52
|
+
// Surface server-side validation errors on the matching fields.
|
|
53
|
+
if (error instanceof HttpError && error.fieldErrors) {
|
|
54
|
+
for (const field of FIELDS) {
|
|
55
|
+
const message = error.fieldErrors[field]?.[0];
|
|
56
|
+
if (message) setError(field, { message });
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
if (error instanceof HttpError && error.status === 409) {
|
|
60
|
+
setError("email", { message: error.message });
|
|
61
|
+
}
|
|
62
|
+
toast.error(getErrorMessage(error, "Could not create your account"));
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
return (
|
|
67
|
+
<form onSubmit={onSubmit} className="form" noValidate>
|
|
68
|
+
<FormField
|
|
69
|
+
id="name"
|
|
70
|
+
label="Name"
|
|
71
|
+
autoComplete="name"
|
|
72
|
+
placeholder="Ada Lovelace"
|
|
73
|
+
error={errors.name}
|
|
74
|
+
{...register("name")}
|
|
75
|
+
/>
|
|
76
|
+
<FormField
|
|
77
|
+
id="email"
|
|
78
|
+
label="Email"
|
|
79
|
+
type="email"
|
|
80
|
+
autoComplete="email"
|
|
81
|
+
placeholder="you@example.com"
|
|
82
|
+
error={errors.email}
|
|
83
|
+
{...register("email")}
|
|
84
|
+
/>
|
|
85
|
+
<div className="field-group">
|
|
86
|
+
<FormField
|
|
87
|
+
id="password"
|
|
88
|
+
label="Password"
|
|
89
|
+
autoComplete="new-password"
|
|
90
|
+
placeholder="At least 8 characters"
|
|
91
|
+
error={errors.password}
|
|
92
|
+
render={(props) => <PasswordInput {...props} />}
|
|
93
|
+
{...register("password")}
|
|
94
|
+
/>
|
|
95
|
+
<PasswordStrengthMeter password={password} />
|
|
96
|
+
</div>
|
|
97
|
+
<FormField
|
|
98
|
+
id="confirmPassword"
|
|
99
|
+
label="Confirm password"
|
|
100
|
+
autoComplete="new-password"
|
|
101
|
+
error={errors.confirmPassword}
|
|
102
|
+
render={(props) => <PasswordInput {...props} />}
|
|
103
|
+
{...register("confirmPassword")}
|
|
104
|
+
/>
|
|
105
|
+
<Button type="submit" size="lg" disabled={isSubmitting} className="full-width">
|
|
106
|
+
{isSubmitting && <LoaderCircle className="spin" />}
|
|
107
|
+
Create account
|
|
108
|
+
</Button>
|
|
109
|
+
<p className="form-footer">
|
|
110
|
+
Already have an account?{" "}
|
|
111
|
+
<Link href="/login" className="text-link">
|
|
112
|
+
Sign in
|
|
113
|
+
</Link>
|
|
114
|
+
</p>
|
|
115
|
+
</form>
|
|
116
|
+
);
|
|
117
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
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, useWatch } from "react-hook-form";
|
|
8
|
+
import { toast } from "sonner";
|
|
9
|
+
|
|
10
|
+
import { FormField } from "@/components/auth/form-field";
|
|
11
|
+
import { PasswordInput, PasswordStrengthMeter } from "@/components/auth/password-input";
|
|
12
|
+
import { Button } from "@/components/ui/button";
|
|
13
|
+
import { apiFetch, getErrorMessage } from "@/lib/http";
|
|
14
|
+
import { resetPasswordFormSchema } from "@/lib/validations/auth";
|
|
15
|
+
|
|
16
|
+
export function ResetPasswordForm() {
|
|
17
|
+
const router = useRouter();
|
|
18
|
+
const token = useSearchParams().get("token") ?? "";
|
|
19
|
+
|
|
20
|
+
const {
|
|
21
|
+
register,
|
|
22
|
+
handleSubmit,
|
|
23
|
+
control,
|
|
24
|
+
formState: { errors, isSubmitting },
|
|
25
|
+
} = useForm({
|
|
26
|
+
resolver: zodResolver(resetPasswordFormSchema),
|
|
27
|
+
defaultValues: { password: "", confirmPassword: "" },
|
|
28
|
+
});
|
|
29
|
+
const password = useWatch({ control, name: "password" });
|
|
30
|
+
|
|
31
|
+
const onSubmit = handleSubmit(async ({ password }) => {
|
|
32
|
+
try {
|
|
33
|
+
await apiFetch("/api/auth/reset-password", { method: "POST", json: { token, password } });
|
|
34
|
+
toast.success("Password updated. Sign in with your new password.");
|
|
35
|
+
router.replace("/login");
|
|
36
|
+
} catch (error) {
|
|
37
|
+
toast.error(getErrorMessage(error, "Could not reset your password"));
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
if (!token) {
|
|
42
|
+
return (
|
|
43
|
+
<p className="muted text-sm">
|
|
44
|
+
This link is missing its token.{" "}
|
|
45
|
+
<Link href="/forgot-password" className="text-link">
|
|
46
|
+
Request a new one
|
|
47
|
+
</Link>
|
|
48
|
+
.
|
|
49
|
+
</p>
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return (
|
|
54
|
+
<form onSubmit={onSubmit} className="form" noValidate>
|
|
55
|
+
<div className="field-group">
|
|
56
|
+
<FormField
|
|
57
|
+
id="password"
|
|
58
|
+
label="New password"
|
|
59
|
+
autoComplete="new-password"
|
|
60
|
+
error={errors.password}
|
|
61
|
+
render={(props) => <PasswordInput {...props} />}
|
|
62
|
+
{...register("password")}
|
|
63
|
+
/>
|
|
64
|
+
<PasswordStrengthMeter password={password} />
|
|
65
|
+
</div>
|
|
66
|
+
<FormField
|
|
67
|
+
id="confirmPassword"
|
|
68
|
+
label="Confirm new password"
|
|
69
|
+
autoComplete="new-password"
|
|
70
|
+
error={errors.confirmPassword}
|
|
71
|
+
render={(props) => <PasswordInput {...props} />}
|
|
72
|
+
{...register("confirmPassword")}
|
|
73
|
+
/>
|
|
74
|
+
<Button type="submit" size="lg" disabled={isSubmitting} className="full-width">
|
|
75
|
+
{isSubmitting && <LoaderCircle className="spin" />}
|
|
76
|
+
Update password
|
|
77
|
+
</Button>
|
|
78
|
+
</form>
|
|
79
|
+
);
|
|
80
|
+
}
|
|
@@ -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="notice notice--center">
|
|
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,43 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { LoaderCircle, Mail } from "lucide-react";
|
|
4
|
+
import { useState } from "react";
|
|
5
|
+
import { toast } from "sonner";
|
|
6
|
+
|
|
7
|
+
import { useUser } from "@/components/providers/user-provider";
|
|
8
|
+
import { Button } from "@/components/ui/button";
|
|
9
|
+
import { apiFetch, getErrorMessage } from "@/lib/http";
|
|
10
|
+
|
|
11
|
+
/** Reminds unverified users to confirm their email, with a resend button. */
|
|
12
|
+
export function VerifyEmailBanner() {
|
|
13
|
+
const user = useUser();
|
|
14
|
+
const [state, setState] = useState<"idle" | "sending" | "sent">("idle");
|
|
15
|
+
|
|
16
|
+
if (user.emailVerified) return null;
|
|
17
|
+
|
|
18
|
+
async function resend() {
|
|
19
|
+
setState("sending");
|
|
20
|
+
try {
|
|
21
|
+
await apiFetch("/api/auth/resend-verification", { method: "POST" });
|
|
22
|
+
setState("sent");
|
|
23
|
+
toast.success(`Verification email sent to ${user.email}`);
|
|
24
|
+
} catch (error) {
|
|
25
|
+
setState("idle");
|
|
26
|
+
toast.error(getErrorMessage(error, "Could not send the email"));
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return (
|
|
31
|
+
<div className="banner" role="status">
|
|
32
|
+
<Mail size={16} />
|
|
33
|
+
<p className="banner-text">
|
|
34
|
+
Please verify your email address <strong>{user.email}</strong>. Check your inbox for the
|
|
35
|
+
link.
|
|
36
|
+
</p>
|
|
37
|
+
<Button size="sm" variant="outline" onClick={resend} disabled={state !== "idle"}>
|
|
38
|
+
{state === "sending" && <LoaderCircle className="spin" />}
|
|
39
|
+
{state === "sent" ? "Email sent" : "Resend email"}
|
|
40
|
+
</Button>
|
|
41
|
+
</div>
|
|
42
|
+
);
|
|
43
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { CircleCheck, CircleX } from "lucide-react";
|
|
4
|
+
import Link from "next/link";
|
|
5
|
+
import { useSearchParams } from "next/navigation";
|
|
6
|
+
|
|
7
|
+
import { Button } from "@/components/ui/button";
|
|
8
|
+
|
|
9
|
+
const MESSAGES = {
|
|
10
|
+
success: {
|
|
11
|
+
title: "Email verified",
|
|
12
|
+
text: "Thanks for confirming your email address.",
|
|
13
|
+
},
|
|
14
|
+
invalid: {
|
|
15
|
+
title: "Link invalid or expired",
|
|
16
|
+
text: "Verification links work once and expire after 24 hours. Request a new one from your dashboard.",
|
|
17
|
+
},
|
|
18
|
+
error: {
|
|
19
|
+
title: "Something went wrong",
|
|
20
|
+
text: "We couldn't verify your email right now. Please try the link again.",
|
|
21
|
+
},
|
|
22
|
+
} as const;
|
|
23
|
+
|
|
24
|
+
export function VerifyEmailStatus() {
|
|
25
|
+
const raw = useSearchParams().get("status");
|
|
26
|
+
const status = raw === "success" || raw === "error" ? raw : "invalid";
|
|
27
|
+
const { title, text } = MESSAGES[status];
|
|
28
|
+
const Icon = status === "success" ? CircleCheck : CircleX;
|
|
29
|
+
|
|
30
|
+
return (
|
|
31
|
+
<div className="status-page-body">
|
|
32
|
+
<div className={`status-icon status-icon--${status === "success" ? "success" : "danger"}`}>
|
|
33
|
+
<Icon size={28} />
|
|
34
|
+
</div>
|
|
35
|
+
<div className="stack-sm">
|
|
36
|
+
<h1 className="status-title">{title}</h1>
|
|
37
|
+
<p className="muted text-sm">{text}</p>
|
|
38
|
+
</div>
|
|
39
|
+
<Button asChild>
|
|
40
|
+
<Link href="/dashboard">Go to dashboard</Link>
|
|
41
|
+
</Button>
|
|
42
|
+
</div>
|
|
43
|
+
);
|
|
44
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
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 { useUser } from "@/components/providers/user-provider";
|
|
8
|
+
import { getNavItems } from "@/config/rbac";
|
|
9
|
+
|
|
10
|
+
const ICONS: Record<string, LucideIcon> = {
|
|
11
|
+
"/dashboard": LayoutDashboard,
|
|
12
|
+
"/reports": ChartColumn,
|
|
13
|
+
"/admin": Users,
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/** Links the current role may open (from NAV_ITEMS in src/config/rbac.ts). */
|
|
17
|
+
export function NavLinks({ onNavigate }: { onNavigate?: () => void }) {
|
|
18
|
+
const user = useUser();
|
|
19
|
+
const pathname = usePathname();
|
|
20
|
+
|
|
21
|
+
return getNavItems(user.role).map((item) => {
|
|
22
|
+
const Icon = ICONS[item.href];
|
|
23
|
+
const active = pathname === item.href || pathname.startsWith(`${item.href}/`);
|
|
24
|
+
return (
|
|
25
|
+
<Link
|
|
26
|
+
key={item.href}
|
|
27
|
+
href={item.href}
|
|
28
|
+
onClick={onNavigate}
|
|
29
|
+
aria-current={active ? "page" : undefined}
|
|
30
|
+
className="nav-link"
|
|
31
|
+
>
|
|
32
|
+
{Icon && <Icon size={16} />}
|
|
33
|
+
{item.label}
|
|
34
|
+
</Link>
|
|
35
|
+
);
|
|
36
|
+
});
|
|
37
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { Logo } from "@/components/logo";
|
|
4
|
+
import { NavLinks } from "@/components/layout/nav-links";
|
|
5
|
+
import { UserMenu } from "@/components/layout/user-menu";
|
|
6
|
+
import { ThemeToggle } from "@/components/theme-toggle";
|
|
7
|
+
|
|
8
|
+
/** Top navigation bar: only shows links the current role is permitted to open. */
|
|
9
|
+
export function Navbar() {
|
|
10
|
+
return (
|
|
11
|
+
<header className="app-header">
|
|
12
|
+
<div className="app-header-inner">
|
|
13
|
+
<Logo href="/dashboard" />
|
|
14
|
+
<nav className="app-nav app-nav--desktop" aria-label="Main">
|
|
15
|
+
<NavLinks />
|
|
16
|
+
</nav>
|
|
17
|
+
<div className="app-header-actions">
|
|
18
|
+
<ThemeToggle />
|
|
19
|
+
<UserMenu />
|
|
20
|
+
</div>
|
|
21
|
+
</div>
|
|
22
|
+
<nav className="app-nav app-nav--mobile" aria-label="Main">
|
|
23
|
+
<NavLinks />
|
|
24
|
+
</nav>
|
|
25
|
+
</header>
|
|
26
|
+
);
|
|
27
|
+
}
|
|
@@ -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="page-header">
|
|
14
|
+
<div className="page-header-text">
|
|
15
|
+
<h1 className="page-title">{title}</h1>
|
|
16
|
+
{description && <p className="muted text-sm">{description}</p>}
|
|
17
|
+
</div>
|
|
18
|
+
{actions && <div className="page-header-actions">{actions}</div>}
|
|
19
|
+
</div>
|
|
20
|
+
);
|
|
21
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { Menu, X } from "lucide-react";
|
|
4
|
+
import { usePathname } from "next/navigation";
|
|
5
|
+
import { useEffect, useState, type ReactNode } from "react";
|
|
6
|
+
|
|
7
|
+
import { Logo } from "@/components/logo";
|
|
8
|
+
import { NavLinks } from "@/components/layout/nav-links";
|
|
9
|
+
import { UserMenu } from "@/components/layout/user-menu";
|
|
10
|
+
import { ThemeToggle } from "@/components/theme-toggle";
|
|
11
|
+
import { Button } from "@/components/ui/button";
|
|
12
|
+
import { cn } from "@/lib/utils";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Dashboard shell with a sidebar. On small screens the sidebar becomes a
|
|
16
|
+
* drawer opened from the top bar.
|
|
17
|
+
*/
|
|
18
|
+
export function SidebarLayout({ children }: { children: ReactNode }) {
|
|
19
|
+
const [open, setOpen] = useState(false);
|
|
20
|
+
const pathname = usePathname();
|
|
21
|
+
const [lastPath, setLastPath] = useState(pathname);
|
|
22
|
+
|
|
23
|
+
// Close the drawer after navigating (adjusting state during render, not in an effect).
|
|
24
|
+
if (pathname !== lastPath) {
|
|
25
|
+
setLastPath(pathname);
|
|
26
|
+
setOpen(false);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
useEffect(() => {
|
|
30
|
+
if (!open) return;
|
|
31
|
+
const onKey = (e: KeyboardEvent) => e.key === "Escape" && setOpen(false);
|
|
32
|
+
document.addEventListener("keydown", onKey);
|
|
33
|
+
return () => document.removeEventListener("keydown", onKey);
|
|
34
|
+
}, [open]);
|
|
35
|
+
|
|
36
|
+
return (
|
|
37
|
+
<div className="sidebar-layout">
|
|
38
|
+
<aside className={cn("sidebar", open && "sidebar--open")} aria-label="Sidebar">
|
|
39
|
+
<div className="sidebar-header">
|
|
40
|
+
<Logo href="/dashboard" />
|
|
41
|
+
<Button
|
|
42
|
+
variant="ghost"
|
|
43
|
+
size="icon"
|
|
44
|
+
className="sidebar-close"
|
|
45
|
+
onClick={() => setOpen(false)}
|
|
46
|
+
aria-label="Close menu"
|
|
47
|
+
>
|
|
48
|
+
<X />
|
|
49
|
+
</Button>
|
|
50
|
+
</div>
|
|
51
|
+
<nav className="sidebar-nav" aria-label="Main">
|
|
52
|
+
<span className="sidebar-section">Navigation</span>
|
|
53
|
+
<NavLinks onNavigate={() => setOpen(false)} />
|
|
54
|
+
</nav>
|
|
55
|
+
<div className="sidebar-footer">
|
|
56
|
+
<UserMenu side="top" showName />
|
|
57
|
+
</div>
|
|
58
|
+
</aside>
|
|
59
|
+
{open && <div className="sidebar-backdrop" onClick={() => setOpen(false)} aria-hidden />}
|
|
60
|
+
|
|
61
|
+
<div className="sidebar-main">
|
|
62
|
+
<header className="sidebar-topbar">
|
|
63
|
+
<Button
|
|
64
|
+
variant="ghost"
|
|
65
|
+
size="icon"
|
|
66
|
+
className="sidebar-open"
|
|
67
|
+
onClick={() => setOpen(true)}
|
|
68
|
+
aria-label="Open menu"
|
|
69
|
+
aria-expanded={open}
|
|
70
|
+
>
|
|
71
|
+
<Menu />
|
|
72
|
+
</Button>
|
|
73
|
+
<div className="sidebar-topbar-actions">
|
|
74
|
+
<ThemeToggle />
|
|
75
|
+
</div>
|
|
76
|
+
</header>
|
|
77
|
+
<main className="sidebar-content">
|
|
78
|
+
<div className="page-container">{children}</div>
|
|
79
|
+
</main>
|
|
80
|
+
</div>
|
|
81
|
+
</div>
|
|
82
|
+
);
|
|
83
|
+
}
|