hazo_auth 0.3.0 → 1.0.1
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 +628 -1
- package/hazo_auth_config.example.ini +39 -0
- package/instrumentation.ts +1 -1
- package/next.config.mjs +1 -1
- package/package.json +3 -1
- package/src/app/api/{auth → hazo_auth/auth}/upload_profile_picture/route.ts +2 -2
- package/src/app/api/{auth → hazo_auth}/change_password/route.ts +23 -0
- package/src/app/api/hazo_auth/get_auth/route.ts +89 -0
- package/src/app/api/hazo_auth/invalidate_cache/route.ts +139 -0
- package/src/app/api/{auth → hazo_auth}/logout/route.ts +27 -0
- package/src/app/api/hazo_auth/upload_profile_picture/route.ts +268 -0
- package/src/app/api/hazo_auth/user_management/permissions/route.ts +367 -0
- package/src/app/api/hazo_auth/user_management/roles/route.ts +442 -0
- package/src/app/api/hazo_auth/user_management/users/roles/route.ts +367 -0
- package/src/app/api/hazo_auth/user_management/users/route.ts +239 -0
- package/src/app/api/{auth → hazo_auth}/validate_reset_token/route.ts +3 -0
- package/src/app/api/{auth → hazo_auth}/verify_email/route.ts +3 -0
- package/src/app/globals.css +1 -1
- package/src/app/hazo_auth/user_management/page.tsx +14 -0
- package/src/app/hazo_auth/user_management/user_management_page_client.tsx +16 -0
- package/src/app/hazo_connect/api/sqlite/data/route.ts +7 -1
- package/src/app/hazo_connect/api/sqlite/schema/route.ts +14 -4
- package/src/app/hazo_connect/api/sqlite/tables/route.ts +14 -4
- package/src/app/hazo_connect/sqlite_admin/sqlite-admin-client.tsx +40 -3
- package/src/app/layout.tsx +1 -1
- package/src/app/page.tsx +4 -4
- package/src/components/layouts/email_verification/hooks/use_email_verification.ts +4 -4
- package/src/components/layouts/email_verification/index.tsx +1 -1
- package/src/components/layouts/forgot_password/hooks/use_forgot_password_form.ts +1 -1
- package/src/components/layouts/login/hooks/use_login_form.ts +2 -2
- package/src/components/layouts/my_settings/components/profile_picture_dialog.tsx +1 -1
- package/src/components/layouts/my_settings/components/profile_picture_library_tab.tsx +2 -2
- package/src/components/layouts/my_settings/hooks/use_my_settings.ts +5 -5
- package/src/components/layouts/my_settings/index.tsx +1 -1
- package/src/components/layouts/register/hooks/use_register_form.ts +1 -1
- package/src/components/layouts/reset_password/hooks/use_reset_password_form.ts +3 -3
- package/src/components/layouts/reset_password/index.tsx +2 -2
- package/src/components/layouts/shared/components/logout_button.tsx +1 -1
- package/src/components/layouts/shared/components/profile_pic_menu.tsx +4 -4
- package/src/components/layouts/shared/components/sidebar_layout_wrapper.tsx +19 -7
- package/src/components/layouts/shared/components/unauthorized_guard.tsx +1 -1
- package/src/components/layouts/shared/hooks/use_auth_status.ts +1 -1
- package/src/components/layouts/shared/hooks/use_hazo_auth.ts +158 -0
- package/src/components/layouts/user_management/components/roles_matrix.tsx +607 -0
- package/src/components/layouts/user_management/index.tsx +1295 -0
- package/src/components/ui/alert-dialog.tsx +141 -0
- package/src/components/ui/checkbox.tsx +30 -0
- package/src/components/ui/table.tsx +120 -0
- package/src/lib/auth/auth_cache.ts +220 -0
- package/src/lib/auth/auth_rate_limiter.ts +121 -0
- package/src/lib/auth/auth_types.ts +65 -0
- package/src/lib/auth/hazo_get_auth.server.ts +333 -0
- package/src/lib/auth_utility_config.server.ts +136 -0
- package/src/lib/hazo_connect_setup.server.ts +2 -3
- package/src/lib/my_settings_config.server.ts +1 -1
- package/src/lib/profile_pic_menu_config.server.ts +4 -4
- package/src/lib/reset_password_config.server.ts +5 -5
- package/src/lib/services/email_service.ts +2 -2
- package/src/lib/services/profile_picture_remove_service.ts +1 -1
- package/src/lib/services/token_service.ts +2 -2
- package/src/lib/user_management_config.server.ts +40 -0
- package/src/lib/utils.ts +1 -1
- package/src/middleware.ts +15 -13
- package/src/server/types/express.d.ts +1 -0
- package/src/stories/project_overview.stories.tsx +1 -1
- package/tailwind.config.ts +1 -1
- /package/src/app/api/{auth → hazo_auth}/forgot_password/route.ts +0 -0
- /package/src/app/api/{auth → hazo_auth}/library_photos/route.ts +0 -0
- /package/src/app/api/{auth → hazo_auth}/login/route.ts +0 -0
- /package/src/app/api/{auth → hazo_auth}/me/route.ts +0 -0
- /package/src/app/api/{auth → hazo_auth}/profile_picture/[filename]/route.ts +0 -0
- /package/src/app/api/{auth → hazo_auth}/register/route.ts +0 -0
- /package/src/app/api/{auth → hazo_auth}/remove_profile_picture/route.ts +0 -0
- /package/src/app/api/{auth → hazo_auth}/resend_verification/route.ts +0 -0
- /package/src/app/api/{auth → hazo_auth}/reset_password/route.ts +0 -0
- /package/src/app/api/{auth → hazo_auth}/update_user/route.ts +0 -0
- /package/src/app/{forgot_password → hazo_auth/forgot_password}/forgot_password_page_client.tsx +0 -0
- /package/src/app/{forgot_password → hazo_auth/forgot_password}/page.tsx +0 -0
- /package/src/app/{login → hazo_auth/login}/login_page_client.tsx +0 -0
- /package/src/app/{login → hazo_auth/login}/page.tsx +0 -0
- /package/src/app/{my_settings → hazo_auth/my_settings}/my_settings_page_client.tsx +0 -0
- /package/src/app/{my_settings → hazo_auth/my_settings}/page.tsx +0 -0
- /package/src/app/{register → hazo_auth/register}/page.tsx +0 -0
- /package/src/app/{register → hazo_auth/register}/register_page_client.tsx +0 -0
- /package/src/app/{reset_password → hazo_auth/reset_password}/page.tsx +0 -0
- /package/src/app/{reset_password → hazo_auth/reset_password}/reset_password_page_client.tsx +0 -0
- /package/src/app/{verify_email → hazo_auth/verify_email}/page.tsx +0 -0
- /package/src/app/{verify_email → hazo_auth/verify_email}/verify_email_page_client.tsx +0 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hazo_auth",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"files": [
|
|
5
5
|
"src/**/*",
|
|
6
6
|
"public/file.svg",
|
|
@@ -35,7 +35,9 @@
|
|
|
35
35
|
"test:watch": "cross-env NODE_ENV=test POSTGREST_URL=http://209.38.26.241:4402 POSTGREST_API_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoiYXBpX3VzZXIifQ.zBoUGymrxTUk1DNYIGUCtQU4HFaWEHlbE9_8Y3hUaTw jest --watch"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
|
+
"@radix-ui/react-alert-dialog": "^1.1.15",
|
|
38
39
|
"@radix-ui/react-avatar": "^1.1.11",
|
|
40
|
+
"@radix-ui/react-checkbox": "^1.3.3",
|
|
39
41
|
"@radix-ui/react-dialog": "^1.1.15",
|
|
40
42
|
"@radix-ui/react-dropdown-menu": "^2.1.16",
|
|
41
43
|
"@radix-ui/react-label": "^2.1.8",
|
|
@@ -160,7 +160,7 @@ export async function POST(request: NextRequest) {
|
|
|
160
160
|
// Generate URL (relative to public or absolute)
|
|
161
161
|
// For Next.js, we'll serve from a public route or use absolute path
|
|
162
162
|
// For now, use a relative path that can be served via API or static file serving
|
|
163
|
-
const profilePictureUrl = `/api/
|
|
163
|
+
const profilePictureUrl = `/api/hazo_auth/profile_picture/${fileName}`;
|
|
164
164
|
|
|
165
165
|
// Update user record
|
|
166
166
|
const updateResult = await update_user_profile_picture(
|
|
@@ -198,7 +198,7 @@ export async function POST(request: NextRequest) {
|
|
|
198
198
|
// Only delete if the old profile picture was an uploaded file
|
|
199
199
|
if (oldSourceUI === "upload") {
|
|
200
200
|
try {
|
|
201
|
-
// Extract filename from URL (e.g., /api/
|
|
201
|
+
// Extract filename from URL (e.g., /api/hazo_auth/profile_picture/user_id.jpg)
|
|
202
202
|
const oldFileName = oldProfilePictureUrl.split("/").pop();
|
|
203
203
|
|
|
204
204
|
if (oldFileName) {
|
|
@@ -6,6 +6,8 @@ import { create_app_logger } from "@/lib/app_logger";
|
|
|
6
6
|
import { change_password } from "@/lib/services/password_change_service";
|
|
7
7
|
import { get_filename, get_line_number } from "@/lib/utils/api_route_helpers";
|
|
8
8
|
import { require_auth } from "@/lib/auth/auth_utils.server";
|
|
9
|
+
import { get_auth_cache } from "@/lib/auth/auth_cache";
|
|
10
|
+
import { get_auth_utility_config } from "@/lib/auth_utility_config.server";
|
|
9
11
|
|
|
10
12
|
// section: api_handler
|
|
11
13
|
export async function POST(request: NextRequest) {
|
|
@@ -75,6 +77,27 @@ export async function POST(request: NextRequest) {
|
|
|
75
77
|
);
|
|
76
78
|
}
|
|
77
79
|
|
|
80
|
+
// Invalidate user cache after password change
|
|
81
|
+
try {
|
|
82
|
+
const config = get_auth_utility_config();
|
|
83
|
+
const cache = get_auth_cache(
|
|
84
|
+
config.cache_max_users,
|
|
85
|
+
config.cache_ttl_minutes,
|
|
86
|
+
config.cache_max_age_minutes,
|
|
87
|
+
);
|
|
88
|
+
cache.invalidate_user(user_id);
|
|
89
|
+
} catch (cache_error) {
|
|
90
|
+
// Log but don't fail password change if cache invalidation fails
|
|
91
|
+
const cache_error_message =
|
|
92
|
+
cache_error instanceof Error ? cache_error.message : "Unknown error";
|
|
93
|
+
logger.warn("password_change_cache_invalidation_failed", {
|
|
94
|
+
filename: get_filename(),
|
|
95
|
+
line_number: get_line_number(),
|
|
96
|
+
user_id,
|
|
97
|
+
error: cache_error_message,
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
78
101
|
logger.info("password_change_successful", {
|
|
79
102
|
filename: get_filename(),
|
|
80
103
|
line_number: get_line_number(),
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// file_description: API route for hazo_get_auth utility (client-side calls)
|
|
2
|
+
// section: imports
|
|
3
|
+
import { NextRequest, NextResponse } from "next/server";
|
|
4
|
+
import { hazo_get_auth } from "@/lib/auth/hazo_get_auth.server";
|
|
5
|
+
import { PermissionError } from "@/lib/auth/auth_types";
|
|
6
|
+
import { create_app_logger } from "@/lib/app_logger";
|
|
7
|
+
import { get_filename, get_line_number } from "@/lib/utils/api_route_helpers";
|
|
8
|
+
|
|
9
|
+
// section: route_config
|
|
10
|
+
export const dynamic = "force-dynamic";
|
|
11
|
+
|
|
12
|
+
// section: api_handler
|
|
13
|
+
/**
|
|
14
|
+
* POST - Get authentication status and permissions
|
|
15
|
+
* Body: { required_permissions?: string[], strict?: boolean }
|
|
16
|
+
*/
|
|
17
|
+
export async function POST(request: NextRequest) {
|
|
18
|
+
const logger = create_app_logger();
|
|
19
|
+
|
|
20
|
+
try {
|
|
21
|
+
const body = await request.json();
|
|
22
|
+
const { required_permissions, strict } = body;
|
|
23
|
+
|
|
24
|
+
// Validate required_permissions if provided
|
|
25
|
+
if (
|
|
26
|
+
required_permissions !== undefined &&
|
|
27
|
+
(!Array.isArray(required_permissions) ||
|
|
28
|
+
!required_permissions.every((p) => typeof p === "string"))
|
|
29
|
+
) {
|
|
30
|
+
return NextResponse.json(
|
|
31
|
+
{ error: "required_permissions must be an array of strings" },
|
|
32
|
+
{ status: 400 },
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Validate strict if provided
|
|
37
|
+
if (strict !== undefined && typeof strict !== "boolean") {
|
|
38
|
+
return NextResponse.json(
|
|
39
|
+
{ error: "strict must be a boolean" },
|
|
40
|
+
{ status: 400 },
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Call hazo_get_auth
|
|
45
|
+
const result = await hazo_get_auth(request, {
|
|
46
|
+
required_permissions,
|
|
47
|
+
strict,
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
return NextResponse.json(result, { status: 200 });
|
|
51
|
+
} catch (error) {
|
|
52
|
+
// Handle PermissionError (strict mode)
|
|
53
|
+
if (error instanceof PermissionError) {
|
|
54
|
+
logger.warn("auth_utility_permission_error", {
|
|
55
|
+
filename: get_filename(),
|
|
56
|
+
line_number: get_line_number(),
|
|
57
|
+
missing_permissions: error.missing_permissions,
|
|
58
|
+
required_permissions: error.required_permissions,
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
return NextResponse.json(
|
|
62
|
+
{
|
|
63
|
+
error: "Permission denied",
|
|
64
|
+
missing_permissions: error.missing_permissions,
|
|
65
|
+
user_friendly_message: error.user_friendly_message,
|
|
66
|
+
},
|
|
67
|
+
{ status: 403 },
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Handle other errors
|
|
72
|
+
const error_message =
|
|
73
|
+
error instanceof Error ? error.message : "Unknown error";
|
|
74
|
+
const error_stack = error instanceof Error ? error.stack : undefined;
|
|
75
|
+
|
|
76
|
+
logger.error("auth_utility_api_error", {
|
|
77
|
+
filename: get_filename(),
|
|
78
|
+
line_number: get_line_number(),
|
|
79
|
+
error_message,
|
|
80
|
+
error_stack,
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
return NextResponse.json(
|
|
84
|
+
{ error: error_message },
|
|
85
|
+
{ status: 500 },
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
// file_description: API route for manual cache invalidation (admin endpoint)
|
|
2
|
+
// section: imports
|
|
3
|
+
import { NextRequest, NextResponse } from "next/server";
|
|
4
|
+
import { get_auth_cache } from "@/lib/auth/auth_cache";
|
|
5
|
+
import { get_auth_utility_config } from "@/lib/auth_utility_config.server";
|
|
6
|
+
import { create_app_logger } from "@/lib/app_logger";
|
|
7
|
+
import { get_filename, get_line_number } from "@/lib/utils/api_route_helpers";
|
|
8
|
+
import { hazo_get_auth } from "@/lib/auth/hazo_get_auth.server";
|
|
9
|
+
|
|
10
|
+
// section: route_config
|
|
11
|
+
export const dynamic = "force-dynamic";
|
|
12
|
+
|
|
13
|
+
// section: api_handler
|
|
14
|
+
/**
|
|
15
|
+
* POST - Manually invalidate auth cache
|
|
16
|
+
* Body: { user_id?: string, role_ids?: number[], invalidate_all?: boolean }
|
|
17
|
+
* Requires admin permission (checked via hazo_get_auth)
|
|
18
|
+
*/
|
|
19
|
+
export async function POST(request: NextRequest) {
|
|
20
|
+
const logger = create_app_logger();
|
|
21
|
+
|
|
22
|
+
try {
|
|
23
|
+
// Check authentication and admin permission
|
|
24
|
+
const auth_result = await hazo_get_auth(request, {
|
|
25
|
+
required_permissions: ["admin_user_management"], // Require admin permission
|
|
26
|
+
strict: true, // Throw error if not authorized
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
if (!auth_result.authenticated) {
|
|
30
|
+
return NextResponse.json(
|
|
31
|
+
{ error: "Authentication required" },
|
|
32
|
+
{ status: 401 },
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const body = await request.json();
|
|
37
|
+
const { user_id, role_ids, invalidate_all } = body;
|
|
38
|
+
|
|
39
|
+
// Validate input
|
|
40
|
+
if (invalidate_all !== undefined && typeof invalidate_all !== "boolean") {
|
|
41
|
+
return NextResponse.json(
|
|
42
|
+
{ error: "invalidate_all must be a boolean" },
|
|
43
|
+
{ status: 400 },
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (user_id !== undefined && typeof user_id !== "string") {
|
|
48
|
+
return NextResponse.json(
|
|
49
|
+
{ error: "user_id must be a string" },
|
|
50
|
+
{ status: 400 },
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (
|
|
55
|
+
role_ids !== undefined &&
|
|
56
|
+
(!Array.isArray(role_ids) ||
|
|
57
|
+
!role_ids.every((id) => typeof id === "number"))
|
|
58
|
+
) {
|
|
59
|
+
return NextResponse.json(
|
|
60
|
+
{ error: "role_ids must be an array of numbers" },
|
|
61
|
+
{ status: 400 },
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const config = get_auth_utility_config();
|
|
66
|
+
const cache = get_auth_cache(
|
|
67
|
+
config.cache_max_users,
|
|
68
|
+
config.cache_ttl_minutes,
|
|
69
|
+
config.cache_max_age_minutes,
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
// Perform invalidation
|
|
73
|
+
if (invalidate_all === true) {
|
|
74
|
+
cache.invalidate_all();
|
|
75
|
+
logger.info("auth_cache_invalidated_all", {
|
|
76
|
+
filename: get_filename(),
|
|
77
|
+
line_number: get_line_number(),
|
|
78
|
+
user_id: auth_result.user.id,
|
|
79
|
+
});
|
|
80
|
+
} else if (user_id) {
|
|
81
|
+
cache.invalidate_user(user_id);
|
|
82
|
+
logger.info("auth_cache_invalidated_user", {
|
|
83
|
+
filename: get_filename(),
|
|
84
|
+
line_number: get_line_number(),
|
|
85
|
+
invalidated_user_id: user_id,
|
|
86
|
+
admin_user_id: auth_result.user.id,
|
|
87
|
+
});
|
|
88
|
+
} else if (role_ids && role_ids.length > 0) {
|
|
89
|
+
cache.invalidate_by_roles(role_ids);
|
|
90
|
+
logger.info("auth_cache_invalidated_roles", {
|
|
91
|
+
filename: get_filename(),
|
|
92
|
+
line_number: get_line_number(),
|
|
93
|
+
role_ids,
|
|
94
|
+
admin_user_id: auth_result.user.id,
|
|
95
|
+
});
|
|
96
|
+
} else {
|
|
97
|
+
return NextResponse.json(
|
|
98
|
+
{
|
|
99
|
+
error:
|
|
100
|
+
"Must provide user_id, role_ids, or invalidate_all=true",
|
|
101
|
+
},
|
|
102
|
+
{ status: 400 },
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return NextResponse.json(
|
|
107
|
+
{
|
|
108
|
+
success: true,
|
|
109
|
+
message: "Cache invalidated successfully",
|
|
110
|
+
},
|
|
111
|
+
{ status: 200 },
|
|
112
|
+
);
|
|
113
|
+
} catch (error) {
|
|
114
|
+
// Handle PermissionError (strict mode)
|
|
115
|
+
if (error instanceof Error && error.name === "PermissionError") {
|
|
116
|
+
return NextResponse.json(
|
|
117
|
+
{ error: "Permission denied. Admin access required." },
|
|
118
|
+
{ status: 403 },
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const error_message =
|
|
123
|
+
error instanceof Error ? error.message : "Unknown error";
|
|
124
|
+
const error_stack = error instanceof Error ? error.stack : undefined;
|
|
125
|
+
|
|
126
|
+
logger.error("auth_cache_invalidation_error", {
|
|
127
|
+
filename: get_filename(),
|
|
128
|
+
line_number: get_line_number(),
|
|
129
|
+
error_message,
|
|
130
|
+
error_stack,
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
return NextResponse.json(
|
|
134
|
+
{ error: "Failed to invalidate cache" },
|
|
135
|
+
{ status: 500 },
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
import { NextRequest, NextResponse } from "next/server";
|
|
4
4
|
import { create_app_logger } from "@/lib/app_logger";
|
|
5
5
|
import { get_filename, get_line_number } from "@/lib/utils/api_route_helpers";
|
|
6
|
+
import { get_auth_cache } from "@/lib/auth/auth_cache";
|
|
7
|
+
import { get_auth_utility_config } from "@/lib/auth_utility_config.server";
|
|
6
8
|
|
|
7
9
|
// section: api_handler
|
|
8
10
|
export async function POST(request: NextRequest) {
|
|
@@ -32,6 +34,31 @@ export async function POST(request: NextRequest) {
|
|
|
32
34
|
path: "/",
|
|
33
35
|
});
|
|
34
36
|
|
|
37
|
+
// Invalidate user cache
|
|
38
|
+
if (user_id) {
|
|
39
|
+
try {
|
|
40
|
+
const config = get_auth_utility_config();
|
|
41
|
+
const cache = get_auth_cache(
|
|
42
|
+
config.cache_max_users,
|
|
43
|
+
config.cache_ttl_minutes,
|
|
44
|
+
config.cache_max_age_minutes,
|
|
45
|
+
);
|
|
46
|
+
cache.invalidate_user(user_id);
|
|
47
|
+
} catch (cache_error) {
|
|
48
|
+
// Log but don't fail logout if cache invalidation fails
|
|
49
|
+
const cache_error_message =
|
|
50
|
+
cache_error instanceof Error
|
|
51
|
+
? cache_error.message
|
|
52
|
+
: "Unknown error";
|
|
53
|
+
logger.warn("logout_cache_invalidation_failed", {
|
|
54
|
+
filename: get_filename(),
|
|
55
|
+
line_number: get_line_number(),
|
|
56
|
+
user_id,
|
|
57
|
+
error: cache_error_message,
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
35
62
|
if (user_email || user_id) {
|
|
36
63
|
logger.info("logout_successful", {
|
|
37
64
|
filename: get_filename(),
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
// file_description: API route for uploading 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 { get_profile_picture_config } from "@/lib/profile_picture_config.server";
|
|
7
|
+
import { get_file_types_config } from "@/lib/file_types_config.server";
|
|
8
|
+
import { update_user_profile_picture } from "@/lib/services/profile_picture_service";
|
|
9
|
+
import { createCrudService } from "hazo_connect/server";
|
|
10
|
+
import { map_db_source_to_ui } from "@/lib/services/profile_picture_source_mapper";
|
|
11
|
+
import { get_filename, get_line_number } from "@/lib/utils/api_route_helpers";
|
|
12
|
+
import fs from "fs";
|
|
13
|
+
import path from "path";
|
|
14
|
+
|
|
15
|
+
// section: api_handler
|
|
16
|
+
export async function POST(request: NextRequest) {
|
|
17
|
+
const logger = create_app_logger();
|
|
18
|
+
|
|
19
|
+
try {
|
|
20
|
+
// Use centralized auth check
|
|
21
|
+
let user_id: string;
|
|
22
|
+
try {
|
|
23
|
+
const { require_auth } = await import("@/lib/auth/auth_utils.server");
|
|
24
|
+
const user = await require_auth(request);
|
|
25
|
+
user_id = user.user_id;
|
|
26
|
+
} catch (error) {
|
|
27
|
+
if (error instanceof Error && error.message === "Authentication required") {
|
|
28
|
+
logger.warn("profile_picture_upload_authentication_failed", {
|
|
29
|
+
filename: get_filename(),
|
|
30
|
+
line_number: get_line_number(),
|
|
31
|
+
error: "User not authenticated",
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
return NextResponse.json(
|
|
35
|
+
{ error: "Authentication required" },
|
|
36
|
+
{ status: 401 }
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
throw error;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Check if upload is enabled
|
|
43
|
+
const config = get_profile_picture_config();
|
|
44
|
+
if (!config.allow_photo_upload) {
|
|
45
|
+
logger.warn("profile_picture_upload_disabled", {
|
|
46
|
+
filename: get_filename(),
|
|
47
|
+
line_number: get_line_number(),
|
|
48
|
+
user_id,
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
return NextResponse.json(
|
|
52
|
+
{ error: "Photo upload is not enabled" },
|
|
53
|
+
{ status: 403 }
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (!config.upload_photo_path) {
|
|
58
|
+
logger.warn("profile_picture_upload_path_not_configured", {
|
|
59
|
+
filename: get_filename(),
|
|
60
|
+
line_number: get_line_number(),
|
|
61
|
+
user_id,
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
return NextResponse.json(
|
|
65
|
+
{ error: "Upload path is not configured" },
|
|
66
|
+
{ status: 500 }
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Get FormData
|
|
71
|
+
const formData = await request.formData();
|
|
72
|
+
const file = formData.get("file") as File | null;
|
|
73
|
+
|
|
74
|
+
if (!file) {
|
|
75
|
+
logger.warn("profile_picture_upload_no_file", {
|
|
76
|
+
filename: get_filename(),
|
|
77
|
+
line_number: get_line_number(),
|
|
78
|
+
user_id,
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
return NextResponse.json(
|
|
82
|
+
{ error: "No file provided" },
|
|
83
|
+
{ status: 400 }
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Validate file type
|
|
88
|
+
const fileTypes = get_file_types_config();
|
|
89
|
+
const fileType = file.type;
|
|
90
|
+
if (!fileTypes.allowed_image_mime_types.includes(fileType)) {
|
|
91
|
+
logger.warn("profile_picture_upload_invalid_type", {
|
|
92
|
+
filename: get_filename(),
|
|
93
|
+
line_number: get_line_number(),
|
|
94
|
+
user_id,
|
|
95
|
+
fileType,
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
return NextResponse.json(
|
|
99
|
+
{ error: "Invalid file type. Only JPG and PNG files are allowed." },
|
|
100
|
+
{ status: 400 }
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Validate file size (should already be compressed client-side, but check server-side too)
|
|
105
|
+
const fileSize = file.size;
|
|
106
|
+
if (fileSize > config.max_photo_size) {
|
|
107
|
+
logger.warn("profile_picture_upload_too_large", {
|
|
108
|
+
filename: get_filename(),
|
|
109
|
+
line_number: get_line_number(),
|
|
110
|
+
user_id,
|
|
111
|
+
fileSize,
|
|
112
|
+
maxSize: config.max_photo_size,
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
return NextResponse.json(
|
|
116
|
+
{ error: `File size exceeds maximum allowed size of ${config.max_photo_size} bytes` },
|
|
117
|
+
{ status: 400 }
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Get current user profile picture info before updating
|
|
122
|
+
const hazoConnect = get_hazo_connect_instance();
|
|
123
|
+
const users_service = createCrudService(hazoConnect, "hazo_users");
|
|
124
|
+
const current_users = await users_service.findBy({ id: user_id });
|
|
125
|
+
|
|
126
|
+
let oldProfilePictureUrl: string | null = null;
|
|
127
|
+
let oldProfileSource: string | null = null;
|
|
128
|
+
|
|
129
|
+
if (Array.isArray(current_users) && current_users.length > 0) {
|
|
130
|
+
const current_user = current_users[0];
|
|
131
|
+
oldProfilePictureUrl = (current_user.profile_picture_url as string) || null;
|
|
132
|
+
oldProfileSource = (current_user.profile_source as string) || null;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Determine file extension from MIME type
|
|
136
|
+
const mimeToExt: Record<string, string> = {
|
|
137
|
+
"image/jpeg": "jpg",
|
|
138
|
+
"image/jpg": "jpg",
|
|
139
|
+
"image/png": "png",
|
|
140
|
+
};
|
|
141
|
+
const fileExtension = mimeToExt[fileType] || "jpg";
|
|
142
|
+
const fileName = `${user_id}.${fileExtension}`;
|
|
143
|
+
|
|
144
|
+
// Resolve upload path
|
|
145
|
+
const uploadPath = path.isAbsolute(config.upload_photo_path)
|
|
146
|
+
? config.upload_photo_path
|
|
147
|
+
: path.resolve(process.cwd(), config.upload_photo_path);
|
|
148
|
+
|
|
149
|
+
// Create upload directory if it doesn't exist
|
|
150
|
+
if (!fs.existsSync(uploadPath)) {
|
|
151
|
+
fs.mkdirSync(uploadPath, { recursive: true });
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Save file
|
|
155
|
+
const filePath = path.join(uploadPath, fileName);
|
|
156
|
+
const arrayBuffer = await file.arrayBuffer();
|
|
157
|
+
const buffer = Buffer.from(arrayBuffer);
|
|
158
|
+
fs.writeFileSync(filePath, buffer);
|
|
159
|
+
|
|
160
|
+
// Generate URL (relative to public or absolute)
|
|
161
|
+
// For Next.js, we'll serve from a public route or use absolute path
|
|
162
|
+
// For now, use a relative path that can be served via API or static file serving
|
|
163
|
+
const profilePictureUrl = `/api/hazo_auth/profile_picture/${fileName}`;
|
|
164
|
+
|
|
165
|
+
// Update user record
|
|
166
|
+
const updateResult = await update_user_profile_picture(
|
|
167
|
+
hazoConnect,
|
|
168
|
+
user_id,
|
|
169
|
+
profilePictureUrl,
|
|
170
|
+
"upload",
|
|
171
|
+
);
|
|
172
|
+
|
|
173
|
+
if (!updateResult.success) {
|
|
174
|
+
// Clean up uploaded file
|
|
175
|
+
try {
|
|
176
|
+
fs.unlinkSync(filePath);
|
|
177
|
+
} catch (error) {
|
|
178
|
+
// Ignore cleanup errors
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
logger.warn("profile_picture_upload_update_failed", {
|
|
182
|
+
filename: get_filename(),
|
|
183
|
+
line_number: get_line_number(),
|
|
184
|
+
user_id,
|
|
185
|
+
error: updateResult.error,
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
return NextResponse.json(
|
|
189
|
+
{ error: updateResult.error || "Failed to update profile picture" },
|
|
190
|
+
{ status: 500 }
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// Delete old profile picture file if it exists and was an uploaded file
|
|
195
|
+
if (oldProfilePictureUrl && oldProfileSource) {
|
|
196
|
+
const oldSourceUI = map_db_source_to_ui(oldProfileSource);
|
|
197
|
+
|
|
198
|
+
// Only delete if the old profile picture was an uploaded file
|
|
199
|
+
if (oldSourceUI === "upload") {
|
|
200
|
+
try {
|
|
201
|
+
// Extract filename from URL (e.g., /api/hazo_auth/profile_picture/user_id.jpg)
|
|
202
|
+
const oldFileName = oldProfilePictureUrl.split("/").pop();
|
|
203
|
+
|
|
204
|
+
if (oldFileName) {
|
|
205
|
+
// Check if it's a user-specific file (starts with user_id)
|
|
206
|
+
if (oldFileName.startsWith(user_id)) {
|
|
207
|
+
const oldFilePath = path.join(uploadPath, oldFileName);
|
|
208
|
+
|
|
209
|
+
// Only delete if it's a different file (different extension)
|
|
210
|
+
if (oldFilePath !== filePath && fs.existsSync(oldFilePath)) {
|
|
211
|
+
fs.unlinkSync(oldFilePath);
|
|
212
|
+
|
|
213
|
+
logger.info("profile_picture_old_file_deleted", {
|
|
214
|
+
filename: get_filename(),
|
|
215
|
+
line_number: get_line_number(),
|
|
216
|
+
user_id,
|
|
217
|
+
oldFileName,
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
} catch (error) {
|
|
223
|
+
// Log error but don't fail the request
|
|
224
|
+
logger.warn("profile_picture_old_file_delete_failed", {
|
|
225
|
+
filename: get_filename(),
|
|
226
|
+
line_number: get_line_number(),
|
|
227
|
+
user_id,
|
|
228
|
+
oldProfilePictureUrl,
|
|
229
|
+
error: error instanceof Error ? error.message : "Unknown error",
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
logger.info("profile_picture_upload_successful", {
|
|
236
|
+
filename: get_filename(),
|
|
237
|
+
line_number: get_line_number(),
|
|
238
|
+
user_id,
|
|
239
|
+
fileName,
|
|
240
|
+
fileSize,
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
return NextResponse.json(
|
|
244
|
+
{
|
|
245
|
+
success: true,
|
|
246
|
+
profile_picture_url: profilePictureUrl,
|
|
247
|
+
message: "Profile picture uploaded successfully",
|
|
248
|
+
},
|
|
249
|
+
{ status: 200 }
|
|
250
|
+
);
|
|
251
|
+
} catch (error) {
|
|
252
|
+
const error_message = error instanceof Error ? error.message : "Unknown error";
|
|
253
|
+
const error_stack = error instanceof Error ? error.stack : undefined;
|
|
254
|
+
|
|
255
|
+
logger.error("profile_picture_upload_error", {
|
|
256
|
+
filename: get_filename(),
|
|
257
|
+
line_number: get_line_number(),
|
|
258
|
+
error_message,
|
|
259
|
+
error_stack,
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
return NextResponse.json(
|
|
263
|
+
{ error: "Failed to upload profile picture. Please try again." },
|
|
264
|
+
{ status: 500 }
|
|
265
|
+
);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|