create-mohit-next 1.0.0 → 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 +96 -65
- package/index.js +195 -195
- package/lib/features.js +358 -0
- package/lib/scaffold.js +471 -0
- package/package.json +10 -6
- 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/{template → templates/common}/_prettierignore +4 -0
- package/templates/common/commitlint.config.mjs +10 -0
- package/templates/next/README.md +328 -0
- package/{template → templates/next}/_env.example +10 -1
- package/{template → templates/next}/_gitignore +8 -0
- package/templates/next/e2e/rbac.spec.ts +69 -0
- package/templates/next/e2e/server.mjs +35 -0
- package/{template → templates/next}/eslint.config.mjs +12 -0
- package/{template → templates/next}/next.config.ts +2 -0
- package/{template → templates/next}/package.json +18 -3
- package/templates/next/playwright.config.ts +30 -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/{template → templates/next}/src/app/(app)/dashboard/page.tsx +22 -29
- package/{template → templates/next}/src/app/(app)/error.tsx +7 -7
- package/{template → templates/next}/src/app/(app)/layout.tsx +25 -2
- 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/{template → templates/next}/src/app/(auth)/register/page.tsx +2 -4
- 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/{template → templates/next}/src/app/api/admin/users/[id]/route.ts +25 -4
- package/templates/next/src/app/api/auth/forgot-password/route.ts +47 -0
- package/{template → templates/next}/src/app/api/auth/login/route.ts +19 -2
- package/{template → templates/next}/src/app/api/auth/me/route.ts +4 -2
- package/{template → templates/next}/src/app/api/auth/register/route.ts +38 -4
- 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/{template → templates/next}/src/app/forbidden/page.tsx +11 -13
- package/{template → templates/next}/src/app/layout.tsx +9 -6
- 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/{template → templates/next}/src/components/auth/auth-card.tsx +8 -8
- package/templates/next/src/components/auth/forgot-password-form.tsx +76 -0
- package/templates/next/src/components/auth/form-field.tsx +40 -0
- package/{template → templates/next}/src/components/auth/login-form.tsx +14 -9
- package/templates/next/src/components/auth/password-input.tsx +43 -0
- package/{template → templates/next}/src/components/auth/register-form.tsx +22 -19
- package/templates/next/src/components/auth/reset-password-form.tsx +80 -0
- package/{template → templates/next}/src/components/auth/session-refresher.tsx +1 -1
- 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/{template → templates/next}/src/components/logo.tsx +3 -6
- package/{template → templates/next}/src/components/reports/animated-bar.tsx +1 -5
- 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/{template → templates/next}/src/config/rbac.ts +35 -5
- package/{template → templates/next}/src/lib/api.ts +40 -4
- package/templates/next/src/lib/audit.ts +66 -0
- package/{template → templates/next}/src/lib/auth.ts +10 -2
- package/{template → templates/next}/src/lib/dal.ts +8 -5
- 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/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/{template → templates/next}/src/lib/session.ts +5 -1
- package/templates/next/src/lib/theme.ts +32 -0
- package/templates/next/src/lib/tokens.ts +63 -0
- package/{template → templates/next}/src/lib/users.ts +49 -12
- package/templates/next/src/lib/validations/account.ts +20 -0
- package/templates/next/src/lib/validations/auth.test.ts +39 -0
- package/{template → templates/next}/src/lib/validations/auth.ts +24 -2
- package/templates/next/src/models/audit-log.ts +33 -0
- package/templates/next/src/models/auth-token.ts +26 -0
- package/{template → templates/next}/src/models/user.ts +5 -0
- package/{template → templates/next}/src/proxy.ts +1 -1
- package/templates/next/src/styles/app.css +1092 -0
- package/{template → templates/next}/src/types/user.ts +3 -0
- package/templates/next/vitest.config.mts +13 -0
- package/templates/styles/css.css +580 -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/dropdown-menu.tsx +268 -0
- package/templates/ui/shadcn/components/table.tsx +115 -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
- package/template/README.md +0 -262
- package/template/_prettierrc +0 -9
- package/template/components.json +0 -25
- package/template/src/app/(app)/admin/page.tsx +0 -53
- package/template/src/app/(app)/reports/page.tsx +0 -109
- package/template/src/app/(auth)/layout.tsx +0 -22
- package/template/src/app/not-found.tsx +0 -18
- package/template/src/app/page.tsx +0 -187
- package/template/src/components/admin/users-table.tsx +0 -162
- package/template/src/components/auth/form-field.tsx +0 -32
- package/template/src/components/layout/logout-button.tsx +0 -34
- package/template/src/components/layout/navbar.tsx +0 -67
- package/template/src/components/layout/page-header.tsx +0 -21
- package/template/src/components/layout/skeletons.tsx +0 -35
- package/template/src/components/role-badge.tsx +0 -17
- package/template/src/components/theme-toggle.tsx +0 -25
- package/template/src/lib/env.ts +0 -36
- package/template/src/lib/theme.ts +0 -8
- /package/{template → templates/next}/AGENTS.md +0 -0
- /package/{template → templates/next}/Dockerfile +0 -0
- /package/{template → templates/next}/_dockerignore +0 -0
- /package/{template → templates/next}/docker-compose.yml +0 -0
- /package/{template → templates/next}/public/robots.txt +0 -0
- /package/{template → templates/next}/src/app/(app)/loading.tsx +0 -0
- /package/{template → templates/next}/src/app/(auth)/login/page.tsx +0 -0
- /package/{template → templates/next}/src/app/api/admin/users/route.ts +0 -0
- /package/{template → templates/next}/src/app/api/auth/logout/route.ts +0 -0
- /package/{template → templates/next}/src/app/api/health/route.ts +0 -0
- /package/{template → templates/next}/src/app/favicon.ico +0 -0
- /package/{template → templates/next}/src/components/auth/can.tsx +0 -0
- /package/{template → templates/next}/src/components/motion/fade-in.tsx +0 -0
- /package/{template → templates/next}/src/components/providers/user-provider.tsx +0 -0
- /package/{template → templates/next}/src/config/site.ts +0 -0
- /package/{template → templates/next}/src/lib/db.ts +0 -0
- /package/{template → templates/next}/src/lib/http.ts +0 -0
- /package/{template → templates/next}/src/lib/redirect.ts +0 -0
- /package/{template → templates/next}/src/lib/validations/user.ts +0 -0
- /package/{template → templates/next}/tsconfig.json +0 -0
- /package/{template/src/app/globals.css → templates/styles/shadcn.css} +0 -0
- /package/{template/src/components/ui → templates/ui/shadcn/components}/badge.tsx +0 -0
- /package/{template/src/components/ui → templates/ui/shadcn/components}/button.tsx +0 -0
- /package/{template/src/components/ui → templates/ui/shadcn/components}/card.tsx +0 -0
- /package/{template/src/components/ui → templates/ui/shadcn/components}/input.tsx +0 -0
- /package/{template/src/components/ui → templates/ui/shadcn/components}/label.tsx +0 -0
- /package/{template/src/components/ui → templates/ui/shadcn/components}/native-select.tsx +0 -0
- /package/{template/src/components/ui → templates/ui/shadcn/components}/skeleton.tsx +0 -0
- /package/{template/src/lib → templates/ui/shadcn}/utils.ts +0 -0
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { zodResolver } from "@hookform/resolvers/zod";
|
|
4
|
+
import { LoaderCircle, MailCheck } from "lucide-react";
|
|
5
|
+
import Link from "next/link";
|
|
6
|
+
import { useState } from "react";
|
|
7
|
+
import { useForm } from "react-hook-form";
|
|
8
|
+
import { toast } from "sonner";
|
|
9
|
+
|
|
10
|
+
import { FormField } from "@/components/auth/form-field";
|
|
11
|
+
import { Button } from "@/components/ui/button";
|
|
12
|
+
import { apiFetch, getErrorMessage } from "@/lib/http";
|
|
13
|
+
import { forgotPasswordSchema } from "@/lib/validations/auth";
|
|
14
|
+
|
|
15
|
+
export function ForgotPasswordForm() {
|
|
16
|
+
const [sentTo, setSentTo] = useState<string | null>(null);
|
|
17
|
+
const {
|
|
18
|
+
register,
|
|
19
|
+
handleSubmit,
|
|
20
|
+
formState: { errors, isSubmitting },
|
|
21
|
+
} = useForm({
|
|
22
|
+
resolver: zodResolver(forgotPasswordSchema),
|
|
23
|
+
defaultValues: { email: "" },
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
const onSubmit = handleSubmit(async (values) => {
|
|
27
|
+
try {
|
|
28
|
+
await apiFetch("/api/auth/forgot-password", { method: "POST", json: values });
|
|
29
|
+
setSentTo(values.email);
|
|
30
|
+
} catch (error) {
|
|
31
|
+
toast.error(getErrorMessage(error, "Could not send the reset link"));
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
if (sentTo) {
|
|
36
|
+
return (
|
|
37
|
+
<div className="notice">
|
|
38
|
+
<MailCheck size={20} />
|
|
39
|
+
<div className="stack-sm">
|
|
40
|
+
<p className="notice-title">Check your inbox</p>
|
|
41
|
+
<p className="muted text-sm">
|
|
42
|
+
If an account exists for <strong>{sentTo}</strong>, we've sent a link to reset the
|
|
43
|
+
password. It expires in 1 hour.
|
|
44
|
+
</p>
|
|
45
|
+
<Link href="/login" className="text-link text-sm">
|
|
46
|
+
Back to sign in
|
|
47
|
+
</Link>
|
|
48
|
+
</div>
|
|
49
|
+
</div>
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return (
|
|
54
|
+
<form onSubmit={onSubmit} className="form" noValidate>
|
|
55
|
+
<FormField
|
|
56
|
+
id="email"
|
|
57
|
+
label="Email"
|
|
58
|
+
type="email"
|
|
59
|
+
autoComplete="email"
|
|
60
|
+
placeholder="you@example.com"
|
|
61
|
+
error={errors.email}
|
|
62
|
+
{...register("email")}
|
|
63
|
+
/>
|
|
64
|
+
<Button type="submit" size="lg" disabled={isSubmitting} className="full-width">
|
|
65
|
+
{isSubmitting && <LoaderCircle className="spin" />}
|
|
66
|
+
Send reset link
|
|
67
|
+
</Button>
|
|
68
|
+
<p className="form-footer">
|
|
69
|
+
Remembered it?{" "}
|
|
70
|
+
<Link href="/login" className="text-link">
|
|
71
|
+
Sign in
|
|
72
|
+
</Link>
|
|
73
|
+
</p>
|
|
74
|
+
</form>
|
|
75
|
+
);
|
|
76
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { ComponentProps, ReactNode } from "react";
|
|
2
|
+
import type { FieldError } from "react-hook-form";
|
|
3
|
+
|
|
4
|
+
import { Input } from "@/components/ui/input";
|
|
5
|
+
import { Label } from "@/components/ui/label";
|
|
6
|
+
|
|
7
|
+
type FormFieldProps = ComponentProps<typeof Input> & {
|
|
8
|
+
id: string;
|
|
9
|
+
label: string;
|
|
10
|
+
error?: FieldError;
|
|
11
|
+
/** Rendered on the right of the label (e.g. a "Forgot password?" link). */
|
|
12
|
+
labelAside?: ReactNode;
|
|
13
|
+
/** Replaces the default <Input> (e.g. a PasswordInput). */
|
|
14
|
+
render?: (props: ComponentProps<typeof Input>) => ReactNode;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/** Label + input + error message, wired up for accessibility. */
|
|
18
|
+
export function FormField({ id, label, error, labelAside, render, ...inputProps }: FormFieldProps) {
|
|
19
|
+
const errorId = `${id}-error`;
|
|
20
|
+
const props = {
|
|
21
|
+
id,
|
|
22
|
+
"aria-invalid": error ? true : undefined,
|
|
23
|
+
"aria-describedby": error ? errorId : undefined,
|
|
24
|
+
...inputProps,
|
|
25
|
+
};
|
|
26
|
+
return (
|
|
27
|
+
<div className="field">
|
|
28
|
+
<div className="field-label-row">
|
|
29
|
+
<Label htmlFor={id}>{label}</Label>
|
|
30
|
+
{labelAside}
|
|
31
|
+
</div>
|
|
32
|
+
{render ? render(props) : <Input {...props} />}
|
|
33
|
+
{error?.message && (
|
|
34
|
+
<p id={errorId} className="field-error" role="alert">
|
|
35
|
+
{error.message}
|
|
36
|
+
</p>
|
|
37
|
+
)}
|
|
38
|
+
</div>
|
|
39
|
+
);
|
|
40
|
+
}
|
|
@@ -8,6 +8,7 @@ import { useForm } from "react-hook-form";
|
|
|
8
8
|
import { toast } from "sonner";
|
|
9
9
|
|
|
10
10
|
import { FormField } from "@/components/auth/form-field";
|
|
11
|
+
import { PasswordInput } from "@/components/auth/password-input";
|
|
11
12
|
import { Button } from "@/components/ui/button";
|
|
12
13
|
import { apiFetch, getErrorMessage } from "@/lib/http";
|
|
13
14
|
import { safeRedirectPath } from "@/lib/redirect";
|
|
@@ -46,7 +47,7 @@ export function LoginForm() {
|
|
|
46
47
|
next === "/dashboard" ? "/register" : `/register?next=${encodeURIComponent(next)}`;
|
|
47
48
|
|
|
48
49
|
return (
|
|
49
|
-
<form onSubmit={onSubmit} className="
|
|
50
|
+
<form onSubmit={onSubmit} className="form" noValidate>
|
|
50
51
|
<FormField
|
|
51
52
|
id="email"
|
|
52
53
|
label="Email"
|
|
@@ -59,21 +60,25 @@ export function LoginForm() {
|
|
|
59
60
|
<FormField
|
|
60
61
|
id="password"
|
|
61
62
|
label="Password"
|
|
62
|
-
type="password"
|
|
63
63
|
autoComplete="current-password"
|
|
64
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} />}
|
|
65
73
|
{...register("password")}
|
|
66
74
|
/>
|
|
67
|
-
<Button type="submit" size="lg" disabled={isSubmitting} className="
|
|
68
|
-
{isSubmitting && <LoaderCircle className="
|
|
75
|
+
<Button type="submit" size="lg" disabled={isSubmitting} className="full-width">
|
|
76
|
+
{isSubmitting && <LoaderCircle className="spin" />}
|
|
69
77
|
Sign in
|
|
70
78
|
</Button>
|
|
71
|
-
<p className="
|
|
79
|
+
<p className="form-footer">
|
|
72
80
|
Don't have an account?{" "}
|
|
73
|
-
<Link
|
|
74
|
-
href={registerHref}
|
|
75
|
-
className="font-medium text-foreground underline-offset-4 hover:underline"
|
|
76
|
-
>
|
|
81
|
+
<Link href={registerHref} className="text-link">
|
|
77
82
|
Create one
|
|
78
83
|
</Link>
|
|
79
84
|
</p>
|
|
@@ -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
|
+
}
|
|
@@ -4,10 +4,11 @@ import { zodResolver } from "@hookform/resolvers/zod";
|
|
|
4
4
|
import { LoaderCircle } from "lucide-react";
|
|
5
5
|
import Link from "next/link";
|
|
6
6
|
import { useRouter, useSearchParams } from "next/navigation";
|
|
7
|
-
import { useForm } from "react-hook-form";
|
|
7
|
+
import { useForm, useWatch } from "react-hook-form";
|
|
8
8
|
import { toast } from "sonner";
|
|
9
9
|
|
|
10
10
|
import { FormField } from "@/components/auth/form-field";
|
|
11
|
+
import { PasswordInput, PasswordStrengthMeter } from "@/components/auth/password-input";
|
|
11
12
|
import { Button } from "@/components/ui/button";
|
|
12
13
|
import { ROLE_LABELS } from "@/config/rbac";
|
|
13
14
|
import { apiFetch, getErrorMessage, HttpError } from "@/lib/http";
|
|
@@ -26,11 +27,13 @@ export function RegisterForm() {
|
|
|
26
27
|
register,
|
|
27
28
|
handleSubmit,
|
|
28
29
|
setError,
|
|
30
|
+
control,
|
|
29
31
|
formState: { errors, isSubmitting },
|
|
30
32
|
} = useForm({
|
|
31
33
|
resolver: zodResolver(registerFormSchema),
|
|
32
34
|
defaultValues: { name: "", email: "", password: "", confirmPassword: "" },
|
|
33
35
|
});
|
|
36
|
+
const password = useWatch({ control, name: "password" });
|
|
34
37
|
|
|
35
38
|
const onSubmit = handleSubmit(async ({ name, email, password }) => {
|
|
36
39
|
try {
|
|
@@ -61,7 +64,7 @@ export function RegisterForm() {
|
|
|
61
64
|
});
|
|
62
65
|
|
|
63
66
|
return (
|
|
64
|
-
<form onSubmit={onSubmit} className="
|
|
67
|
+
<form onSubmit={onSubmit} className="form" noValidate>
|
|
65
68
|
<FormField
|
|
66
69
|
id="name"
|
|
67
70
|
label="Name"
|
|
@@ -79,33 +82,33 @@ export function RegisterForm() {
|
|
|
79
82
|
error={errors.email}
|
|
80
83
|
{...register("email")}
|
|
81
84
|
/>
|
|
82
|
-
<
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
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>
|
|
91
97
|
<FormField
|
|
92
98
|
id="confirmPassword"
|
|
93
99
|
label="Confirm password"
|
|
94
|
-
type="password"
|
|
95
100
|
autoComplete="new-password"
|
|
96
101
|
error={errors.confirmPassword}
|
|
102
|
+
render={(props) => <PasswordInput {...props} />}
|
|
97
103
|
{...register("confirmPassword")}
|
|
98
104
|
/>
|
|
99
|
-
<Button type="submit" size="lg" disabled={isSubmitting} className="
|
|
100
|
-
{isSubmitting && <LoaderCircle className="
|
|
105
|
+
<Button type="submit" size="lg" disabled={isSubmitting} className="full-width">
|
|
106
|
+
{isSubmitting && <LoaderCircle className="spin" />}
|
|
101
107
|
Create account
|
|
102
108
|
</Button>
|
|
103
|
-
<p className="
|
|
109
|
+
<p className="form-footer">
|
|
104
110
|
Already have an account?{" "}
|
|
105
|
-
<Link
|
|
106
|
-
href="/login"
|
|
107
|
-
className="font-medium text-foreground underline-offset-4 hover:underline"
|
|
108
|
-
>
|
|
111
|
+
<Link href="/login" className="text-link">
|
|
109
112
|
Sign in
|
|
110
113
|
</Link>
|
|
111
114
|
</p>
|
|
@@ -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
|
+
}
|
|
@@ -34,7 +34,7 @@ export function SessionRefresher() {
|
|
|
34
34
|
if (!refreshed) return null;
|
|
35
35
|
|
|
36
36
|
return (
|
|
37
|
-
<div className="
|
|
37
|
+
<div className="notice notice--center">
|
|
38
38
|
<p>Your role was just updated. Try the page again.</p>
|
|
39
39
|
<Button size="sm" onClick={() => router.push(target)}>
|
|
40
40
|
<RefreshCw /> Try again
|
|
@@ -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
|
+
}
|