hazo_auth 0.1.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.
Files changed (162) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +48 -0
  3. package/components.json +22 -0
  4. package/hazo_auth_config.example.ini +414 -0
  5. package/hazo_notify_config.example.ini +159 -0
  6. package/instrumentation.ts +32 -0
  7. package/migrations/001_add_token_type_to_refresh_tokens.sql +14 -0
  8. package/migrations/002_add_name_to_hazo_users.sql +7 -0
  9. package/next.config.mjs +55 -0
  10. package/package.json +114 -0
  11. package/postcss.config.mjs +8 -0
  12. package/public/file.svg +1 -0
  13. package/public/globe.svg +1 -0
  14. package/public/next.svg +1 -0
  15. package/public/vercel.svg +1 -0
  16. package/public/window.svg +1 -0
  17. package/scripts/apply_migration.ts +118 -0
  18. package/src/app/api/auth/change_password/route.ts +109 -0
  19. package/src/app/api/auth/forgot_password/route.ts +107 -0
  20. package/src/app/api/auth/library_photos/route.ts +70 -0
  21. package/src/app/api/auth/login/route.ts +155 -0
  22. package/src/app/api/auth/logout/route.ts +62 -0
  23. package/src/app/api/auth/me/route.ts +47 -0
  24. package/src/app/api/auth/profile_picture/[filename]/route.ts +67 -0
  25. package/src/app/api/auth/register/route.ts +106 -0
  26. package/src/app/api/auth/remove_profile_picture/route.ts +86 -0
  27. package/src/app/api/auth/resend_verification/route.ts +107 -0
  28. package/src/app/api/auth/reset_password/route.ts +107 -0
  29. package/src/app/api/auth/update_user/route.ts +126 -0
  30. package/src/app/api/auth/upload_profile_picture/route.ts +268 -0
  31. package/src/app/api/auth/validate_reset_token/route.ts +80 -0
  32. package/src/app/api/auth/verify_email/route.ts +85 -0
  33. package/src/app/api/migrations/apply/route.ts +91 -0
  34. package/src/app/favicon.ico +0 -0
  35. package/src/app/fonts/GeistMonoVF.woff +0 -0
  36. package/src/app/fonts/GeistVF.woff +0 -0
  37. package/src/app/forgot_password/forgot_password_page_client.tsx +60 -0
  38. package/src/app/forgot_password/page.tsx +24 -0
  39. package/src/app/globals.css +89 -0
  40. package/src/app/hazo_connect/api/sqlite/data/route.ts +197 -0
  41. package/src/app/hazo_connect/api/sqlite/schema/route.ts +35 -0
  42. package/src/app/hazo_connect/api/sqlite/tables/route.ts +26 -0
  43. package/src/app/hazo_connect/sqlite_admin/page.tsx +51 -0
  44. package/src/app/hazo_connect/sqlite_admin/sqlite-admin-client.tsx +947 -0
  45. package/src/app/layout.tsx +43 -0
  46. package/src/app/login/login_page_client.tsx +71 -0
  47. package/src/app/login/page.tsx +26 -0
  48. package/src/app/my_settings/my_settings_page_client.tsx +120 -0
  49. package/src/app/my_settings/page.tsx +40 -0
  50. package/src/app/page.tsx +170 -0
  51. package/src/app/register/page.tsx +26 -0
  52. package/src/app/register/register_page_client.tsx +72 -0
  53. package/src/app/reset_password/page.tsx +29 -0
  54. package/src/app/reset_password/reset_password_page_client.tsx +81 -0
  55. package/src/app/verify_email/page.tsx +24 -0
  56. package/src/app/verify_email/verify_email_page_client.tsx +60 -0
  57. package/src/components/layouts/email_verification/config/email_verification_field_config.ts +86 -0
  58. package/src/components/layouts/email_verification/hooks/use_email_verification.ts +291 -0
  59. package/src/components/layouts/email_verification/index.tsx +297 -0
  60. package/src/components/layouts/forgot_password/config/forgot_password_field_config.ts +58 -0
  61. package/src/components/layouts/forgot_password/hooks/use_forgot_password_form.ts +179 -0
  62. package/src/components/layouts/forgot_password/index.tsx +168 -0
  63. package/src/components/layouts/login/config/login_field_config.ts +67 -0
  64. package/src/components/layouts/login/hooks/use_login_form.ts +281 -0
  65. package/src/components/layouts/login/index.tsx +224 -0
  66. package/src/components/layouts/my_settings/components/editable_field.tsx +177 -0
  67. package/src/components/layouts/my_settings/components/password_change_dialog.tsx +301 -0
  68. package/src/components/layouts/my_settings/components/profile_picture_dialog.tsx +385 -0
  69. package/src/components/layouts/my_settings/components/profile_picture_display.tsx +66 -0
  70. package/src/components/layouts/my_settings/components/profile_picture_gravatar_tab.tsx +143 -0
  71. package/src/components/layouts/my_settings/components/profile_picture_library_tab.tsx +282 -0
  72. package/src/components/layouts/my_settings/components/profile_picture_upload_tab.tsx +341 -0
  73. package/src/components/layouts/my_settings/config/my_settings_field_config.ts +61 -0
  74. package/src/components/layouts/my_settings/hooks/use_my_settings.ts +458 -0
  75. package/src/components/layouts/my_settings/index.tsx +351 -0
  76. package/src/components/layouts/register/config/register_field_config.ts +101 -0
  77. package/src/components/layouts/register/hooks/use_register_form.ts +272 -0
  78. package/src/components/layouts/register/index.tsx +208 -0
  79. package/src/components/layouts/reset_password/config/reset_password_field_config.ts +86 -0
  80. package/src/components/layouts/reset_password/hooks/use_reset_password_form.ts +276 -0
  81. package/src/components/layouts/reset_password/index.tsx +294 -0
  82. package/src/components/layouts/shared/components/already_logged_in_guard.tsx +95 -0
  83. package/src/components/layouts/shared/components/field_error_message.tsx +29 -0
  84. package/src/components/layouts/shared/components/form_action_buttons.tsx +64 -0
  85. package/src/components/layouts/shared/components/form_field_wrapper.tsx +44 -0
  86. package/src/components/layouts/shared/components/form_header.tsx +36 -0
  87. package/src/components/layouts/shared/components/logout_button.tsx +76 -0
  88. package/src/components/layouts/shared/components/password_field.tsx +72 -0
  89. package/src/components/layouts/shared/components/sidebar_layout_wrapper.tsx +264 -0
  90. package/src/components/layouts/shared/components/two_column_auth_layout.tsx +44 -0
  91. package/src/components/layouts/shared/components/unauthorized_guard.tsx +78 -0
  92. package/src/components/layouts/shared/components/visual_panel.tsx +41 -0
  93. package/src/components/layouts/shared/config/layout_customization.ts +95 -0
  94. package/src/components/layouts/shared/data/layout_data_client.ts +19 -0
  95. package/src/components/layouts/shared/hooks/use_auth_status.ts +103 -0
  96. package/src/components/layouts/shared/utils/ip_address.ts +37 -0
  97. package/src/components/layouts/shared/utils/validation.ts +66 -0
  98. package/src/components/ui/avatar.tsx +50 -0
  99. package/src/components/ui/button.tsx +57 -0
  100. package/src/components/ui/dialog.tsx +122 -0
  101. package/src/components/ui/hazo_ui_tooltip.tsx +67 -0
  102. package/src/components/ui/input.tsx +22 -0
  103. package/src/components/ui/label.tsx +26 -0
  104. package/src/components/ui/separator.tsx +31 -0
  105. package/src/components/ui/sheet.tsx +139 -0
  106. package/src/components/ui/sidebar.tsx +773 -0
  107. package/src/components/ui/skeleton.tsx +15 -0
  108. package/src/components/ui/sonner.tsx +31 -0
  109. package/src/components/ui/switch.tsx +29 -0
  110. package/src/components/ui/tabs.tsx +55 -0
  111. package/src/components/ui/tooltip.tsx +32 -0
  112. package/src/components/ui/vertical-tabs.tsx +59 -0
  113. package/src/hooks/use-mobile.tsx +19 -0
  114. package/src/lib/already_logged_in_config.server.ts +46 -0
  115. package/src/lib/app_logger.ts +24 -0
  116. package/src/lib/auth/auth_utils.server.ts +196 -0
  117. package/src/lib/auth/server_auth.ts +88 -0
  118. package/src/lib/config/config_loader.server.ts +149 -0
  119. package/src/lib/email_verification_config.server.ts +32 -0
  120. package/src/lib/file_types_config.server.ts +25 -0
  121. package/src/lib/forgot_password_config.server.ts +32 -0
  122. package/src/lib/hazo_connect_instance.server.ts +77 -0
  123. package/src/lib/hazo_connect_setup.server.ts +181 -0
  124. package/src/lib/hazo_connect_setup.ts +54 -0
  125. package/src/lib/login_config.server.ts +46 -0
  126. package/src/lib/messages_config.server.ts +45 -0
  127. package/src/lib/migrations/apply_migration.ts +105 -0
  128. package/src/lib/my_settings_config.server.ts +135 -0
  129. package/src/lib/password_requirements_config.server.ts +39 -0
  130. package/src/lib/profile_picture_config.server.ts +56 -0
  131. package/src/lib/register_config.server.ts +57 -0
  132. package/src/lib/reset_password_config.server.ts +75 -0
  133. package/src/lib/services/email_service.ts +581 -0
  134. package/src/lib/services/email_verification_service.ts +264 -0
  135. package/src/lib/services/login_service.ts +118 -0
  136. package/src/lib/services/password_change_service.ts +154 -0
  137. package/src/lib/services/password_reset_service.ts +405 -0
  138. package/src/lib/services/profile_picture_remove_service.ts +120 -0
  139. package/src/lib/services/profile_picture_service.ts +215 -0
  140. package/src/lib/services/profile_picture_source_mapper.ts +62 -0
  141. package/src/lib/services/registration_service.ts +163 -0
  142. package/src/lib/services/token_service.ts +240 -0
  143. package/src/lib/services/user_update_service.ts +128 -0
  144. package/src/lib/ui_sizes_config.server.ts +37 -0
  145. package/src/lib/user_fields_config.server.ts +31 -0
  146. package/src/lib/utils/api_route_helpers.ts +60 -0
  147. package/src/lib/utils.ts +11 -0
  148. package/src/middleware.ts +91 -0
  149. package/src/server/config/config_loader.ts +496 -0
  150. package/src/server/index.ts +38 -0
  151. package/src/server/logging/logger_service.ts +56 -0
  152. package/src/server/routes/root_router.ts +16 -0
  153. package/src/server/server.ts +28 -0
  154. package/src/server/types/app_types.ts +74 -0
  155. package/src/server/types/express.d.ts +15 -0
  156. package/src/stories/email_verification_layout.stories.tsx +137 -0
  157. package/src/stories/forgot_password_layout.stories.tsx +85 -0
  158. package/src/stories/login_layout.stories.tsx +85 -0
  159. package/src/stories/project_overview.stories.tsx +33 -0
  160. package/src/stories/register_layout.stories.tsx +107 -0
  161. package/tailwind.config.ts +77 -0
  162. package/tsconfig.json +27 -0
@@ -0,0 +1,297 @@
1
+ // file_description: email verification layout component built atop shared layout utilities
2
+ // section: client_directive
3
+ "use client";
4
+
5
+ // section: imports
6
+ import { Input } from "@/components/ui/input";
7
+ import { Button } from "@/components/ui/button";
8
+ import { FormFieldWrapper } from "@/components/layouts/shared/components/form_field_wrapper";
9
+ import { FormHeader } from "@/components/layouts/shared/components/form_header";
10
+ import { FormActionButtons } from "@/components/layouts/shared/components/form_action_buttons";
11
+ import { TwoColumnAuthLayout } from "@/components/layouts/shared/components/two_column_auth_layout";
12
+ import {
13
+ type ButtonPaletteOverrides,
14
+ type LayoutFieldMapOverrides,
15
+ type LayoutLabelOverrides,
16
+ } from "@/components/layouts/shared/config/layout_customization";
17
+ import {
18
+ EMAIL_VERIFICATION_FIELD_IDS,
19
+ createEmailVerificationFieldDefinitions,
20
+ resolveEmailVerificationButtonPalette,
21
+ resolveEmailVerificationLabels,
22
+ EMAIL_VERIFICATION_SUCCESS_LABEL_DEFAULTS,
23
+ EMAIL_VERIFICATION_ERROR_LABEL_DEFAULTS,
24
+ type EmailVerificationSuccessLabels,
25
+ type EmailVerificationErrorLabels,
26
+ } from "@/components/layouts/email_verification/config/email_verification_field_config";
27
+ import {
28
+ use_email_verification,
29
+ type UseEmailVerificationResult,
30
+ } from "@/components/layouts/email_verification/hooks/use_email_verification";
31
+ import { type LayoutDataClient } from "@/components/layouts/shared/data/layout_data_client";
32
+ import { CheckCircle, XCircle, Loader2 } from "lucide-react";
33
+ import { AlreadyLoggedInGuard } from "@/components/layouts/shared/components/already_logged_in_guard";
34
+
35
+ // section: types
36
+ export type EmailVerificationLayoutProps<TClient = unknown> = {
37
+ image_src: string;
38
+ image_alt: string;
39
+ image_background_color?: string;
40
+ field_overrides?: LayoutFieldMapOverrides;
41
+ labels?: LayoutLabelOverrides;
42
+ button_colors?: ButtonPaletteOverrides;
43
+ success_labels?: Partial<EmailVerificationSuccessLabels>;
44
+ error_labels?: Partial<EmailVerificationErrorLabels>;
45
+ redirect_delay?: number;
46
+ login_path?: string;
47
+ already_logged_in_message?: string;
48
+ showLogoutButton?: boolean;
49
+ showReturnHomeButton?: boolean;
50
+ returnHomeButtonLabel?: string;
51
+ returnHomePath?: string;
52
+ data_client: LayoutDataClient<TClient>;
53
+ };
54
+
55
+ const ORDERED_FIELDS: EmailVerificationFieldId[] = [
56
+ EMAIL_VERIFICATION_FIELD_IDS.EMAIL,
57
+ ];
58
+
59
+ type EmailVerificationFieldId = (typeof EMAIL_VERIFICATION_FIELD_IDS)[keyof typeof EMAIL_VERIFICATION_FIELD_IDS];
60
+
61
+ // section: component
62
+ export default function email_verification_layout<TClient>({
63
+ image_src,
64
+ image_alt,
65
+ image_background_color = "#f1f5f9",
66
+ field_overrides,
67
+ labels,
68
+ button_colors,
69
+ success_labels,
70
+ error_labels,
71
+ redirect_delay = 5,
72
+ login_path = "/login",
73
+ data_client,
74
+ already_logged_in_message,
75
+ showLogoutButton = true,
76
+ showReturnHomeButton = false,
77
+ returnHomeButtonLabel = "Return home",
78
+ returnHomePath = "/",
79
+ }: EmailVerificationLayoutProps<TClient>) {
80
+ const fieldDefinitions = createEmailVerificationFieldDefinitions(field_overrides);
81
+ const resolvedLabels = resolveEmailVerificationLabels(labels);
82
+ const resolvedButtonPalette = resolveEmailVerificationButtonPalette(button_colors);
83
+ const resolvedSuccessLabels = {
84
+ ...EMAIL_VERIFICATION_SUCCESS_LABEL_DEFAULTS,
85
+ ...(success_labels ?? {}),
86
+ };
87
+ const resolvedErrorLabels = {
88
+ ...EMAIL_VERIFICATION_ERROR_LABEL_DEFAULTS,
89
+ ...(error_labels ?? {}),
90
+ };
91
+
92
+ const verification = use_email_verification({
93
+ dataClient: data_client,
94
+ redirectDelay: redirect_delay,
95
+ loginPath: login_path,
96
+ });
97
+
98
+ const renderFields = (formState: UseEmailVerificationResult) => {
99
+ return ORDERED_FIELDS.map((fieldId) => {
100
+ const fieldDefinition = fieldDefinitions[fieldId];
101
+ const fieldValue = formState.values[fieldId];
102
+ const fieldError = formState.errors[fieldId];
103
+
104
+ const inputElement = (
105
+ <Input
106
+ id={fieldDefinition.id}
107
+ type={fieldDefinition.type}
108
+ value={fieldValue}
109
+ onChange={(event) =>
110
+ formState.handleFieldChange(fieldId, event.target.value)
111
+ }
112
+ onBlur={
113
+ fieldId === EMAIL_VERIFICATION_FIELD_IDS.EMAIL
114
+ ? formState.handleEmailBlur
115
+ : undefined
116
+ }
117
+ autoComplete={fieldDefinition.autoComplete}
118
+ placeholder={fieldDefinition.placeholder}
119
+ aria-label={fieldDefinition.ariaLabel}
120
+ className="cls_email_verification_layout_field_input"
121
+ />
122
+ );
123
+
124
+ // Only show email error if field has been touched (blurred)
125
+ const shouldShowError =
126
+ fieldId === EMAIL_VERIFICATION_FIELD_IDS.EMAIL
127
+ ? formState.emailTouched && fieldError
128
+ ? fieldError
129
+ : undefined
130
+ : fieldError;
131
+
132
+ return (
133
+ <FormFieldWrapper
134
+ key={fieldId}
135
+ fieldId={fieldDefinition.id}
136
+ label={fieldDefinition.label}
137
+ input={inputElement}
138
+ errorMessage={shouldShowError}
139
+ />
140
+ );
141
+ });
142
+ };
143
+
144
+ // Verifying state
145
+ if (verification.isVerifying) {
146
+ return (
147
+ <AlreadyLoggedInGuard
148
+ image_src={image_src}
149
+ image_alt={image_alt}
150
+ image_background_color={image_background_color}
151
+ message={already_logged_in_message}
152
+ showLogoutButton={showLogoutButton}
153
+ showReturnHomeButton={showReturnHomeButton}
154
+ returnHomeButtonLabel={returnHomeButtonLabel}
155
+ returnHomePath={returnHomePath}
156
+ requireEmailVerified={false}
157
+ >
158
+ <TwoColumnAuthLayout
159
+ imageSrc={image_src}
160
+ imageAlt={image_alt}
161
+ imageBackgroundColor={image_background_color}
162
+ formContent={
163
+ <div className="cls_email_verification_verifying flex flex-col items-center justify-center gap-4 py-8">
164
+ <Loader2 className="h-12 w-12 animate-spin text-slate-600" aria-hidden="true" />
165
+ <div className="cls_email_verification_verifying_text text-center">
166
+ <h1 className="cls_email_verification_verifying_heading text-2xl font-semibold text-slate-900">
167
+ {resolvedLabels.heading}
168
+ </h1>
169
+ <p className="cls_email_verification_verifying_subheading mt-2 text-sm text-slate-600">
170
+ {resolvedLabels.subHeading}
171
+ </p>
172
+ </div>
173
+ </div>
174
+ }
175
+ />
176
+ </AlreadyLoggedInGuard>
177
+ );
178
+ }
179
+
180
+ // Success state
181
+ if (verification.isVerified) {
182
+ return (
183
+ <AlreadyLoggedInGuard
184
+ image_src={image_src}
185
+ image_alt={image_alt}
186
+ image_background_color={image_background_color}
187
+ message={already_logged_in_message}
188
+ showLogoutButton={showLogoutButton}
189
+ showReturnHomeButton={showReturnHomeButton}
190
+ returnHomeButtonLabel={returnHomeButtonLabel}
191
+ returnHomePath={returnHomePath}
192
+ requireEmailVerified={false}
193
+ >
194
+ <TwoColumnAuthLayout
195
+ imageSrc={image_src}
196
+ imageAlt={image_alt}
197
+ imageBackgroundColor={image_background_color}
198
+ formContent={
199
+ <div className="cls_email_verification_success flex flex-col gap-6">
200
+ <div className="cls_email_verification_success_content flex flex-col items-center gap-4 text-center">
201
+ <CheckCircle className="h-16 w-16 text-green-600" aria-hidden="true" />
202
+ <div className="cls_email_verification_success_text">
203
+ <h1 className="cls_email_verification_success_heading text-2xl font-semibold text-slate-900">
204
+ {resolvedSuccessLabels.heading}
205
+ </h1>
206
+ <p className="cls_email_verification_success_message mt-2 text-sm text-slate-600">
207
+ {resolvedSuccessLabels.message}
208
+ </p>
209
+ </div>
210
+ <div className="cls_email_verification_redirect_info mt-2 text-sm text-slate-500">
211
+ {resolvedSuccessLabels.redirectMessage} {verification.redirectCountdown} seconds...
212
+ </div>
213
+ </div>
214
+ <div className="cls_email_verification_success_actions flex justify-center">
215
+ <Button
216
+ type="button"
217
+ onClick={verification.handleGoToLogin}
218
+ className="cls_email_verification_go_to_login_button"
219
+ style={{
220
+ backgroundColor: resolvedButtonPalette.submitBackground,
221
+ color: resolvedButtonPalette.submitText,
222
+ }}
223
+ >
224
+ {resolvedSuccessLabels.goToLoginButton}
225
+ </Button>
226
+ </div>
227
+ </div>
228
+ }
229
+ />
230
+ </AlreadyLoggedInGuard>
231
+ );
232
+ }
233
+
234
+ // Error state with resend form
235
+ return (
236
+ <AlreadyLoggedInGuard
237
+ image_src={image_src}
238
+ image_alt={image_alt}
239
+ image_background_color={image_background_color}
240
+ message={already_logged_in_message}
241
+ showLogoutButton={showLogoutButton}
242
+ showReturnHomeButton={showReturnHomeButton}
243
+ returnHomeButtonLabel={returnHomeButtonLabel}
244
+ returnHomePath={returnHomePath}
245
+ requireEmailVerified={false}
246
+ >
247
+ <TwoColumnAuthLayout
248
+ imageSrc={image_src}
249
+ imageAlt={image_alt}
250
+ imageBackgroundColor={image_background_color}
251
+ formContent={
252
+ <>
253
+ <div className="cls_email_verification_error_header flex flex-col items-center gap-4 text-center">
254
+ <XCircle className="h-12 w-12 text-red-600" aria-hidden="true" />
255
+ <div className="cls_email_verification_error_text">
256
+ <h1 className="cls_email_verification_error_heading text-2xl font-semibold text-slate-900">
257
+ {resolvedErrorLabels.heading}
258
+ </h1>
259
+ <p className="cls_email_verification_error_message mt-2 text-sm text-slate-600">
260
+ {verification.errorMessage || resolvedErrorLabels.message}
261
+ </p>
262
+ </div>
263
+ </div>
264
+ <div className="cls_email_verification_resend_form">
265
+ <FormHeader
266
+ heading={resolvedErrorLabels.resendFormHeading}
267
+ subHeading="Enter your email address to receive a new verification link."
268
+ />
269
+ <form
270
+ className="cls_email_verification_layout_form_fields flex flex-col gap-5"
271
+ onSubmit={verification.handleResendSubmit}
272
+ aria-label="Resend verification email form"
273
+ >
274
+ {renderFields(verification)}
275
+ <FormActionButtons
276
+ submitLabel={resolvedLabels.submitButton}
277
+ cancelLabel={resolvedLabels.cancelButton}
278
+ buttonPalette={resolvedButtonPalette}
279
+ isSubmitDisabled={verification.isSubmitDisabled}
280
+ onCancel={verification.handleCancel}
281
+ submitAriaLabel="Submit resend verification email form"
282
+ cancelAriaLabel="Cancel resend verification email form"
283
+ />
284
+ {verification.isSubmitting && (
285
+ <div className="cls_email_verification_submitting_indicator text-sm text-slate-600 text-center">
286
+ Sending verification email...
287
+ </div>
288
+ )}
289
+ </form>
290
+ </div>
291
+ </>
292
+ }
293
+ />
294
+ </AlreadyLoggedInGuard>
295
+ );
296
+ }
297
+
@@ -0,0 +1,58 @@
1
+ // file_description: forgot password layout specific configuration helpers
2
+ // section: imports
3
+ import type { LayoutFieldMap, LayoutFieldMapOverrides } from "@/components/layouts/shared/config/layout_customization";
4
+ import {
5
+ resolveButtonPalette,
6
+ resolveFieldDefinitions,
7
+ resolveLabels,
8
+ type ButtonPaletteDefaults,
9
+ type ButtonPaletteOverrides,
10
+ type LayoutLabelDefaults,
11
+ type LayoutLabelOverrides,
12
+ } from "@/components/layouts/shared/config/layout_customization";
13
+
14
+ // section: field_identifiers
15
+ export const FORGOT_PASSWORD_FIELD_IDS = {
16
+ EMAIL: "email_address",
17
+ } as const;
18
+
19
+ export type ForgotPasswordFieldId = (typeof FORGOT_PASSWORD_FIELD_IDS)[keyof typeof FORGOT_PASSWORD_FIELD_IDS];
20
+
21
+ // section: field_definitions
22
+ const FORGOT_PASSWORD_FIELD_DEFINITIONS: LayoutFieldMap = {
23
+ [FORGOT_PASSWORD_FIELD_IDS.EMAIL]: {
24
+ id: FORGOT_PASSWORD_FIELD_IDS.EMAIL,
25
+ label: "Email address",
26
+ type: "email",
27
+ autoComplete: "email",
28
+ placeholder: "Enter your email address",
29
+ ariaLabel: "Email address input field",
30
+ },
31
+ };
32
+
33
+ export const createForgotPasswordFieldDefinitions = (
34
+ overrides?: LayoutFieldMapOverrides,
35
+ ) => resolveFieldDefinitions(FORGOT_PASSWORD_FIELD_DEFINITIONS, overrides);
36
+
37
+ // section: label_defaults
38
+ const FORGOT_PASSWORD_LABEL_DEFAULTS: LayoutLabelDefaults = {
39
+ heading: "Forgot your password?",
40
+ subHeading: "Enter your email address and we'll send you a link to reset your password.",
41
+ submitButton: "Send reset link",
42
+ cancelButton: "Cancel",
43
+ };
44
+
45
+ export const resolveForgotPasswordLabels = (overrides?: LayoutLabelOverrides) =>
46
+ resolveLabels(FORGOT_PASSWORD_LABEL_DEFAULTS, overrides);
47
+
48
+ // section: button_palette_defaults
49
+ const FORGOT_PASSWORD_BUTTON_PALETTE_DEFAULTS: ButtonPaletteDefaults = {
50
+ submitBackground: "#0f172a",
51
+ submitText: "#ffffff",
52
+ cancelBorder: "#cbd5f5",
53
+ cancelText: "#0f172a",
54
+ };
55
+
56
+ export const resolveForgotPasswordButtonPalette = (overrides?: ButtonPaletteOverrides) =>
57
+ resolveButtonPalette(FORGOT_PASSWORD_BUTTON_PALETTE_DEFAULTS, overrides);
58
+
@@ -0,0 +1,179 @@
1
+ // file_description: encapsulate forgot password form state, validation, and data interactions
2
+ // section: imports
3
+ import { useCallback, useMemo, useState } from "react";
4
+ import { toast } from "sonner";
5
+ import type { LayoutDataClient } from "@/components/layouts/shared/data/layout_data_client";
6
+ import { FORGOT_PASSWORD_FIELD_IDS, type ForgotPasswordFieldId } from "@/components/layouts/forgot_password/config/forgot_password_field_config";
7
+ import { validateEmail } from "@/components/layouts/shared/utils/validation";
8
+
9
+ // section: types
10
+ export type ForgotPasswordFormValues = Record<ForgotPasswordFieldId, string>;
11
+ export type ForgotPasswordFormErrors = Partial<Record<ForgotPasswordFieldId, string>> & {
12
+ submit?: string;
13
+ };
14
+
15
+ export type UseForgotPasswordFormParams<TClient = unknown> = {
16
+ dataClient: LayoutDataClient<TClient>;
17
+ };
18
+
19
+ export type UseForgotPasswordFormResult = {
20
+ values: ForgotPasswordFormValues;
21
+ errors: ForgotPasswordFormErrors;
22
+ isSubmitDisabled: boolean;
23
+ isSubmitting: boolean;
24
+ emailTouched: boolean;
25
+ handleFieldChange: (fieldId: ForgotPasswordFieldId, value: string) => void;
26
+ handleEmailBlur: () => void;
27
+ handleSubmit: (event: React.FormEvent<HTMLFormElement>) => void;
28
+ handleCancel: () => void;
29
+ };
30
+
31
+ // section: helpers
32
+ const buildInitialValues = (): ForgotPasswordFormValues => ({
33
+ [FORGOT_PASSWORD_FIELD_IDS.EMAIL]: "",
34
+ });
35
+
36
+ // section: hook
37
+ export const use_forgot_password_form = <TClient,>({
38
+ dataClient,
39
+ }: UseForgotPasswordFormParams<TClient>): UseForgotPasswordFormResult => {
40
+ const [values, setValues] = useState<ForgotPasswordFormValues>(buildInitialValues);
41
+ const [errors, setErrors] = useState<ForgotPasswordFormErrors>({});
42
+ const [emailTouched, setEmailTouched] = useState<boolean>(false);
43
+ const [isSubmitting, setIsSubmitting] = useState<boolean>(false);
44
+
45
+ const isSubmitDisabled = useMemo(() => {
46
+ if (isSubmitting) {
47
+ return true;
48
+ }
49
+
50
+ const hasEmptyField = Object.values(values).some((fieldValue) => fieldValue.trim() === "");
51
+ const hasErrors = Object.keys(errors).length > 0;
52
+ return hasEmptyField || hasErrors;
53
+ }, [errors, values, isSubmitting]);
54
+
55
+ const handleFieldChange = useCallback((fieldId: ForgotPasswordFieldId, value: string) => {
56
+ setValues((previousValues) => {
57
+ const nextValues: ForgotPasswordFormValues = {
58
+ ...previousValues,
59
+ [fieldId]: value,
60
+ };
61
+
62
+ setErrors((previousErrors) => {
63
+ const updatedErrors: ForgotPasswordFormErrors = { ...previousErrors };
64
+
65
+ // Only validate email on change if it has been touched (blurred)
66
+ if (fieldId === FORGOT_PASSWORD_FIELD_IDS.EMAIL && emailTouched) {
67
+ const emailError = validateEmail(value);
68
+ if (emailError) {
69
+ updatedErrors[FORGOT_PASSWORD_FIELD_IDS.EMAIL] = emailError;
70
+ } else {
71
+ delete updatedErrors[FORGOT_PASSWORD_FIELD_IDS.EMAIL];
72
+ }
73
+ }
74
+
75
+ return updatedErrors;
76
+ });
77
+
78
+ return nextValues;
79
+ });
80
+ }, [emailTouched]);
81
+
82
+ const handleEmailBlur = useCallback(() => {
83
+ setEmailTouched(true);
84
+ // Validate email on blur
85
+ setErrors((previousErrors) => {
86
+ const updatedErrors: ForgotPasswordFormErrors = { ...previousErrors };
87
+ const emailValue = values[FORGOT_PASSWORD_FIELD_IDS.EMAIL];
88
+ const emailError = validateEmail(emailValue);
89
+ if (emailError) {
90
+ updatedErrors[FORGOT_PASSWORD_FIELD_IDS.EMAIL] = emailError;
91
+ } else {
92
+ delete updatedErrors[FORGOT_PASSWORD_FIELD_IDS.EMAIL];
93
+ }
94
+ return updatedErrors;
95
+ });
96
+ }, [values]);
97
+
98
+ const handleSubmit = useCallback(
99
+ async (event: React.FormEvent<HTMLFormElement>) => {
100
+ event.preventDefault();
101
+
102
+ // Final validation
103
+ const emailError = validateEmail(values[FORGOT_PASSWORD_FIELD_IDS.EMAIL]);
104
+
105
+ if (emailError) {
106
+ setErrors({
107
+ [FORGOT_PASSWORD_FIELD_IDS.EMAIL]: emailError,
108
+ });
109
+ return;
110
+ }
111
+
112
+ setIsSubmitting(true);
113
+ setErrors({});
114
+
115
+ try {
116
+ const response = await fetch("/api/auth/forgot_password", {
117
+ method: "POST",
118
+ headers: {
119
+ "Content-Type": "application/json",
120
+ },
121
+ body: JSON.stringify({
122
+ email: values[FORGOT_PASSWORD_FIELD_IDS.EMAIL],
123
+ }),
124
+ });
125
+
126
+ const data = await response.json();
127
+
128
+ if (!response.ok) {
129
+ throw new Error(data.error || "Password reset request failed");
130
+ }
131
+
132
+ // Show success notification
133
+ toast.success("Password reset link sent", {
134
+ description: "If an account with that email exists, a password reset link has been sent.",
135
+ });
136
+
137
+ // Reset form on success
138
+ setValues(buildInitialValues());
139
+ setErrors({});
140
+ setEmailTouched(false);
141
+ } catch (error) {
142
+ const errorMessage =
143
+ error instanceof Error ? error.message : "Password reset request failed. Please try again.";
144
+
145
+ // Show error notification
146
+ toast.error("Password reset failed", {
147
+ description: errorMessage,
148
+ });
149
+
150
+ // Set error state
151
+ setErrors({
152
+ submit: errorMessage,
153
+ });
154
+ } finally {
155
+ setIsSubmitting(false);
156
+ }
157
+ },
158
+ [values, dataClient],
159
+ );
160
+
161
+ const handleCancel = useCallback(() => {
162
+ setValues(buildInitialValues());
163
+ setErrors({});
164
+ setEmailTouched(false);
165
+ }, []);
166
+
167
+ return {
168
+ values,
169
+ errors,
170
+ isSubmitDisabled,
171
+ isSubmitting,
172
+ emailTouched,
173
+ handleFieldChange,
174
+ handleEmailBlur,
175
+ handleSubmit,
176
+ handleCancel,
177
+ };
178
+ };
179
+
@@ -0,0 +1,168 @@
1
+ // file_description: forgot password layout component built atop shared layout utilities
2
+ // section: client_directive
3
+ "use client";
4
+
5
+ // section: imports
6
+ import { Input } from "@/components/ui/input";
7
+ import { FormFieldWrapper } from "@/components/layouts/shared/components/form_field_wrapper";
8
+ import { FormHeader } from "@/components/layouts/shared/components/form_header";
9
+ import { FormActionButtons } from "@/components/layouts/shared/components/form_action_buttons";
10
+ import { TwoColumnAuthLayout } from "@/components/layouts/shared/components/two_column_auth_layout";
11
+ import { AlreadyLoggedInGuard } from "@/components/layouts/shared/components/already_logged_in_guard";
12
+ import {
13
+ type ButtonPaletteOverrides,
14
+ type LayoutFieldMapOverrides,
15
+ type LayoutLabelOverrides,
16
+ } from "@/components/layouts/shared/config/layout_customization";
17
+ import {
18
+ FORGOT_PASSWORD_FIELD_IDS,
19
+ createForgotPasswordFieldDefinitions,
20
+ resolveForgotPasswordButtonPalette,
21
+ resolveForgotPasswordLabels,
22
+ } from "@/components/layouts/forgot_password/config/forgot_password_field_config";
23
+ import {
24
+ use_forgot_password_form,
25
+ type UseForgotPasswordFormResult,
26
+ } from "@/components/layouts/forgot_password/hooks/use_forgot_password_form";
27
+ import { type LayoutDataClient } from "@/components/layouts/shared/data/layout_data_client";
28
+
29
+ // section: types
30
+ export type ForgotPasswordLayoutProps<TClient = unknown> = {
31
+ image_src: string;
32
+ image_alt: string;
33
+ image_background_color?: string;
34
+ field_overrides?: LayoutFieldMapOverrides;
35
+ labels?: LayoutLabelOverrides;
36
+ button_colors?: ButtonPaletteOverrides;
37
+ data_client: LayoutDataClient<TClient>;
38
+ alreadyLoggedInMessage?: string;
39
+ showLogoutButton?: boolean;
40
+ showReturnHomeButton?: boolean;
41
+ returnHomeButtonLabel?: string;
42
+ returnHomePath?: string;
43
+ };
44
+
45
+ const ORDERED_FIELDS: ForgotPasswordFieldId[] = [
46
+ FORGOT_PASSWORD_FIELD_IDS.EMAIL,
47
+ ];
48
+
49
+ type ForgotPasswordFieldId = (typeof FORGOT_PASSWORD_FIELD_IDS)[keyof typeof FORGOT_PASSWORD_FIELD_IDS];
50
+
51
+ // section: component
52
+ export default function forgot_password_layout<TClient>({
53
+ image_src,
54
+ image_alt,
55
+ image_background_color = "#f1f5f9",
56
+ field_overrides,
57
+ labels,
58
+ button_colors,
59
+ data_client,
60
+ alreadyLoggedInMessage = "You are already logged in",
61
+ showLogoutButton = true,
62
+ showReturnHomeButton = false,
63
+ returnHomeButtonLabel = "Return home",
64
+ returnHomePath = "/",
65
+ }: ForgotPasswordLayoutProps<TClient>) {
66
+ const fieldDefinitions = createForgotPasswordFieldDefinitions(field_overrides);
67
+ const resolvedLabels = resolveForgotPasswordLabels(labels);
68
+ const resolvedButtonPalette = resolveForgotPasswordButtonPalette(button_colors);
69
+
70
+ const form = use_forgot_password_form({
71
+ dataClient: data_client,
72
+ });
73
+
74
+ const renderFields = (formState: UseForgotPasswordFormResult) => {
75
+ return ORDERED_FIELDS.map((fieldId) => {
76
+ const fieldDefinition = fieldDefinitions[fieldId];
77
+ const fieldValue = formState.values[fieldId];
78
+ const fieldError = formState.errors[fieldId];
79
+
80
+ const inputElement = (
81
+ <Input
82
+ id={fieldDefinition.id}
83
+ type={fieldDefinition.type}
84
+ value={fieldValue}
85
+ onChange={(event) =>
86
+ formState.handleFieldChange(fieldId, event.target.value)
87
+ }
88
+ onBlur={
89
+ fieldId === FORGOT_PASSWORD_FIELD_IDS.EMAIL
90
+ ? formState.handleEmailBlur
91
+ : undefined
92
+ }
93
+ autoComplete={fieldDefinition.autoComplete}
94
+ placeholder={fieldDefinition.placeholder}
95
+ aria-label={fieldDefinition.ariaLabel}
96
+ className="cls_forgot_password_layout_field_input"
97
+ />
98
+ );
99
+
100
+ // Only show email error if field has been touched (blurred)
101
+ const shouldShowError =
102
+ fieldId === FORGOT_PASSWORD_FIELD_IDS.EMAIL
103
+ ? formState.emailTouched && fieldError
104
+ ? fieldError
105
+ : undefined
106
+ : fieldError;
107
+
108
+ return (
109
+ <FormFieldWrapper
110
+ key={fieldId}
111
+ fieldId={fieldDefinition.id}
112
+ label={fieldDefinition.label}
113
+ input={inputElement}
114
+ errorMessage={shouldShowError}
115
+ />
116
+ );
117
+ });
118
+ };
119
+
120
+ return (
121
+ <AlreadyLoggedInGuard
122
+ image_src={image_src}
123
+ image_alt={image_alt}
124
+ image_background_color={image_background_color}
125
+ message={alreadyLoggedInMessage}
126
+ showLogoutButton={showLogoutButton}
127
+ showReturnHomeButton={showReturnHomeButton}
128
+ returnHomeButtonLabel={returnHomeButtonLabel}
129
+ returnHomePath={returnHomePath}
130
+ >
131
+ <TwoColumnAuthLayout
132
+ imageSrc={image_src}
133
+ imageAlt={image_alt}
134
+ imageBackgroundColor={image_background_color}
135
+ formContent={
136
+ <>
137
+ <FormHeader
138
+ heading={resolvedLabels.heading}
139
+ subHeading={resolvedLabels.subHeading}
140
+ />
141
+ <form
142
+ className="cls_forgot_password_layout_form_fields flex flex-col gap-5"
143
+ onSubmit={form.handleSubmit}
144
+ aria-label="Forgot password form"
145
+ >
146
+ {renderFields(form)}
147
+ <FormActionButtons
148
+ submitLabel={resolvedLabels.submitButton}
149
+ cancelLabel={resolvedLabels.cancelButton}
150
+ buttonPalette={resolvedButtonPalette}
151
+ isSubmitDisabled={form.isSubmitDisabled}
152
+ onCancel={form.handleCancel}
153
+ submitAriaLabel="Submit forgot password form"
154
+ cancelAriaLabel="Cancel forgot password form"
155
+ />
156
+ {form.isSubmitting && (
157
+ <div className="cls_forgot_password_submitting_indicator text-sm text-slate-600 text-center">
158
+ Sending reset link...
159
+ </div>
160
+ )}
161
+ </form>
162
+ </>
163
+ }
164
+ />
165
+ </AlreadyLoggedInGuard>
166
+ );
167
+ }
168
+