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.
- package/LICENSE +21 -0
- package/README.md +48 -0
- package/components.json +22 -0
- package/hazo_auth_config.example.ini +414 -0
- package/hazo_notify_config.example.ini +159 -0
- package/instrumentation.ts +32 -0
- package/migrations/001_add_token_type_to_refresh_tokens.sql +14 -0
- package/migrations/002_add_name_to_hazo_users.sql +7 -0
- package/next.config.mjs +55 -0
- package/package.json +114 -0
- package/postcss.config.mjs +8 -0
- package/public/file.svg +1 -0
- package/public/globe.svg +1 -0
- package/public/next.svg +1 -0
- package/public/vercel.svg +1 -0
- package/public/window.svg +1 -0
- package/scripts/apply_migration.ts +118 -0
- package/src/app/api/auth/change_password/route.ts +109 -0
- package/src/app/api/auth/forgot_password/route.ts +107 -0
- package/src/app/api/auth/library_photos/route.ts +70 -0
- package/src/app/api/auth/login/route.ts +155 -0
- package/src/app/api/auth/logout/route.ts +62 -0
- package/src/app/api/auth/me/route.ts +47 -0
- package/src/app/api/auth/profile_picture/[filename]/route.ts +67 -0
- package/src/app/api/auth/register/route.ts +106 -0
- package/src/app/api/auth/remove_profile_picture/route.ts +86 -0
- package/src/app/api/auth/resend_verification/route.ts +107 -0
- package/src/app/api/auth/reset_password/route.ts +107 -0
- package/src/app/api/auth/update_user/route.ts +126 -0
- package/src/app/api/auth/upload_profile_picture/route.ts +268 -0
- package/src/app/api/auth/validate_reset_token/route.ts +80 -0
- package/src/app/api/auth/verify_email/route.ts +85 -0
- package/src/app/api/migrations/apply/route.ts +91 -0
- package/src/app/favicon.ico +0 -0
- package/src/app/fonts/GeistMonoVF.woff +0 -0
- package/src/app/fonts/GeistVF.woff +0 -0
- package/src/app/forgot_password/forgot_password_page_client.tsx +60 -0
- package/src/app/forgot_password/page.tsx +24 -0
- package/src/app/globals.css +89 -0
- package/src/app/hazo_connect/api/sqlite/data/route.ts +197 -0
- package/src/app/hazo_connect/api/sqlite/schema/route.ts +35 -0
- package/src/app/hazo_connect/api/sqlite/tables/route.ts +26 -0
- package/src/app/hazo_connect/sqlite_admin/page.tsx +51 -0
- package/src/app/hazo_connect/sqlite_admin/sqlite-admin-client.tsx +947 -0
- package/src/app/layout.tsx +43 -0
- package/src/app/login/login_page_client.tsx +71 -0
- package/src/app/login/page.tsx +26 -0
- package/src/app/my_settings/my_settings_page_client.tsx +120 -0
- package/src/app/my_settings/page.tsx +40 -0
- package/src/app/page.tsx +170 -0
- package/src/app/register/page.tsx +26 -0
- package/src/app/register/register_page_client.tsx +72 -0
- package/src/app/reset_password/page.tsx +29 -0
- package/src/app/reset_password/reset_password_page_client.tsx +81 -0
- package/src/app/verify_email/page.tsx +24 -0
- package/src/app/verify_email/verify_email_page_client.tsx +60 -0
- package/src/components/layouts/email_verification/config/email_verification_field_config.ts +86 -0
- package/src/components/layouts/email_verification/hooks/use_email_verification.ts +291 -0
- package/src/components/layouts/email_verification/index.tsx +297 -0
- package/src/components/layouts/forgot_password/config/forgot_password_field_config.ts +58 -0
- package/src/components/layouts/forgot_password/hooks/use_forgot_password_form.ts +179 -0
- package/src/components/layouts/forgot_password/index.tsx +168 -0
- package/src/components/layouts/login/config/login_field_config.ts +67 -0
- package/src/components/layouts/login/hooks/use_login_form.ts +281 -0
- package/src/components/layouts/login/index.tsx +224 -0
- package/src/components/layouts/my_settings/components/editable_field.tsx +177 -0
- package/src/components/layouts/my_settings/components/password_change_dialog.tsx +301 -0
- package/src/components/layouts/my_settings/components/profile_picture_dialog.tsx +385 -0
- package/src/components/layouts/my_settings/components/profile_picture_display.tsx +66 -0
- package/src/components/layouts/my_settings/components/profile_picture_gravatar_tab.tsx +143 -0
- package/src/components/layouts/my_settings/components/profile_picture_library_tab.tsx +282 -0
- package/src/components/layouts/my_settings/components/profile_picture_upload_tab.tsx +341 -0
- package/src/components/layouts/my_settings/config/my_settings_field_config.ts +61 -0
- package/src/components/layouts/my_settings/hooks/use_my_settings.ts +458 -0
- package/src/components/layouts/my_settings/index.tsx +351 -0
- package/src/components/layouts/register/config/register_field_config.ts +101 -0
- package/src/components/layouts/register/hooks/use_register_form.ts +272 -0
- package/src/components/layouts/register/index.tsx +208 -0
- package/src/components/layouts/reset_password/config/reset_password_field_config.ts +86 -0
- package/src/components/layouts/reset_password/hooks/use_reset_password_form.ts +276 -0
- package/src/components/layouts/reset_password/index.tsx +294 -0
- package/src/components/layouts/shared/components/already_logged_in_guard.tsx +95 -0
- package/src/components/layouts/shared/components/field_error_message.tsx +29 -0
- package/src/components/layouts/shared/components/form_action_buttons.tsx +64 -0
- package/src/components/layouts/shared/components/form_field_wrapper.tsx +44 -0
- package/src/components/layouts/shared/components/form_header.tsx +36 -0
- package/src/components/layouts/shared/components/logout_button.tsx +76 -0
- package/src/components/layouts/shared/components/password_field.tsx +72 -0
- package/src/components/layouts/shared/components/sidebar_layout_wrapper.tsx +264 -0
- package/src/components/layouts/shared/components/two_column_auth_layout.tsx +44 -0
- package/src/components/layouts/shared/components/unauthorized_guard.tsx +78 -0
- package/src/components/layouts/shared/components/visual_panel.tsx +41 -0
- package/src/components/layouts/shared/config/layout_customization.ts +95 -0
- package/src/components/layouts/shared/data/layout_data_client.ts +19 -0
- package/src/components/layouts/shared/hooks/use_auth_status.ts +103 -0
- package/src/components/layouts/shared/utils/ip_address.ts +37 -0
- package/src/components/layouts/shared/utils/validation.ts +66 -0
- package/src/components/ui/avatar.tsx +50 -0
- package/src/components/ui/button.tsx +57 -0
- package/src/components/ui/dialog.tsx +122 -0
- package/src/components/ui/hazo_ui_tooltip.tsx +67 -0
- package/src/components/ui/input.tsx +22 -0
- package/src/components/ui/label.tsx +26 -0
- package/src/components/ui/separator.tsx +31 -0
- package/src/components/ui/sheet.tsx +139 -0
- package/src/components/ui/sidebar.tsx +773 -0
- package/src/components/ui/skeleton.tsx +15 -0
- package/src/components/ui/sonner.tsx +31 -0
- package/src/components/ui/switch.tsx +29 -0
- package/src/components/ui/tabs.tsx +55 -0
- package/src/components/ui/tooltip.tsx +32 -0
- package/src/components/ui/vertical-tabs.tsx +59 -0
- package/src/hooks/use-mobile.tsx +19 -0
- package/src/lib/already_logged_in_config.server.ts +46 -0
- package/src/lib/app_logger.ts +24 -0
- package/src/lib/auth/auth_utils.server.ts +196 -0
- package/src/lib/auth/server_auth.ts +88 -0
- package/src/lib/config/config_loader.server.ts +149 -0
- package/src/lib/email_verification_config.server.ts +32 -0
- package/src/lib/file_types_config.server.ts +25 -0
- package/src/lib/forgot_password_config.server.ts +32 -0
- package/src/lib/hazo_connect_instance.server.ts +77 -0
- package/src/lib/hazo_connect_setup.server.ts +181 -0
- package/src/lib/hazo_connect_setup.ts +54 -0
- package/src/lib/login_config.server.ts +46 -0
- package/src/lib/messages_config.server.ts +45 -0
- package/src/lib/migrations/apply_migration.ts +105 -0
- package/src/lib/my_settings_config.server.ts +135 -0
- package/src/lib/password_requirements_config.server.ts +39 -0
- package/src/lib/profile_picture_config.server.ts +56 -0
- package/src/lib/register_config.server.ts +57 -0
- package/src/lib/reset_password_config.server.ts +75 -0
- package/src/lib/services/email_service.ts +581 -0
- package/src/lib/services/email_verification_service.ts +264 -0
- package/src/lib/services/login_service.ts +118 -0
- package/src/lib/services/password_change_service.ts +154 -0
- package/src/lib/services/password_reset_service.ts +405 -0
- package/src/lib/services/profile_picture_remove_service.ts +120 -0
- package/src/lib/services/profile_picture_service.ts +215 -0
- package/src/lib/services/profile_picture_source_mapper.ts +62 -0
- package/src/lib/services/registration_service.ts +163 -0
- package/src/lib/services/token_service.ts +240 -0
- package/src/lib/services/user_update_service.ts +128 -0
- package/src/lib/ui_sizes_config.server.ts +37 -0
- package/src/lib/user_fields_config.server.ts +31 -0
- package/src/lib/utils/api_route_helpers.ts +60 -0
- package/src/lib/utils.ts +11 -0
- package/src/middleware.ts +91 -0
- package/src/server/config/config_loader.ts +496 -0
- package/src/server/index.ts +38 -0
- package/src/server/logging/logger_service.ts +56 -0
- package/src/server/routes/root_router.ts +16 -0
- package/src/server/server.ts +28 -0
- package/src/server/types/app_types.ts +74 -0
- package/src/server/types/express.d.ts +15 -0
- package/src/stories/email_verification_layout.stories.tsx +137 -0
- package/src/stories/forgot_password_layout.stories.tsx +85 -0
- package/src/stories/login_layout.stories.tsx +85 -0
- package/src/stories/project_overview.stories.tsx +33 -0
- package/src/stories/register_layout.stories.tsx +107 -0
- package/tailwind.config.ts +77 -0
- package/tsconfig.json +27 -0
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
// file_description: client component for reset password page that initializes hazo_connect and renders reset password layout
|
|
2
|
+
// section: client_directive
|
|
3
|
+
"use client";
|
|
4
|
+
|
|
5
|
+
// section: imports
|
|
6
|
+
import { useEffect, useState } from "react";
|
|
7
|
+
import reset_password_layout from "@/components/layouts/reset_password";
|
|
8
|
+
import { createLayoutDataClient } from "@/components/layouts/shared/data/layout_data_client";
|
|
9
|
+
import { create_sqlite_hazo_connect } from "@/lib/hazo_connect_setup";
|
|
10
|
+
import type { LayoutDataClient } from "@/components/layouts/shared/data/layout_data_client";
|
|
11
|
+
|
|
12
|
+
// section: types
|
|
13
|
+
type ResetPasswordPageClientProps = {
|
|
14
|
+
errorMessage?: string;
|
|
15
|
+
successMessage?: string;
|
|
16
|
+
loginPath?: string;
|
|
17
|
+
forgotPasswordPath?: string;
|
|
18
|
+
alreadyLoggedInMessage?: string;
|
|
19
|
+
showLogoutButton?: boolean;
|
|
20
|
+
showReturnHomeButton?: boolean;
|
|
21
|
+
returnHomeButtonLabel?: string;
|
|
22
|
+
returnHomePath?: string;
|
|
23
|
+
passwordRequirements?: {
|
|
24
|
+
minimum_length: number;
|
|
25
|
+
require_uppercase: boolean;
|
|
26
|
+
require_lowercase: boolean;
|
|
27
|
+
require_number: boolean;
|
|
28
|
+
require_special: boolean;
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
// section: component
|
|
33
|
+
export function ResetPasswordPageClient({
|
|
34
|
+
errorMessage,
|
|
35
|
+
successMessage,
|
|
36
|
+
loginPath,
|
|
37
|
+
forgotPasswordPath,
|
|
38
|
+
alreadyLoggedInMessage,
|
|
39
|
+
showLogoutButton,
|
|
40
|
+
showReturnHomeButton,
|
|
41
|
+
returnHomeButtonLabel,
|
|
42
|
+
returnHomePath,
|
|
43
|
+
passwordRequirements,
|
|
44
|
+
}: ResetPasswordPageClientProps) {
|
|
45
|
+
const [dataClient, setDataClient] = useState<LayoutDataClient<unknown> | null>(null);
|
|
46
|
+
|
|
47
|
+
useEffect(() => {
|
|
48
|
+
// Initialize hazo_connect on client side
|
|
49
|
+
const hazoConnect = create_sqlite_hazo_connect();
|
|
50
|
+
const client = createLayoutDataClient(hazoConnect);
|
|
51
|
+
|
|
52
|
+
setDataClient(client);
|
|
53
|
+
}, []);
|
|
54
|
+
|
|
55
|
+
// Show loading state while initializing
|
|
56
|
+
if (!dataClient) {
|
|
57
|
+
return <div className="cls_reset_password_page_loading text-slate-600">Loading...</div>;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const ResetPasswordLayout = reset_password_layout;
|
|
61
|
+
|
|
62
|
+
return (
|
|
63
|
+
<ResetPasswordLayout
|
|
64
|
+
image_src="/globe.svg"
|
|
65
|
+
image_alt="Illustration of a globe representing secure authentication workflows"
|
|
66
|
+
image_background_color="#e2e8f0"
|
|
67
|
+
data_client={dataClient}
|
|
68
|
+
errorMessage={errorMessage}
|
|
69
|
+
successMessage={successMessage}
|
|
70
|
+
loginPath={loginPath}
|
|
71
|
+
forgotPasswordPath={forgotPasswordPath}
|
|
72
|
+
alreadyLoggedInMessage={alreadyLoggedInMessage}
|
|
73
|
+
showLogoutButton={showLogoutButton}
|
|
74
|
+
showReturnHomeButton={showReturnHomeButton}
|
|
75
|
+
returnHomeButtonLabel={returnHomeButtonLabel}
|
|
76
|
+
returnHomePath={returnHomePath}
|
|
77
|
+
password_requirements={passwordRequirements}
|
|
78
|
+
/>
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// file_description: render the email verification page shell and mount the email verification layout component within sidebar
|
|
2
|
+
// section: imports
|
|
3
|
+
import { SidebarLayoutWrapper } from "@/components/layouts/shared/components/sidebar_layout_wrapper";
|
|
4
|
+
import { VerifyEmailPageClient } from "./verify_email_page_client";
|
|
5
|
+
import { get_email_verification_config } from "@/lib/email_verification_config.server";
|
|
6
|
+
|
|
7
|
+
// section: component
|
|
8
|
+
export default function verify_email_page() {
|
|
9
|
+
// Read email verification configuration from hazo_auth_config.ini (server-side)
|
|
10
|
+
const emailVerificationConfig = get_email_verification_config();
|
|
11
|
+
|
|
12
|
+
return (
|
|
13
|
+
<SidebarLayoutWrapper>
|
|
14
|
+
<VerifyEmailPageClient
|
|
15
|
+
alreadyLoggedInMessage={emailVerificationConfig.alreadyLoggedInMessage}
|
|
16
|
+
showLogoutButton={emailVerificationConfig.showLogoutButton}
|
|
17
|
+
showReturnHomeButton={emailVerificationConfig.showReturnHomeButton}
|
|
18
|
+
returnHomeButtonLabel={emailVerificationConfig.returnHomeButtonLabel}
|
|
19
|
+
returnHomePath={emailVerificationConfig.returnHomePath}
|
|
20
|
+
/>
|
|
21
|
+
</SidebarLayoutWrapper>
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// file_description: client component for verify email page that initializes hazo_connect and renders email verification layout
|
|
2
|
+
// section: client_directive
|
|
3
|
+
"use client";
|
|
4
|
+
|
|
5
|
+
// section: imports
|
|
6
|
+
import { useEffect, useState } from "react";
|
|
7
|
+
import email_verification_layout from "@/components/layouts/email_verification";
|
|
8
|
+
import { createLayoutDataClient } from "@/components/layouts/shared/data/layout_data_client";
|
|
9
|
+
import { create_sqlite_hazo_connect } from "@/lib/hazo_connect_setup";
|
|
10
|
+
import type { LayoutDataClient } from "@/components/layouts/shared/data/layout_data_client";
|
|
11
|
+
|
|
12
|
+
// section: types
|
|
13
|
+
type VerifyEmailPageClientProps = {
|
|
14
|
+
alreadyLoggedInMessage?: string;
|
|
15
|
+
showLogoutButton?: boolean;
|
|
16
|
+
showReturnHomeButton?: boolean;
|
|
17
|
+
returnHomeButtonLabel?: string;
|
|
18
|
+
returnHomePath?: string;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
// section: component
|
|
22
|
+
export function VerifyEmailPageClient({
|
|
23
|
+
alreadyLoggedInMessage,
|
|
24
|
+
showLogoutButton,
|
|
25
|
+
showReturnHomeButton,
|
|
26
|
+
returnHomeButtonLabel,
|
|
27
|
+
returnHomePath,
|
|
28
|
+
}: VerifyEmailPageClientProps) {
|
|
29
|
+
const [dataClient, setDataClient] = useState<LayoutDataClient<unknown> | null>(null);
|
|
30
|
+
|
|
31
|
+
useEffect(() => {
|
|
32
|
+
// Initialize hazo_connect on client side
|
|
33
|
+
const hazoConnect = create_sqlite_hazo_connect();
|
|
34
|
+
const client = createLayoutDataClient(hazoConnect);
|
|
35
|
+
|
|
36
|
+
setDataClient(client);
|
|
37
|
+
}, []);
|
|
38
|
+
|
|
39
|
+
// Show loading state while initializing
|
|
40
|
+
if (!dataClient) {
|
|
41
|
+
return <div className="cls_verify_email_page_loading text-slate-600">Loading...</div>;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const EmailVerificationLayout = email_verification_layout;
|
|
45
|
+
|
|
46
|
+
return (
|
|
47
|
+
<EmailVerificationLayout
|
|
48
|
+
image_src="/globe.svg"
|
|
49
|
+
image_alt="Illustration of a globe representing secure authentication workflows"
|
|
50
|
+
image_background_color="#e2e8f0"
|
|
51
|
+
data_client={dataClient}
|
|
52
|
+
already_logged_in_message={alreadyLoggedInMessage}
|
|
53
|
+
showLogoutButton={showLogoutButton}
|
|
54
|
+
showReturnHomeButton={showReturnHomeButton}
|
|
55
|
+
returnHomeButtonLabel={returnHomeButtonLabel}
|
|
56
|
+
returnHomePath={returnHomePath}
|
|
57
|
+
/>
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// file_description: email verification 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 EMAIL_VERIFICATION_FIELD_IDS = {
|
|
16
|
+
EMAIL: "email_address",
|
|
17
|
+
} as const;
|
|
18
|
+
|
|
19
|
+
export type EmailVerificationFieldId = (typeof EMAIL_VERIFICATION_FIELD_IDS)[keyof typeof EMAIL_VERIFICATION_FIELD_IDS];
|
|
20
|
+
|
|
21
|
+
// section: field_definitions
|
|
22
|
+
const EMAIL_VERIFICATION_FIELD_DEFINITIONS: LayoutFieldMap = {
|
|
23
|
+
[EMAIL_VERIFICATION_FIELD_IDS.EMAIL]: {
|
|
24
|
+
id: EMAIL_VERIFICATION_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 createEmailVerificationFieldDefinitions = (
|
|
34
|
+
overrides?: LayoutFieldMapOverrides,
|
|
35
|
+
) => resolveFieldDefinitions(EMAIL_VERIFICATION_FIELD_DEFINITIONS, overrides);
|
|
36
|
+
|
|
37
|
+
// section: label_defaults
|
|
38
|
+
const EMAIL_VERIFICATION_LABEL_DEFAULTS: LayoutLabelDefaults = {
|
|
39
|
+
heading: "Email verification",
|
|
40
|
+
subHeading: "Verifying your email address...",
|
|
41
|
+
submitButton: "Resend verification email",
|
|
42
|
+
cancelButton: "Cancel",
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export const resolveEmailVerificationLabels = (overrides?: LayoutLabelOverrides) =>
|
|
46
|
+
resolveLabels(EMAIL_VERIFICATION_LABEL_DEFAULTS, overrides);
|
|
47
|
+
|
|
48
|
+
// section: button_palette_defaults
|
|
49
|
+
const EMAIL_VERIFICATION_BUTTON_PALETTE_DEFAULTS: ButtonPaletteDefaults = {
|
|
50
|
+
submitBackground: "#0f172a",
|
|
51
|
+
submitText: "#ffffff",
|
|
52
|
+
cancelBorder: "#cbd5f5",
|
|
53
|
+
cancelText: "#0f172a",
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
export const resolveEmailVerificationButtonPalette = (overrides?: ButtonPaletteOverrides) =>
|
|
57
|
+
resolveButtonPalette(EMAIL_VERIFICATION_BUTTON_PALETTE_DEFAULTS, overrides);
|
|
58
|
+
|
|
59
|
+
// section: success_labels
|
|
60
|
+
export type EmailVerificationSuccessLabels = {
|
|
61
|
+
heading: string;
|
|
62
|
+
message: string;
|
|
63
|
+
redirectMessage: string;
|
|
64
|
+
goToLoginButton: string;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
export const EMAIL_VERIFICATION_SUCCESS_LABEL_DEFAULTS: EmailVerificationSuccessLabels = {
|
|
68
|
+
heading: "Email verified successfully",
|
|
69
|
+
message: "Your email address has been verified. You can now log in to your account.",
|
|
70
|
+
redirectMessage: "Redirecting to login page in",
|
|
71
|
+
goToLoginButton: "Go to login",
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
// section: error_labels
|
|
75
|
+
export type EmailVerificationErrorLabels = {
|
|
76
|
+
heading: string;
|
|
77
|
+
message: string;
|
|
78
|
+
resendFormHeading: string;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
export const EMAIL_VERIFICATION_ERROR_LABEL_DEFAULTS: EmailVerificationErrorLabels = {
|
|
82
|
+
heading: "Verification failed",
|
|
83
|
+
message: "The verification link is invalid or has expired.",
|
|
84
|
+
resendFormHeading: "Resend verification email",
|
|
85
|
+
};
|
|
86
|
+
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
// file_description: encapsulate email verification state, validation, and data interactions
|
|
2
|
+
// section: imports
|
|
3
|
+
import { useCallback, useEffect, useMemo, useState } from "react";
|
|
4
|
+
import { useRouter, useSearchParams } from "next/navigation";
|
|
5
|
+
import { toast } from "sonner";
|
|
6
|
+
import type { LayoutDataClient } from "@/components/layouts/shared/data/layout_data_client";
|
|
7
|
+
import { EMAIL_VERIFICATION_FIELD_IDS, type EmailVerificationFieldId } from "@/components/layouts/email_verification/config/email_verification_field_config";
|
|
8
|
+
import { validateEmail } from "@/components/layouts/shared/utils/validation";
|
|
9
|
+
|
|
10
|
+
// section: types
|
|
11
|
+
export type EmailVerificationFormValues = Record<EmailVerificationFieldId, string>;
|
|
12
|
+
export type EmailVerificationFormErrors = Partial<Record<EmailVerificationFieldId, string>> & {
|
|
13
|
+
submit?: string;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export type UseEmailVerificationParams<TClient = unknown> = {
|
|
17
|
+
dataClient: LayoutDataClient<TClient>;
|
|
18
|
+
redirectDelay?: number; // Delay in seconds before redirecting to login
|
|
19
|
+
loginPath?: string; // Path to redirect to after successful verification
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export type UseEmailVerificationResult = {
|
|
23
|
+
isVerifying: boolean;
|
|
24
|
+
isVerified: boolean;
|
|
25
|
+
isError: boolean;
|
|
26
|
+
errorMessage?: string;
|
|
27
|
+
email?: string; // Email from token if available
|
|
28
|
+
values: EmailVerificationFormValues;
|
|
29
|
+
errors: EmailVerificationFormErrors;
|
|
30
|
+
isSubmitDisabled: boolean;
|
|
31
|
+
isSubmitting: boolean;
|
|
32
|
+
emailTouched: boolean;
|
|
33
|
+
redirectCountdown: number;
|
|
34
|
+
handleFieldChange: (fieldId: EmailVerificationFieldId, value: string) => void;
|
|
35
|
+
handleEmailBlur: () => void;
|
|
36
|
+
handleResendSubmit: (event: React.FormEvent<HTMLFormElement>) => void;
|
|
37
|
+
handleCancel: () => void;
|
|
38
|
+
handleGoToLogin: () => void;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// section: helpers
|
|
42
|
+
const buildInitialValues = (initialEmail?: string): EmailVerificationFormValues => ({
|
|
43
|
+
[EMAIL_VERIFICATION_FIELD_IDS.EMAIL]: initialEmail || "",
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// section: hook
|
|
47
|
+
export const use_email_verification = <TClient,>({
|
|
48
|
+
dataClient,
|
|
49
|
+
redirectDelay = 5,
|
|
50
|
+
loginPath = "/login",
|
|
51
|
+
}: UseEmailVerificationParams<TClient>): UseEmailVerificationResult => {
|
|
52
|
+
const router = useRouter();
|
|
53
|
+
const searchParams = useSearchParams();
|
|
54
|
+
const token = searchParams.get("token");
|
|
55
|
+
const emailParam = searchParams.get("email");
|
|
56
|
+
const messageParam = searchParams.get("message");
|
|
57
|
+
|
|
58
|
+
const [isVerifying, setIsVerifying] = useState<boolean>(false);
|
|
59
|
+
const [isVerified, setIsVerified] = useState<boolean>(false);
|
|
60
|
+
const [isError, setIsError] = useState<boolean>(false);
|
|
61
|
+
const [errorMessage, setErrorMessage] = useState<string | undefined>(messageParam || undefined);
|
|
62
|
+
const [email, setEmail] = useState<string | undefined>(emailParam || undefined);
|
|
63
|
+
const [values, setValues] = useState<EmailVerificationFormValues>(buildInitialValues(emailParam || undefined));
|
|
64
|
+
const [errors, setErrors] = useState<EmailVerificationFormErrors>({});
|
|
65
|
+
const [emailTouched, setEmailTouched] = useState<boolean>(false);
|
|
66
|
+
const [isSubmitting, setIsSubmitting] = useState<boolean>(false);
|
|
67
|
+
const [redirectCountdown, setRedirectCountdown] = useState<number>(redirectDelay);
|
|
68
|
+
|
|
69
|
+
// Verify token on mount if token exists, or show error if message is provided
|
|
70
|
+
useEffect(() => {
|
|
71
|
+
// If message is provided (from login redirect), show error state immediately
|
|
72
|
+
if (messageParam && !token) {
|
|
73
|
+
setIsError(true);
|
|
74
|
+
setErrorMessage(messageParam);
|
|
75
|
+
if (emailParam) {
|
|
76
|
+
setEmail(emailParam);
|
|
77
|
+
setValues(buildInitialValues(emailParam));
|
|
78
|
+
}
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (!token) {
|
|
83
|
+
setIsError(true);
|
|
84
|
+
setErrorMessage("No verification token provided");
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const verifyToken = async () => {
|
|
89
|
+
setIsVerifying(true);
|
|
90
|
+
setIsError(false);
|
|
91
|
+
setErrorMessage(undefined);
|
|
92
|
+
|
|
93
|
+
try {
|
|
94
|
+
const response = await fetch(`/api/auth/verify_email?token=${encodeURIComponent(token)}`, {
|
|
95
|
+
method: "GET",
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
const data = await response.json();
|
|
99
|
+
|
|
100
|
+
if (!response.ok) {
|
|
101
|
+
throw new Error(data.error || "Email verification failed");
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Success
|
|
105
|
+
setIsVerified(true);
|
|
106
|
+
setEmail(data.email);
|
|
107
|
+
setValues(buildInitialValues(data.email));
|
|
108
|
+
|
|
109
|
+
// Start countdown for redirect
|
|
110
|
+
let countdown = redirectDelay;
|
|
111
|
+
setRedirectCountdown(countdown);
|
|
112
|
+
|
|
113
|
+
const countdownInterval = setInterval(() => {
|
|
114
|
+
countdown -= 1;
|
|
115
|
+
setRedirectCountdown(countdown);
|
|
116
|
+
|
|
117
|
+
if (countdown <= 0) {
|
|
118
|
+
clearInterval(countdownInterval);
|
|
119
|
+
router.push(loginPath);
|
|
120
|
+
}
|
|
121
|
+
}, 1000);
|
|
122
|
+
|
|
123
|
+
// Cleanup interval on unmount
|
|
124
|
+
return () => clearInterval(countdownInterval);
|
|
125
|
+
} catch (error) {
|
|
126
|
+
const errorMessage =
|
|
127
|
+
error instanceof Error ? error.message : "Email verification failed. Please try again.";
|
|
128
|
+
|
|
129
|
+
setIsError(true);
|
|
130
|
+
setErrorMessage(errorMessage);
|
|
131
|
+
|
|
132
|
+
// Try to extract email from error response if available
|
|
133
|
+
try {
|
|
134
|
+
const response = await fetch(`/api/auth/verify_email?token=${encodeURIComponent(token)}`, {
|
|
135
|
+
method: "GET",
|
|
136
|
+
});
|
|
137
|
+
const data = await response.json();
|
|
138
|
+
if (data.email) {
|
|
139
|
+
setEmail(data.email);
|
|
140
|
+
setValues(buildInitialValues(data.email));
|
|
141
|
+
}
|
|
142
|
+
} catch {
|
|
143
|
+
// Ignore errors when trying to get email
|
|
144
|
+
}
|
|
145
|
+
} finally {
|
|
146
|
+
setIsVerifying(false);
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
void verifyToken();
|
|
151
|
+
}, [token, redirectDelay, loginPath, router]);
|
|
152
|
+
|
|
153
|
+
const isSubmitDisabled = useMemo(() => {
|
|
154
|
+
if (isSubmitting) {
|
|
155
|
+
return true;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const hasEmptyField = Object.values(values).some((fieldValue) => fieldValue.trim() === "");
|
|
159
|
+
const hasErrors = Object.keys(errors).length > 0;
|
|
160
|
+
return hasEmptyField || hasErrors;
|
|
161
|
+
}, [errors, values, isSubmitting]);
|
|
162
|
+
|
|
163
|
+
const handleFieldChange = useCallback((fieldId: EmailVerificationFieldId, value: string) => {
|
|
164
|
+
setValues((previousValues) => {
|
|
165
|
+
const nextValues: EmailVerificationFormValues = {
|
|
166
|
+
...previousValues,
|
|
167
|
+
[fieldId]: value,
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
setErrors((previousErrors) => {
|
|
171
|
+
const updatedErrors: EmailVerificationFormErrors = { ...previousErrors };
|
|
172
|
+
|
|
173
|
+
// Only validate email on change if it has been touched (blurred)
|
|
174
|
+
if (fieldId === EMAIL_VERIFICATION_FIELD_IDS.EMAIL && emailTouched) {
|
|
175
|
+
const emailError = validateEmail(value);
|
|
176
|
+
if (emailError) {
|
|
177
|
+
updatedErrors[EMAIL_VERIFICATION_FIELD_IDS.EMAIL] = emailError;
|
|
178
|
+
} else {
|
|
179
|
+
delete updatedErrors[EMAIL_VERIFICATION_FIELD_IDS.EMAIL];
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
return updatedErrors;
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
return nextValues;
|
|
187
|
+
});
|
|
188
|
+
}, [emailTouched]);
|
|
189
|
+
|
|
190
|
+
const handleEmailBlur = useCallback(() => {
|
|
191
|
+
setEmailTouched(true);
|
|
192
|
+
// Validate email on blur
|
|
193
|
+
setErrors((previousErrors) => {
|
|
194
|
+
const updatedErrors: EmailVerificationFormErrors = { ...previousErrors };
|
|
195
|
+
const emailValue = values[EMAIL_VERIFICATION_FIELD_IDS.EMAIL];
|
|
196
|
+
const emailError = validateEmail(emailValue);
|
|
197
|
+
if (emailError) {
|
|
198
|
+
updatedErrors[EMAIL_VERIFICATION_FIELD_IDS.EMAIL] = emailError;
|
|
199
|
+
} else {
|
|
200
|
+
delete updatedErrors[EMAIL_VERIFICATION_FIELD_IDS.EMAIL];
|
|
201
|
+
}
|
|
202
|
+
return updatedErrors;
|
|
203
|
+
});
|
|
204
|
+
}, [values]);
|
|
205
|
+
|
|
206
|
+
const handleResendSubmit = useCallback(
|
|
207
|
+
async (event: React.FormEvent<HTMLFormElement>) => {
|
|
208
|
+
event.preventDefault();
|
|
209
|
+
|
|
210
|
+
// Final validation
|
|
211
|
+
const emailError = validateEmail(values[EMAIL_VERIFICATION_FIELD_IDS.EMAIL]);
|
|
212
|
+
|
|
213
|
+
if (emailError) {
|
|
214
|
+
setErrors({
|
|
215
|
+
[EMAIL_VERIFICATION_FIELD_IDS.EMAIL]: emailError,
|
|
216
|
+
});
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
setIsSubmitting(true);
|
|
221
|
+
setErrors({});
|
|
222
|
+
|
|
223
|
+
try {
|
|
224
|
+
const response = await fetch("/api/auth/resend_verification", {
|
|
225
|
+
method: "POST",
|
|
226
|
+
headers: {
|
|
227
|
+
"Content-Type": "application/json",
|
|
228
|
+
},
|
|
229
|
+
body: JSON.stringify({
|
|
230
|
+
email: values[EMAIL_VERIFICATION_FIELD_IDS.EMAIL],
|
|
231
|
+
}),
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
const data = await response.json();
|
|
235
|
+
|
|
236
|
+
if (!response.ok) {
|
|
237
|
+
throw new Error(data.error || "Failed to resend verification email");
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// Show success notification
|
|
241
|
+
toast.success("Verification email sent", {
|
|
242
|
+
description: data.message || "If an account with that email exists and is not verified, a verification link has been sent.",
|
|
243
|
+
});
|
|
244
|
+
} catch (error) {
|
|
245
|
+
const errorMessage =
|
|
246
|
+
error instanceof Error ? error.message : "Failed to resend verification email. Please try again.";
|
|
247
|
+
|
|
248
|
+
// Show error notification
|
|
249
|
+
toast.error("Failed to resend verification email", {
|
|
250
|
+
description: errorMessage,
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
// Set error state
|
|
254
|
+
setErrors({
|
|
255
|
+
submit: errorMessage,
|
|
256
|
+
});
|
|
257
|
+
} finally {
|
|
258
|
+
setIsSubmitting(false);
|
|
259
|
+
}
|
|
260
|
+
},
|
|
261
|
+
[values, dataClient],
|
|
262
|
+
);
|
|
263
|
+
|
|
264
|
+
const handleCancel = useCallback(() => {
|
|
265
|
+
router.push(loginPath);
|
|
266
|
+
}, [router, loginPath]);
|
|
267
|
+
|
|
268
|
+
const handleGoToLogin = useCallback(() => {
|
|
269
|
+
router.push(loginPath);
|
|
270
|
+
}, [router, loginPath]);
|
|
271
|
+
|
|
272
|
+
return {
|
|
273
|
+
isVerifying,
|
|
274
|
+
isVerified,
|
|
275
|
+
isError,
|
|
276
|
+
errorMessage,
|
|
277
|
+
email,
|
|
278
|
+
values,
|
|
279
|
+
errors,
|
|
280
|
+
isSubmitDisabled,
|
|
281
|
+
isSubmitting,
|
|
282
|
+
emailTouched,
|
|
283
|
+
redirectCountdown,
|
|
284
|
+
handleFieldChange,
|
|
285
|
+
handleEmailBlur,
|
|
286
|
+
handleResendSubmit,
|
|
287
|
+
handleCancel,
|
|
288
|
+
handleGoToLogin,
|
|
289
|
+
};
|
|
290
|
+
};
|
|
291
|
+
|