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,155 @@
|
|
|
1
|
+
// file_description: API route for user login authentication using hazo_connect
|
|
2
|
+
// section: imports
|
|
3
|
+
import { NextRequest, NextResponse } from "next/server";
|
|
4
|
+
import { get_hazo_connect_instance } from "@/lib/hazo_connect_instance.server";
|
|
5
|
+
import { create_app_logger } from "@/lib/app_logger";
|
|
6
|
+
import { authenticate_user } from "@/lib/services/login_service";
|
|
7
|
+
import { createCrudService } from "hazo_connect/server";
|
|
8
|
+
import { get_filename, get_line_number } from "@/lib/utils/api_route_helpers";
|
|
9
|
+
|
|
10
|
+
// section: api_handler
|
|
11
|
+
export async function POST(request: NextRequest) {
|
|
12
|
+
const logger = create_app_logger();
|
|
13
|
+
|
|
14
|
+
try {
|
|
15
|
+
const body = await request.json();
|
|
16
|
+
const { email, password } = body;
|
|
17
|
+
|
|
18
|
+
// Validate input
|
|
19
|
+
if (!email || !password) {
|
|
20
|
+
logger.warn("login_validation_failed", {
|
|
21
|
+
filename: get_filename(),
|
|
22
|
+
line_number: get_line_number(),
|
|
23
|
+
email: email || "missing",
|
|
24
|
+
has_password: !!password,
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
return NextResponse.json(
|
|
28
|
+
{ error: "Email and password are required" },
|
|
29
|
+
{ status: 400 }
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Validate email format
|
|
34
|
+
const email_pattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
35
|
+
if (!email_pattern.test(email)) {
|
|
36
|
+
logger.warn("login_invalid_email", {
|
|
37
|
+
filename: get_filename(),
|
|
38
|
+
line_number: get_line_number(),
|
|
39
|
+
email,
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
return NextResponse.json(
|
|
43
|
+
{ error: "Invalid email address format" },
|
|
44
|
+
{ status: 400 }
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Get singleton hazo_connect instance (reuses same connection across all routes)
|
|
49
|
+
const hazoConnect = get_hazo_connect_instance();
|
|
50
|
+
|
|
51
|
+
// Authenticate user using the login service
|
|
52
|
+
const result = await authenticate_user(hazoConnect, {
|
|
53
|
+
email,
|
|
54
|
+
password,
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
if (!result.success) {
|
|
58
|
+
const status_code = result.error === "Invalid email or password" ? 401 : 500;
|
|
59
|
+
|
|
60
|
+
logger.warn("login_failed", {
|
|
61
|
+
filename: get_filename(),
|
|
62
|
+
line_number: get_line_number(),
|
|
63
|
+
email,
|
|
64
|
+
error: result.error,
|
|
65
|
+
email_not_verified: result.email_not_verified || false,
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
return NextResponse.json(
|
|
69
|
+
{
|
|
70
|
+
error: result.error || "Login failed",
|
|
71
|
+
email_not_verified: result.email_not_verified || false,
|
|
72
|
+
},
|
|
73
|
+
{ status: status_code }
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// TypeScript assertion: user_id is guaranteed to be present when success is true
|
|
78
|
+
// However, we need to check it to satisfy TypeScript's type checking
|
|
79
|
+
if (!result.user_id) {
|
|
80
|
+
logger.error("login_user_id_missing", {
|
|
81
|
+
filename: get_filename(),
|
|
82
|
+
line_number: get_line_number(),
|
|
83
|
+
email,
|
|
84
|
+
note: "Login succeeded but user_id is missing - this should not happen",
|
|
85
|
+
});
|
|
86
|
+
return NextResponse.json(
|
|
87
|
+
{ error: "Login failed - user ID not found" },
|
|
88
|
+
{ status: 500 }
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const user_id = result.user_id;
|
|
93
|
+
|
|
94
|
+
logger.info("login_successful", {
|
|
95
|
+
filename: get_filename(),
|
|
96
|
+
line_number: get_line_number(),
|
|
97
|
+
user_id: user_id,
|
|
98
|
+
email,
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
// Reuse the existing hazoConnect instance from above
|
|
102
|
+
const users_service = createCrudService(hazoConnect, "hazo_users");
|
|
103
|
+
const users = await users_service.findBy({
|
|
104
|
+
id: user_id,
|
|
105
|
+
});
|
|
106
|
+
const user = users && users.length > 0 ? users[0] : null;
|
|
107
|
+
const user_name = user?.name as string | undefined;
|
|
108
|
+
|
|
109
|
+
// Create response with cookies
|
|
110
|
+
const response = NextResponse.json(
|
|
111
|
+
{
|
|
112
|
+
success: true,
|
|
113
|
+
message: "Login successful",
|
|
114
|
+
user_id: user_id,
|
|
115
|
+
email,
|
|
116
|
+
name: user_name,
|
|
117
|
+
},
|
|
118
|
+
{ status: 200 }
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
// Set authentication cookies
|
|
122
|
+
response.cookies.set("hazo_auth_user_id", user_id, {
|
|
123
|
+
httpOnly: true,
|
|
124
|
+
secure: process.env.NODE_ENV === "production",
|
|
125
|
+
sameSite: "lax",
|
|
126
|
+
path: "/",
|
|
127
|
+
maxAge: 60 * 60 * 24 * 30, // 30 days
|
|
128
|
+
});
|
|
129
|
+
response.cookies.set("hazo_auth_user_email", email, {
|
|
130
|
+
httpOnly: true,
|
|
131
|
+
secure: process.env.NODE_ENV === "production",
|
|
132
|
+
sameSite: "lax",
|
|
133
|
+
path: "/",
|
|
134
|
+
maxAge: 60 * 60 * 24 * 30, // 30 days
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
return response;
|
|
138
|
+
} catch (error) {
|
|
139
|
+
const error_message = error instanceof Error ? error.message : "Unknown error";
|
|
140
|
+
const error_stack = error instanceof Error ? error.stack : undefined;
|
|
141
|
+
|
|
142
|
+
logger.error("login_error", {
|
|
143
|
+
filename: get_filename(),
|
|
144
|
+
line_number: get_line_number(),
|
|
145
|
+
error_message,
|
|
146
|
+
error_stack,
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
return NextResponse.json(
|
|
150
|
+
{ error: "Login failed. Please try again." },
|
|
151
|
+
{ status: 500 }
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// file_description: API route for user logout
|
|
2
|
+
// section: imports
|
|
3
|
+
import { NextRequest, NextResponse } from "next/server";
|
|
4
|
+
import { create_app_logger } from "@/lib/app_logger";
|
|
5
|
+
import { get_filename, get_line_number } from "@/lib/utils/api_route_helpers";
|
|
6
|
+
|
|
7
|
+
// section: api_handler
|
|
8
|
+
export async function POST(request: NextRequest) {
|
|
9
|
+
const logger = create_app_logger();
|
|
10
|
+
|
|
11
|
+
try {
|
|
12
|
+
// Get user info from cookie before clearing
|
|
13
|
+
const user_email = request.cookies.get("hazo_auth_user_email")?.value;
|
|
14
|
+
const user_id = request.cookies.get("hazo_auth_user_id")?.value;
|
|
15
|
+
|
|
16
|
+
// Clear authentication cookies
|
|
17
|
+
const response = NextResponse.json(
|
|
18
|
+
{
|
|
19
|
+
success: true,
|
|
20
|
+
message: "Logout successful",
|
|
21
|
+
},
|
|
22
|
+
{ status: 200 }
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
// Clear cookies by setting them to expire in the past
|
|
26
|
+
response.cookies.set("hazo_auth_user_email", "", {
|
|
27
|
+
expires: new Date(0),
|
|
28
|
+
path: "/",
|
|
29
|
+
});
|
|
30
|
+
response.cookies.set("hazo_auth_user_id", "", {
|
|
31
|
+
expires: new Date(0),
|
|
32
|
+
path: "/",
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
if (user_email || user_id) {
|
|
36
|
+
logger.info("logout_successful", {
|
|
37
|
+
filename: get_filename(),
|
|
38
|
+
line_number: get_line_number(),
|
|
39
|
+
user_id: user_id || "unknown",
|
|
40
|
+
email: user_email || "unknown",
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return response;
|
|
45
|
+
} catch (error) {
|
|
46
|
+
const error_message = error instanceof Error ? error.message : "Unknown error";
|
|
47
|
+
const error_stack = error instanceof Error ? error.stack : undefined;
|
|
48
|
+
|
|
49
|
+
logger.error("logout_error", {
|
|
50
|
+
filename: get_filename(),
|
|
51
|
+
line_number: get_line_number(),
|
|
52
|
+
error_message,
|
|
53
|
+
error_stack,
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
return NextResponse.json(
|
|
57
|
+
{ error: "Logout failed. Please try again." },
|
|
58
|
+
{ status: 500 }
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// file_description: API route to get current authenticated user information
|
|
2
|
+
// section: imports
|
|
3
|
+
import { NextRequest, NextResponse } from "next/server";
|
|
4
|
+
import { get_authenticated_user_with_response } from "@/lib/auth/auth_utils.server";
|
|
5
|
+
|
|
6
|
+
// section: api_handler
|
|
7
|
+
export async function GET(request: NextRequest) {
|
|
8
|
+
try {
|
|
9
|
+
// Use centralized auth utility
|
|
10
|
+
const { auth_result, response } = await get_authenticated_user_with_response(request);
|
|
11
|
+
|
|
12
|
+
// If response is provided, it means cookies were cleared (invalid auth)
|
|
13
|
+
if (response) {
|
|
14
|
+
return response;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// If not authenticated, return false
|
|
18
|
+
if (!auth_result.authenticated) {
|
|
19
|
+
return NextResponse.json(
|
|
20
|
+
{ authenticated: false },
|
|
21
|
+
{ status: 200 }
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Return user info
|
|
26
|
+
return NextResponse.json(
|
|
27
|
+
{
|
|
28
|
+
authenticated: true,
|
|
29
|
+
user_id: auth_result.user_id,
|
|
30
|
+
email: auth_result.email,
|
|
31
|
+
name: auth_result.name,
|
|
32
|
+
email_verified: auth_result.email_verified,
|
|
33
|
+
last_logon: auth_result.last_logon,
|
|
34
|
+
profile_picture_url: auth_result.profile_picture_url,
|
|
35
|
+
profile_source: auth_result.profile_source,
|
|
36
|
+
},
|
|
37
|
+
{ status: 200 }
|
|
38
|
+
);
|
|
39
|
+
} catch (error) {
|
|
40
|
+
// On error, assume not authenticated
|
|
41
|
+
return NextResponse.json(
|
|
42
|
+
{ authenticated: false },
|
|
43
|
+
{ status: 200 }
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// file_description: API route to serve uploaded profile pictures
|
|
2
|
+
// section: imports
|
|
3
|
+
import { NextRequest, NextResponse } from "next/server";
|
|
4
|
+
import { get_profile_picture_config } from "@/lib/profile_picture_config.server";
|
|
5
|
+
import fs from "fs";
|
|
6
|
+
import path from "path";
|
|
7
|
+
|
|
8
|
+
// section: api_handler
|
|
9
|
+
export async function GET(
|
|
10
|
+
request: NextRequest,
|
|
11
|
+
{ params }: { params: { filename: string } }
|
|
12
|
+
) {
|
|
13
|
+
try {
|
|
14
|
+
const config = get_profile_picture_config();
|
|
15
|
+
|
|
16
|
+
if (!config.allow_photo_upload || !config.upload_photo_path) {
|
|
17
|
+
return NextResponse.json(
|
|
18
|
+
{ error: "Profile picture upload is not enabled" },
|
|
19
|
+
{ status: 404 }
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const filename = params.filename;
|
|
24
|
+
|
|
25
|
+
// Validate filename (prevent path traversal)
|
|
26
|
+
if (filename.includes("..") || filename.includes("/") || filename.includes("\\")) {
|
|
27
|
+
return NextResponse.json(
|
|
28
|
+
{ error: "Invalid filename" },
|
|
29
|
+
{ status: 400 }
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Resolve upload path
|
|
34
|
+
const uploadPath = path.isAbsolute(config.upload_photo_path)
|
|
35
|
+
? config.upload_photo_path
|
|
36
|
+
: path.resolve(process.cwd(), config.upload_photo_path);
|
|
37
|
+
|
|
38
|
+
const filePath = path.join(uploadPath, filename);
|
|
39
|
+
|
|
40
|
+
// Check if file exists
|
|
41
|
+
if (!fs.existsSync(filePath)) {
|
|
42
|
+
return NextResponse.json(
|
|
43
|
+
{ error: "File not found" },
|
|
44
|
+
{ status: 404 }
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Read file
|
|
49
|
+
const fileBuffer = fs.readFileSync(filePath);
|
|
50
|
+
const fileExt = path.extname(filename).toLowerCase();
|
|
51
|
+
const contentType = fileExt === ".png" ? "image/png" : "image/jpeg";
|
|
52
|
+
|
|
53
|
+
// Return file with appropriate content type
|
|
54
|
+
return new NextResponse(fileBuffer, {
|
|
55
|
+
headers: {
|
|
56
|
+
"Content-Type": contentType,
|
|
57
|
+
"Cache-Control": "public, max-age=31536000, immutable",
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
} catch (error) {
|
|
61
|
+
return NextResponse.json(
|
|
62
|
+
{ error: "Failed to serve profile picture" },
|
|
63
|
+
{ status: 500 }
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
// file_description: API route for user registration using hazo_connect to insert into hazo_users table
|
|
2
|
+
// section: imports
|
|
3
|
+
import { NextRequest, NextResponse } from "next/server";
|
|
4
|
+
import { get_hazo_connect_instance } from "@/lib/hazo_connect_instance.server";
|
|
5
|
+
import { create_app_logger } from "@/lib/app_logger";
|
|
6
|
+
import { register_user } from "@/lib/services/registration_service";
|
|
7
|
+
import { get_filename, get_line_number } from "@/lib/utils/api_route_helpers";
|
|
8
|
+
|
|
9
|
+
// section: api_handler
|
|
10
|
+
export async function POST(request: NextRequest) {
|
|
11
|
+
const logger = create_app_logger();
|
|
12
|
+
|
|
13
|
+
try {
|
|
14
|
+
const body = await request.json();
|
|
15
|
+
const { name, email, password } = body;
|
|
16
|
+
|
|
17
|
+
// Validate input
|
|
18
|
+
if (!email || !password) {
|
|
19
|
+
logger.warn("registration_validation_failed", {
|
|
20
|
+
filename: get_filename(),
|
|
21
|
+
line_number: get_line_number(),
|
|
22
|
+
email: email || "missing",
|
|
23
|
+
has_password: !!password,
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
return NextResponse.json(
|
|
27
|
+
{ error: "Email and password are required" },
|
|
28
|
+
{ status: 400 }
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Validate email format
|
|
33
|
+
const email_pattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
34
|
+
if (!email_pattern.test(email)) {
|
|
35
|
+
logger.warn("registration_invalid_email", {
|
|
36
|
+
filename: get_filename(),
|
|
37
|
+
line_number: get_line_number(),
|
|
38
|
+
email,
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
return NextResponse.json(
|
|
42
|
+
{ error: "Invalid email address format" },
|
|
43
|
+
{ status: 400 }
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Get singleton hazo_connect instance (reuses same connection across all routes)
|
|
48
|
+
const hazoConnect = get_hazo_connect_instance();
|
|
49
|
+
|
|
50
|
+
// Register user using the registration service
|
|
51
|
+
const result = await register_user(hazoConnect, {
|
|
52
|
+
email,
|
|
53
|
+
password,
|
|
54
|
+
name,
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
if (!result.success) {
|
|
58
|
+
const status_code = result.error === "Email address already registered" ? 409 : 500;
|
|
59
|
+
|
|
60
|
+
logger.warn("registration_failed", {
|
|
61
|
+
filename: get_filename(),
|
|
62
|
+
line_number: get_line_number(),
|
|
63
|
+
email,
|
|
64
|
+
error: result.error,
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
return NextResponse.json(
|
|
68
|
+
{ error: result.error || "Registration failed" },
|
|
69
|
+
{ status: status_code }
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
logger.info("registration_successful", {
|
|
74
|
+
filename: get_filename(),
|
|
75
|
+
line_number: get_line_number(),
|
|
76
|
+
user_id: result.user_id,
|
|
77
|
+
email,
|
|
78
|
+
has_name: !!name,
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
return NextResponse.json(
|
|
82
|
+
{
|
|
83
|
+
success: true,
|
|
84
|
+
message: "Registration successful",
|
|
85
|
+
user_id: result.user_id,
|
|
86
|
+
},
|
|
87
|
+
{ status: 201 }
|
|
88
|
+
);
|
|
89
|
+
} catch (error) {
|
|
90
|
+
const error_message = error instanceof Error ? error.message : "Unknown error";
|
|
91
|
+
const error_stack = error instanceof Error ? error.stack : undefined;
|
|
92
|
+
|
|
93
|
+
logger.error("registration_error", {
|
|
94
|
+
filename: get_filename(),
|
|
95
|
+
line_number: get_line_number(),
|
|
96
|
+
error_message,
|
|
97
|
+
error_stack,
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
return NextResponse.json(
|
|
101
|
+
{ error: "Registration failed. Please try again." },
|
|
102
|
+
{ status: 500 }
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// file_description: API route for removing profile pictures
|
|
2
|
+
// section: imports
|
|
3
|
+
import { NextRequest, NextResponse } from "next/server";
|
|
4
|
+
import { get_hazo_connect_instance } from "@/lib/hazo_connect_instance.server";
|
|
5
|
+
import { create_app_logger } from "@/lib/app_logger";
|
|
6
|
+
import { remove_user_profile_picture } from "@/lib/services/profile_picture_remove_service";
|
|
7
|
+
import { get_filename, get_line_number } from "@/lib/utils/api_route_helpers";
|
|
8
|
+
|
|
9
|
+
// section: api_handler
|
|
10
|
+
export async function DELETE(request: NextRequest) {
|
|
11
|
+
const logger = create_app_logger();
|
|
12
|
+
|
|
13
|
+
try {
|
|
14
|
+
// Use centralized auth check
|
|
15
|
+
let user_id: string;
|
|
16
|
+
try {
|
|
17
|
+
const { require_auth } = await import("@/lib/auth/auth_utils.server");
|
|
18
|
+
const user = await require_auth(request);
|
|
19
|
+
user_id = user.user_id;
|
|
20
|
+
} catch (error) {
|
|
21
|
+
if (error instanceof Error && error.message === "Authentication required") {
|
|
22
|
+
logger.warn("profile_picture_remove_authentication_failed", {
|
|
23
|
+
filename: get_filename(),
|
|
24
|
+
line_number: get_line_number(),
|
|
25
|
+
error: "User not authenticated",
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
return NextResponse.json(
|
|
29
|
+
{ error: "Authentication required" },
|
|
30
|
+
{ status: 401 }
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
throw error;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Get singleton hazo_connect instance
|
|
37
|
+
const hazoConnect = get_hazo_connect_instance();
|
|
38
|
+
|
|
39
|
+
// Remove profile picture
|
|
40
|
+
const result = await remove_user_profile_picture(hazoConnect, user_id);
|
|
41
|
+
|
|
42
|
+
if (!result.success) {
|
|
43
|
+
logger.warn("profile_picture_remove_failed", {
|
|
44
|
+
filename: get_filename(),
|
|
45
|
+
line_number: get_line_number(),
|
|
46
|
+
user_id,
|
|
47
|
+
error: result.error,
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
return NextResponse.json(
|
|
51
|
+
{ error: result.error || "Failed to remove profile picture" },
|
|
52
|
+
{ status: 400 }
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
logger.info("profile_picture_remove_successful", {
|
|
57
|
+
filename: get_filename(),
|
|
58
|
+
line_number: get_line_number(),
|
|
59
|
+
user_id,
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
return NextResponse.json(
|
|
63
|
+
{
|
|
64
|
+
success: true,
|
|
65
|
+
message: "Profile picture removed successfully",
|
|
66
|
+
},
|
|
67
|
+
{ status: 200 }
|
|
68
|
+
);
|
|
69
|
+
} catch (error) {
|
|
70
|
+
const error_message = error instanceof Error ? error.message : "Unknown error";
|
|
71
|
+
const error_stack = error instanceof Error ? error.stack : undefined;
|
|
72
|
+
|
|
73
|
+
logger.error("profile_picture_remove_error", {
|
|
74
|
+
filename: get_filename(),
|
|
75
|
+
line_number: get_line_number(),
|
|
76
|
+
error_message,
|
|
77
|
+
error_stack,
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
return NextResponse.json(
|
|
81
|
+
{ error: "Failed to remove profile picture. Please try again." },
|
|
82
|
+
{ status: 500 }
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
// file_description: API route for resending email verification using hazo_connect
|
|
2
|
+
// section: imports
|
|
3
|
+
import { NextRequest, NextResponse } from "next/server";
|
|
4
|
+
import { get_hazo_connect_instance } from "@/lib/hazo_connect_instance.server";
|
|
5
|
+
import { create_app_logger } from "@/lib/app_logger";
|
|
6
|
+
import { resend_verification_email } from "@/lib/services/email_verification_service";
|
|
7
|
+
import { get_filename, get_line_number } from "@/lib/utils/api_route_helpers";
|
|
8
|
+
|
|
9
|
+
// section: api_handler
|
|
10
|
+
export async function POST(request: NextRequest) {
|
|
11
|
+
const logger = create_app_logger();
|
|
12
|
+
|
|
13
|
+
try {
|
|
14
|
+
const body = await request.json();
|
|
15
|
+
const { email } = body;
|
|
16
|
+
|
|
17
|
+
// Validate input
|
|
18
|
+
if (!email) {
|
|
19
|
+
logger.warn("resend_verification_validation_failed", {
|
|
20
|
+
filename: get_filename(),
|
|
21
|
+
line_number: get_line_number(),
|
|
22
|
+
email: email || "missing",
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
return NextResponse.json(
|
|
26
|
+
{ error: "Email is required" },
|
|
27
|
+
{ status: 400 }
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Validate email format
|
|
32
|
+
const email_pattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
33
|
+
if (!email_pattern.test(email)) {
|
|
34
|
+
logger.warn("resend_verification_invalid_email", {
|
|
35
|
+
filename: get_filename(),
|
|
36
|
+
line_number: get_line_number(),
|
|
37
|
+
email,
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
return NextResponse.json(
|
|
41
|
+
{ error: "Invalid email address format" },
|
|
42
|
+
{ status: 400 }
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Get singleton hazo_connect instance (reuses same connection across all routes)
|
|
47
|
+
const hazoConnect = get_hazo_connect_instance();
|
|
48
|
+
|
|
49
|
+
// Resend verification email using the email verification service
|
|
50
|
+
const result = await resend_verification_email(hazoConnect, {
|
|
51
|
+
email,
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
if (!result.success) {
|
|
55
|
+
logger.warn("resend_verification_failed", {
|
|
56
|
+
filename: get_filename(),
|
|
57
|
+
line_number: get_line_number(),
|
|
58
|
+
email,
|
|
59
|
+
error: result.error,
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
// Still return 200 OK to prevent email enumeration attacks
|
|
63
|
+
return NextResponse.json(
|
|
64
|
+
{
|
|
65
|
+
success: true,
|
|
66
|
+
message: "If an account with that email exists and is not verified, a verification link has been sent.",
|
|
67
|
+
},
|
|
68
|
+
{ status: 200 }
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
logger.info("resend_verification_requested", {
|
|
73
|
+
filename: get_filename(),
|
|
74
|
+
line_number: get_line_number(),
|
|
75
|
+
email,
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
// Always return success to prevent email enumeration attacks
|
|
79
|
+
return NextResponse.json(
|
|
80
|
+
{
|
|
81
|
+
success: true,
|
|
82
|
+
message: "If an account with that email exists and is not verified, a verification link has been sent.",
|
|
83
|
+
},
|
|
84
|
+
{ status: 200 }
|
|
85
|
+
);
|
|
86
|
+
} catch (error) {
|
|
87
|
+
const error_message = error instanceof Error ? error.message : "Unknown error";
|
|
88
|
+
const error_stack = error instanceof Error ? error.stack : undefined;
|
|
89
|
+
|
|
90
|
+
logger.error("resend_verification_error", {
|
|
91
|
+
filename: get_filename(),
|
|
92
|
+
line_number: get_line_number(),
|
|
93
|
+
error_message,
|
|
94
|
+
error_stack,
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
// Still return 200 OK to prevent email enumeration attacks
|
|
98
|
+
return NextResponse.json(
|
|
99
|
+
{
|
|
100
|
+
success: true,
|
|
101
|
+
message: "If an account with that email exists and is not verified, a verification link has been sent.",
|
|
102
|
+
},
|
|
103
|
+
{ status: 200 }
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|