hazo_auth 1.0.5 → 1.2.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 (31) hide show
  1. package/README.md +341 -0
  2. package/hazo_auth_config.example.ini +41 -0
  3. package/instrumentation.ts +2 -2
  4. package/package.json +2 -1
  5. package/scripts/init_users.ts +378 -0
  6. package/src/app/api/hazo_auth/login/route.ts +27 -1
  7. package/src/app/api/hazo_auth/register/route.ts +13 -10
  8. package/src/app/hazo_auth/forgot_password/page.tsx +3 -3
  9. package/src/app/hazo_auth/login/login_page_client.tsx +15 -0
  10. package/src/app/hazo_auth/login/page.tsx +16 -4
  11. package/src/app/hazo_auth/my_settings/page.tsx +3 -3
  12. package/src/app/hazo_auth/register/page.tsx +14 -4
  13. package/src/app/hazo_auth/register/register_page_client.tsx +9 -0
  14. package/src/app/hazo_auth/reset_password/page.tsx +3 -3
  15. package/src/app/hazo_auth/user_management/page.tsx +3 -3
  16. package/src/app/hazo_auth/verify_email/page.tsx +3 -3
  17. package/src/components/layouts/login/hooks/use_login_form.ts +13 -8
  18. package/src/components/layouts/login/index.tsx +28 -0
  19. package/src/components/layouts/register/hooks/use_register_form.ts +4 -1
  20. package/src/components/layouts/register/index.tsx +18 -0
  21. package/src/components/layouts/shared/components/auth_page_shell.tsx +36 -0
  22. package/src/components/layouts/shared/components/standalone_layout_wrapper.tsx +53 -0
  23. package/src/lib/config/config_loader.server.ts +20 -5
  24. package/src/lib/login_config.server.ts +25 -0
  25. package/src/lib/register_config.server.ts +17 -1
  26. package/src/lib/services/login_service.ts +19 -3
  27. package/src/lib/services/registration_service.ts +25 -4
  28. package/src/lib/services/user_profiles_service.ts +143 -0
  29. package/src/lib/services/user_update_service.ts +16 -3
  30. package/src/lib/ui_shell_config.server.ts +73 -0
  31. package/src/lib/utils/error_sanitizer.ts +75 -0
@@ -0,0 +1,143 @@
1
+ // file_description: service for batch retrieval of basic user profile information for chat applications and similar use cases
2
+ // section: imports
3
+ import type { HazoConnectAdapter } from "hazo_connect";
4
+ import { createCrudService } from "hazo_connect/server";
5
+ import { differenceInDays } from "date-fns";
6
+ import { create_app_logger } from "../app_logger";
7
+ import { sanitize_error_for_user } from "../utils/error_sanitizer";
8
+
9
+ // section: types
10
+ /**
11
+ * Basic user profile information returned by get_profiles
12
+ * Contains resolved profile picture URL, email, name, and account age
13
+ */
14
+ export type UserProfileInfo = {
15
+ user_id: string;
16
+ profile_picture_url: string | null;
17
+ email: string;
18
+ name: string | null;
19
+ days_since_created: number;
20
+ };
21
+
22
+ /**
23
+ * Result type for get_profiles function
24
+ * Includes found profiles and list of IDs that were not found
25
+ */
26
+ export type GetProfilesResult = {
27
+ success: boolean;
28
+ profiles: UserProfileInfo[];
29
+ not_found_ids: string[];
30
+ error?: string;
31
+ };
32
+
33
+ // section: helpers
34
+ /**
35
+ * Retrieves basic profile information for multiple users in a single batch call
36
+ * Useful for chat applications and similar use cases where basic user info is needed
37
+ * @param adapter - The hazo_connect adapter instance
38
+ * @param user_ids - Array of user IDs to retrieve profiles for
39
+ * @returns GetProfilesResult with found profiles and list of not found IDs
40
+ */
41
+ export async function hazo_get_user_profiles(
42
+ adapter: HazoConnectAdapter,
43
+ user_ids: string[],
44
+ ): Promise<GetProfilesResult> {
45
+ const logger = create_app_logger();
46
+
47
+ try {
48
+ // Handle empty input
49
+ if (!user_ids || user_ids.length === 0) {
50
+ return {
51
+ success: true,
52
+ profiles: [],
53
+ not_found_ids: [],
54
+ };
55
+ }
56
+
57
+ // Remove duplicates from input
58
+ const unique_user_ids = [...new Set(user_ids)];
59
+
60
+ // Create CRUD service for hazo_users table
61
+ const users_service = createCrudService(adapter, "hazo_users");
62
+
63
+ // Query users by IDs using the 'in' filter
64
+ // PostgREST supports 'in' filter syntax: id=in.(id1,id2,id3)
65
+ const users = await users_service.findBy({
66
+ id: `in.(${unique_user_ids.join(",")})`,
67
+ });
68
+
69
+ // Handle case where no users are found
70
+ if (!Array.isArray(users)) {
71
+ logger.warn("hazo_get_user_profiles_unexpected_response", {
72
+ filename: "user_profiles_service.ts",
73
+ line_number: 70,
74
+ message: "Unexpected response format from database query",
75
+ user_ids: unique_user_ids,
76
+ });
77
+
78
+ return {
79
+ success: true,
80
+ profiles: [],
81
+ not_found_ids: unique_user_ids,
82
+ };
83
+ }
84
+
85
+ // Build set of found user IDs for quick lookup
86
+ const found_user_ids = new Set(users.map((user) => user.id as string));
87
+
88
+ // Determine which user IDs were not found
89
+ const not_found_ids = unique_user_ids.filter((id) => !found_user_ids.has(id));
90
+
91
+ // Transform database records to UserProfileInfo
92
+ const now = new Date();
93
+ const profiles: UserProfileInfo[] = users.map((user) => {
94
+ const created_at = user.created_at as string;
95
+ const created_date = new Date(created_at);
96
+ const days_since_created = differenceInDays(now, created_date);
97
+
98
+ return {
99
+ user_id: user.id as string,
100
+ profile_picture_url: (user.profile_picture_url as string) || null,
101
+ email: user.email_address as string,
102
+ name: (user.name as string) || null,
103
+ days_since_created,
104
+ };
105
+ });
106
+
107
+ // Log successful retrieval
108
+ logger.info("hazo_get_user_profiles_success", {
109
+ filename: "user_profiles_service.ts",
110
+ line_number: 105,
111
+ message: "Successfully retrieved user profiles",
112
+ requested_count: unique_user_ids.length,
113
+ found_count: profiles.length,
114
+ not_found_count: not_found_ids.length,
115
+ });
116
+
117
+ return {
118
+ success: true,
119
+ profiles,
120
+ not_found_ids,
121
+ };
122
+ } catch (error) {
123
+ const user_friendly_error = sanitize_error_for_user(error, {
124
+ logToConsole: true,
125
+ logToLogger: true,
126
+ logger,
127
+ context: {
128
+ filename: "user_profiles_service.ts",
129
+ line_number: 122,
130
+ operation: "hazo_get_user_profiles",
131
+ user_ids_count: user_ids?.length || 0,
132
+ },
133
+ });
134
+
135
+ return {
136
+ success: false,
137
+ profiles: [],
138
+ not_found_ids: [],
139
+ error: user_friendly_error,
140
+ };
141
+ }
142
+ }
143
+
@@ -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 error_message =
120
- error instanceof Error ? error.message : "Unknown error";
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: error_message,
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
+