hazo_auth 1.0.5 → 1.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/README.md +50 -0
- package/hazo_auth_config.example.ini +41 -0
- package/instrumentation.ts +2 -2
- package/package.json +2 -1
- package/scripts/init_users.ts +378 -0
- package/src/app/api/hazo_auth/login/route.ts +27 -1
- package/src/app/api/hazo_auth/register/route.ts +13 -10
- package/src/app/hazo_auth/forgot_password/page.tsx +3 -3
- package/src/app/hazo_auth/login/login_page_client.tsx +15 -0
- package/src/app/hazo_auth/login/page.tsx +16 -4
- package/src/app/hazo_auth/my_settings/page.tsx +3 -3
- package/src/app/hazo_auth/register/page.tsx +14 -4
- package/src/app/hazo_auth/register/register_page_client.tsx +9 -0
- package/src/app/hazo_auth/reset_password/page.tsx +3 -3
- package/src/app/hazo_auth/user_management/page.tsx +3 -3
- package/src/app/hazo_auth/verify_email/page.tsx +3 -3
- package/src/components/layouts/login/hooks/use_login_form.ts +13 -8
- package/src/components/layouts/login/index.tsx +28 -0
- package/src/components/layouts/register/hooks/use_register_form.ts +4 -1
- package/src/components/layouts/register/index.tsx +18 -0
- package/src/components/layouts/shared/components/auth_page_shell.tsx +36 -0
- package/src/components/layouts/shared/components/standalone_layout_wrapper.tsx +53 -0
- package/src/lib/config/config_loader.server.ts +20 -5
- package/src/lib/login_config.server.ts +25 -0
- package/src/lib/register_config.server.ts +17 -1
- package/src/lib/services/login_service.ts +19 -3
- package/src/lib/services/registration_service.ts +25 -4
- package/src/lib/services/user_update_service.ts +16 -3
- package/src/lib/ui_shell_config.server.ts +73 -0
- package/src/lib/utils/error_sanitizer.ts +75 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// file_description: server-only helper to read register layout configuration from hazo_auth_config.ini
|
|
2
2
|
// section: imports
|
|
3
|
-
import { get_config_boolean, read_config_section } from "./config/config_loader.server";
|
|
3
|
+
import { get_config_boolean, get_config_value, read_config_section } from "./config/config_loader.server";
|
|
4
4
|
import { get_password_requirements_config } from "./password_requirements_config.server";
|
|
5
5
|
import { get_already_logged_in_config } from "./already_logged_in_config.server";
|
|
6
6
|
import { get_user_fields_config } from "./user_fields_config.server";
|
|
@@ -20,6 +20,8 @@ export type RegisterConfig = {
|
|
|
20
20
|
showReturnHomeButton: boolean;
|
|
21
21
|
returnHomeButtonLabel: string;
|
|
22
22
|
returnHomePath: string;
|
|
23
|
+
signInPath: string;
|
|
24
|
+
signInLabel: string;
|
|
23
25
|
};
|
|
24
26
|
|
|
25
27
|
// section: helpers
|
|
@@ -44,6 +46,18 @@ export function get_register_config(): RegisterConfig {
|
|
|
44
46
|
// Get shared already logged in config
|
|
45
47
|
const alreadyLoggedInConfig = get_already_logged_in_config();
|
|
46
48
|
|
|
49
|
+
// Read sign in link configuration
|
|
50
|
+
const signInPath = get_config_value(
|
|
51
|
+
"hazo_auth__register_layout",
|
|
52
|
+
"sign_in_path",
|
|
53
|
+
"/hazo_auth/login"
|
|
54
|
+
);
|
|
55
|
+
const signInLabel = get_config_value(
|
|
56
|
+
"hazo_auth__register_layout",
|
|
57
|
+
"sign_in_label",
|
|
58
|
+
"Sign in"
|
|
59
|
+
);
|
|
60
|
+
|
|
47
61
|
return {
|
|
48
62
|
showNameField,
|
|
49
63
|
passwordRequirements,
|
|
@@ -52,6 +66,8 @@ export function get_register_config(): RegisterConfig {
|
|
|
52
66
|
showReturnHomeButton: alreadyLoggedInConfig.showReturnHomeButton,
|
|
53
67
|
returnHomeButtonLabel: alreadyLoggedInConfig.returnHomeButtonLabel,
|
|
54
68
|
returnHomePath: alreadyLoggedInConfig.returnHomePath,
|
|
69
|
+
signInPath,
|
|
70
|
+
signInLabel,
|
|
55
71
|
};
|
|
56
72
|
}
|
|
57
73
|
|
|
@@ -3,6 +3,9 @@
|
|
|
3
3
|
import type { HazoConnectAdapter } from "hazo_connect";
|
|
4
4
|
import { createCrudService } from "hazo_connect/server";
|
|
5
5
|
import argon2 from "argon2";
|
|
6
|
+
import { create_app_logger } from "../app_logger";
|
|
7
|
+
import { sanitize_error_for_user } from "../utils/error_sanitizer";
|
|
8
|
+
import { get_filename, get_line_number } from "../utils/api_route_helpers";
|
|
6
9
|
|
|
7
10
|
// section: types
|
|
8
11
|
export type LoginData = {
|
|
@@ -15,6 +18,7 @@ export type LoginResult = {
|
|
|
15
18
|
user_id?: string;
|
|
16
19
|
error?: string;
|
|
17
20
|
email_not_verified?: boolean;
|
|
21
|
+
stored_url_on_logon?: string | null;
|
|
18
22
|
};
|
|
19
23
|
|
|
20
24
|
// section: helpers
|
|
@@ -98,20 +102,32 @@ export async function authenticate_user(
|
|
|
98
102
|
last_logon: now,
|
|
99
103
|
login_attempts: 0,
|
|
100
104
|
changed_at: now,
|
|
105
|
+
url_on_logon: null, // Clear the stored redirect URL after successful login
|
|
101
106
|
}
|
|
102
107
|
);
|
|
103
108
|
|
|
104
109
|
return {
|
|
105
110
|
success: true,
|
|
106
111
|
user_id: user.id as string,
|
|
112
|
+
stored_url_on_logon: user.url_on_logon as string | null | undefined,
|
|
107
113
|
};
|
|
108
114
|
} catch (error) {
|
|
109
|
-
const
|
|
110
|
-
|
|
115
|
+
const logger = create_app_logger();
|
|
116
|
+
const user_friendly_error = sanitize_error_for_user(error, {
|
|
117
|
+
logToConsole: true,
|
|
118
|
+
logToLogger: true,
|
|
119
|
+
logger,
|
|
120
|
+
context: {
|
|
121
|
+
filename: "login_service.ts",
|
|
122
|
+
line_number: get_line_number(),
|
|
123
|
+
email: data.email,
|
|
124
|
+
operation: "authenticate_user",
|
|
125
|
+
},
|
|
126
|
+
});
|
|
111
127
|
|
|
112
128
|
return {
|
|
113
129
|
success: false,
|
|
114
|
-
error:
|
|
130
|
+
error: user_friendly_error,
|
|
115
131
|
};
|
|
116
132
|
}
|
|
117
133
|
}
|
|
@@ -10,12 +10,15 @@ import { get_profile_picture_config } from "../profile_picture_config.server";
|
|
|
10
10
|
import { map_ui_source_to_db } from "./profile_picture_source_mapper";
|
|
11
11
|
import { create_app_logger } from "../app_logger";
|
|
12
12
|
import { send_template_email } from "./email_service";
|
|
13
|
+
import { sanitize_error_for_user } from "../utils/error_sanitizer";
|
|
14
|
+
import { get_filename, get_line_number } from "../utils/api_route_helpers";
|
|
13
15
|
|
|
14
16
|
// section: types
|
|
15
17
|
export type RegistrationData = {
|
|
16
18
|
email: string;
|
|
17
19
|
password: string;
|
|
18
20
|
name?: string;
|
|
21
|
+
url_on_logon?: string;
|
|
19
22
|
};
|
|
20
23
|
|
|
21
24
|
export type RegistrationResult = {
|
|
@@ -36,7 +39,7 @@ export async function register_user(
|
|
|
36
39
|
data: RegistrationData,
|
|
37
40
|
): Promise<RegistrationResult> {
|
|
38
41
|
try {
|
|
39
|
-
const { email, password, name } = data;
|
|
42
|
+
const { email, password, name, url_on_logon } = data;
|
|
40
43
|
|
|
41
44
|
// Create CRUD service for hazo_users table
|
|
42
45
|
const users_service = createCrudService(adapter, "hazo_users");
|
|
@@ -77,6 +80,14 @@ export async function register_user(
|
|
|
77
80
|
insert_data.name = name;
|
|
78
81
|
}
|
|
79
82
|
|
|
83
|
+
// Validate and include url_on_logon if provided
|
|
84
|
+
if (url_on_logon) {
|
|
85
|
+
// Ensure it's a relative path starting with / but not //
|
|
86
|
+
if (url_on_logon.startsWith("/") && !url_on_logon.startsWith("//")) {
|
|
87
|
+
insert_data.url_on_logon = url_on_logon;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
80
91
|
// Set default profile picture if enabled
|
|
81
92
|
const profile_picture_config = get_profile_picture_config();
|
|
82
93
|
if (profile_picture_config.user_photo_default) {
|
|
@@ -151,12 +162,22 @@ export async function register_user(
|
|
|
151
162
|
user_id,
|
|
152
163
|
};
|
|
153
164
|
} catch (error) {
|
|
154
|
-
const
|
|
155
|
-
|
|
165
|
+
const logger = create_app_logger();
|
|
166
|
+
const user_friendly_error = sanitize_error_for_user(error, {
|
|
167
|
+
logToConsole: true,
|
|
168
|
+
logToLogger: true,
|
|
169
|
+
logger,
|
|
170
|
+
context: {
|
|
171
|
+
filename: "registration_service.ts",
|
|
172
|
+
line_number: get_line_number(),
|
|
173
|
+
email: data.email,
|
|
174
|
+
operation: "register_user",
|
|
175
|
+
},
|
|
176
|
+
});
|
|
156
177
|
|
|
157
178
|
return {
|
|
158
179
|
success: false,
|
|
159
|
-
error:
|
|
180
|
+
error: user_friendly_error,
|
|
160
181
|
};
|
|
161
182
|
}
|
|
162
183
|
}
|
|
@@ -3,6 +3,9 @@
|
|
|
3
3
|
import type { HazoConnectAdapter } from "hazo_connect";
|
|
4
4
|
import { createCrudService } from "hazo_connect/server";
|
|
5
5
|
import { map_ui_source_to_db, type ProfilePictureSourceUI } from "./profile_picture_source_mapper";
|
|
6
|
+
import { create_app_logger } from "../app_logger";
|
|
7
|
+
import { sanitize_error_for_user } from "../utils/error_sanitizer";
|
|
8
|
+
import { get_filename, get_line_number } from "../utils/api_route_helpers";
|
|
6
9
|
|
|
7
10
|
// section: types
|
|
8
11
|
export type UserUpdateData = {
|
|
@@ -116,12 +119,22 @@ export async function update_user_profile(
|
|
|
116
119
|
email_changed,
|
|
117
120
|
};
|
|
118
121
|
} catch (error) {
|
|
119
|
-
const
|
|
120
|
-
|
|
122
|
+
const logger = create_app_logger();
|
|
123
|
+
const user_friendly_error = sanitize_error_for_user(error, {
|
|
124
|
+
logToConsole: true,
|
|
125
|
+
logToLogger: true,
|
|
126
|
+
logger,
|
|
127
|
+
context: {
|
|
128
|
+
filename: "user_update_service.ts",
|
|
129
|
+
line_number: get_line_number(),
|
|
130
|
+
user_id,
|
|
131
|
+
operation: "update_user_profile",
|
|
132
|
+
},
|
|
133
|
+
});
|
|
121
134
|
|
|
122
135
|
return {
|
|
123
136
|
success: false,
|
|
124
|
-
error:
|
|
137
|
+
error: user_friendly_error,
|
|
125
138
|
};
|
|
126
139
|
}
|
|
127
140
|
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// file_description: load ui shell layout settings from hazo_auth_config.ini
|
|
2
|
+
// section: imports
|
|
3
|
+
import { get_config_value } from "./config/config_loader.server";
|
|
4
|
+
|
|
5
|
+
// section: types
|
|
6
|
+
export type UiShellLayoutMode = "test_sidebar" | "standalone";
|
|
7
|
+
|
|
8
|
+
export type UiShellConfig = {
|
|
9
|
+
layout_mode: UiShellLayoutMode;
|
|
10
|
+
standalone_heading: string;
|
|
11
|
+
standalone_description: string;
|
|
12
|
+
standalone_wrapper_class: string;
|
|
13
|
+
standalone_content_class: string;
|
|
14
|
+
standalone_show_heading: boolean;
|
|
15
|
+
standalone_show_description: boolean;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
// section: helpers
|
|
19
|
+
/**
|
|
20
|
+
* Reads ui shell configuration controlling whether pages use the sidebar test shell
|
|
21
|
+
* or a clean standalone wrapper that inherits consumer project styling.
|
|
22
|
+
*/
|
|
23
|
+
export function get_ui_shell_config(): UiShellConfig {
|
|
24
|
+
const section = "hazo_auth__ui_shell";
|
|
25
|
+
|
|
26
|
+
const layoutModeValue = get_config_value(section, "layout_mode", "test_sidebar").toLowerCase();
|
|
27
|
+
const layout_mode: UiShellLayoutMode =
|
|
28
|
+
layoutModeValue === "standalone" ? "standalone" : "test_sidebar";
|
|
29
|
+
|
|
30
|
+
const standalone_heading = get_config_value(
|
|
31
|
+
section,
|
|
32
|
+
"standalone_heading",
|
|
33
|
+
"Welcome to hazo auth"
|
|
34
|
+
);
|
|
35
|
+
const standalone_description = get_config_value(
|
|
36
|
+
section,
|
|
37
|
+
"standalone_description",
|
|
38
|
+
"Reuse the packaged authentication flows while inheriting your existing app shell styles."
|
|
39
|
+
);
|
|
40
|
+
const standalone_wrapper_class = get_config_value(
|
|
41
|
+
section,
|
|
42
|
+
"standalone_wrapper_class",
|
|
43
|
+
"cls_standalone_shell flex min-h-screen w-full items-center justify-center bg-background px-4 py-10"
|
|
44
|
+
);
|
|
45
|
+
const standalone_content_class = get_config_value(
|
|
46
|
+
section,
|
|
47
|
+
"standalone_content_class",
|
|
48
|
+
"cls_standalone_shell_content w-full max-w-5xl shadow-xl rounded-2xl border bg-card"
|
|
49
|
+
);
|
|
50
|
+
const standalone_show_heading = get_config_value(
|
|
51
|
+
section,
|
|
52
|
+
"standalone_show_heading",
|
|
53
|
+
"true"
|
|
54
|
+
).toLowerCase() === "true";
|
|
55
|
+
const standalone_show_description = get_config_value(
|
|
56
|
+
section,
|
|
57
|
+
"standalone_show_description",
|
|
58
|
+
"true"
|
|
59
|
+
).toLowerCase() === "true";
|
|
60
|
+
|
|
61
|
+
return {
|
|
62
|
+
layout_mode,
|
|
63
|
+
standalone_heading,
|
|
64
|
+
standalone_description,
|
|
65
|
+
standalone_wrapper_class,
|
|
66
|
+
standalone_content_class,
|
|
67
|
+
standalone_show_heading,
|
|
68
|
+
standalone_show_description,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
// file_description: utility functions for sanitizing error messages for user display
|
|
2
|
+
// section: imports
|
|
3
|
+
import { create_app_logger } from "../app_logger";
|
|
4
|
+
|
|
5
|
+
// section: constants
|
|
6
|
+
const USER_FRIENDLY_ERROR_MESSAGE = "We are facing some issues in our system, please try again later.";
|
|
7
|
+
|
|
8
|
+
// section: types
|
|
9
|
+
export type ErrorSanitizationOptions = {
|
|
10
|
+
logToConsole?: boolean;
|
|
11
|
+
logToLogger?: boolean;
|
|
12
|
+
logger?: ReturnType<typeof create_app_logger>;
|
|
13
|
+
context?: Record<string, unknown>;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
// section: helpers
|
|
17
|
+
/**
|
|
18
|
+
* Sanitizes error messages for user display
|
|
19
|
+
* Replaces technical error messages with user-friendly ones
|
|
20
|
+
* @param error - The error object or message
|
|
21
|
+
* @param options - Options for logging and context
|
|
22
|
+
* @returns User-friendly error message
|
|
23
|
+
*/
|
|
24
|
+
export function sanitize_error_for_user(
|
|
25
|
+
error: unknown,
|
|
26
|
+
options: ErrorSanitizationOptions = {}
|
|
27
|
+
): string {
|
|
28
|
+
const { logToConsole = true, logToLogger = true, logger, context = {} } = options;
|
|
29
|
+
|
|
30
|
+
// Extract detailed error message
|
|
31
|
+
const detailed_error_message =
|
|
32
|
+
error instanceof Error ? error.message : String(error);
|
|
33
|
+
const error_stack = error instanceof Error ? error.stack : undefined;
|
|
34
|
+
|
|
35
|
+
// Log detailed error to console
|
|
36
|
+
if (logToConsole) {
|
|
37
|
+
console.error("Detailed error:", {
|
|
38
|
+
message: detailed_error_message,
|
|
39
|
+
stack: error_stack,
|
|
40
|
+
...context,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Log detailed error to logger if provided
|
|
45
|
+
if (logToLogger && logger) {
|
|
46
|
+
logger.error("error_occurred", {
|
|
47
|
+
filename: context.filename as string || "unknown",
|
|
48
|
+
line_number: context.line_number as number || 0,
|
|
49
|
+
error_message: detailed_error_message,
|
|
50
|
+
error_stack,
|
|
51
|
+
...context,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Check if error is a PostgREST or database-related error
|
|
56
|
+
const is_database_error =
|
|
57
|
+
detailed_error_message.includes("PostgREST") ||
|
|
58
|
+
detailed_error_message.includes("403 Forbidden") ||
|
|
59
|
+
detailed_error_message.includes("404 Not Found") ||
|
|
60
|
+
detailed_error_message.includes("500 Internal Server Error") ||
|
|
61
|
+
detailed_error_message.includes("database") ||
|
|
62
|
+
detailed_error_message.includes("connection") ||
|
|
63
|
+
detailed_error_message.includes("timeout");
|
|
64
|
+
|
|
65
|
+
// Return user-friendly message for database/API errors
|
|
66
|
+
if (is_database_error) {
|
|
67
|
+
return USER_FRIENDLY_ERROR_MESSAGE;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// For other errors, return the original message (could be user-friendly already)
|
|
71
|
+
// But still log the detailed error
|
|
72
|
+
return detailed_error_message;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
|