hazo_auth 0.1.2 → 1.0.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/hazo_auth_config.example.ini +75 -0
- package/instrumentation.ts +1 -1
- package/next.config.mjs +1 -1
- package/package.json +4 -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}/library_photos/route.ts +3 -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 +35 -6
- 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 +321 -0
- package/src/components/layouts/shared/components/profile_pic_menu_wrapper.tsx +40 -0
- package/src/components/layouts/shared/components/sidebar_layout_wrapper.tsx +22 -72
- 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/dropdown-menu.tsx +201 -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 +138 -0
- 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}/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
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
// file_description: React hook for hazo_get_auth utility (client-side)
|
|
2
|
+
// section: client_directive
|
|
3
|
+
"use client";
|
|
4
|
+
|
|
5
|
+
// section: imports
|
|
6
|
+
import { useState, useEffect, useCallback } from "react";
|
|
7
|
+
import type { HazoAuthResult } from "@/lib/auth/auth_types";
|
|
8
|
+
|
|
9
|
+
// section: types
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Options for use_hazo_auth hook
|
|
13
|
+
*/
|
|
14
|
+
export type UseHazoAuthOptions = {
|
|
15
|
+
/**
|
|
16
|
+
* Array of required permissions to check
|
|
17
|
+
*/
|
|
18
|
+
required_permissions?: string[];
|
|
19
|
+
/**
|
|
20
|
+
* If true, throws error when permissions are missing (default: false)
|
|
21
|
+
*/
|
|
22
|
+
strict?: boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Skip fetch (for conditional use)
|
|
25
|
+
*/
|
|
26
|
+
skip?: boolean;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Result type for use_hazo_auth hook
|
|
31
|
+
*/
|
|
32
|
+
export type UseHazoAuthResult = HazoAuthResult & {
|
|
33
|
+
/**
|
|
34
|
+
* Loading state
|
|
35
|
+
*/
|
|
36
|
+
loading: boolean;
|
|
37
|
+
/**
|
|
38
|
+
* Error state
|
|
39
|
+
*/
|
|
40
|
+
error: Error | null;
|
|
41
|
+
/**
|
|
42
|
+
* Manual refetch function
|
|
43
|
+
*/
|
|
44
|
+
refetch: () => Promise<void>;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
// section: constants
|
|
48
|
+
const AUTH_STATUS_CHANGE_EVENT = "hazo_auth_status_change";
|
|
49
|
+
|
|
50
|
+
// section: helpers
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Triggers a refresh of hazo_auth status across all components
|
|
54
|
+
* Dispatches a custom event that all use_hazo_auth hooks listen to
|
|
55
|
+
*/
|
|
56
|
+
export function trigger_hazo_auth_refresh(): void {
|
|
57
|
+
if (typeof window !== "undefined") {
|
|
58
|
+
window.dispatchEvent(
|
|
59
|
+
new CustomEvent(AUTH_STATUS_CHANGE_EVENT),
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// section: hook
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* React hook for hazo_get_auth utility
|
|
68
|
+
* Fetches authentication status and permissions from /api/auth/get_auth
|
|
69
|
+
* @param options - Optional parameters for permission checking
|
|
70
|
+
* @returns UseHazoAuthResult with auth data, loading state, and refetch function
|
|
71
|
+
*/
|
|
72
|
+
export function use_hazo_auth(
|
|
73
|
+
options?: UseHazoAuthOptions,
|
|
74
|
+
): UseHazoAuthResult {
|
|
75
|
+
const [authResult, setAuthResult] = useState<HazoAuthResult>({
|
|
76
|
+
authenticated: false,
|
|
77
|
+
user: null,
|
|
78
|
+
permissions: [],
|
|
79
|
+
permission_ok: false,
|
|
80
|
+
});
|
|
81
|
+
const [loading, setLoading] = useState<boolean>(true);
|
|
82
|
+
const [error, setError] = useState<Error | null>(null);
|
|
83
|
+
|
|
84
|
+
const fetchAuth = useCallback(async () => {
|
|
85
|
+
if (options?.skip) {
|
|
86
|
+
setLoading(false);
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
setLoading(true);
|
|
91
|
+
setError(null);
|
|
92
|
+
|
|
93
|
+
try {
|
|
94
|
+
const response = await fetch("/api/hazo_auth/get_auth", {
|
|
95
|
+
method: "POST",
|
|
96
|
+
headers: {
|
|
97
|
+
"Content-Type": "application/json",
|
|
98
|
+
},
|
|
99
|
+
credentials: "include",
|
|
100
|
+
body: JSON.stringify({
|
|
101
|
+
required_permissions: options?.required_permissions,
|
|
102
|
+
strict: options?.strict || false,
|
|
103
|
+
}),
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
if (!response.ok) {
|
|
107
|
+
const error_data = await response.json();
|
|
108
|
+
throw new Error(
|
|
109
|
+
error_data.user_friendly_message ||
|
|
110
|
+
error_data.error ||
|
|
111
|
+
"Failed to fetch authentication status",
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const data = await response.json();
|
|
116
|
+
setAuthResult(data);
|
|
117
|
+
} catch (err) {
|
|
118
|
+
const error_message =
|
|
119
|
+
err instanceof Error ? err : new Error("Unknown error");
|
|
120
|
+
setError(error_message);
|
|
121
|
+
setAuthResult({
|
|
122
|
+
authenticated: false,
|
|
123
|
+
user: null,
|
|
124
|
+
permissions: [],
|
|
125
|
+
permission_ok: false,
|
|
126
|
+
});
|
|
127
|
+
} finally {
|
|
128
|
+
setLoading(false);
|
|
129
|
+
}
|
|
130
|
+
}, [options?.required_permissions, options?.strict, options?.skip]);
|
|
131
|
+
|
|
132
|
+
useEffect(() => {
|
|
133
|
+
// Fetch auth status on mount
|
|
134
|
+
void fetchAuth();
|
|
135
|
+
|
|
136
|
+
// Listen for auth status change events
|
|
137
|
+
const handleAuthChange = () => {
|
|
138
|
+
void fetchAuth();
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
window.addEventListener(AUTH_STATUS_CHANGE_EVENT, handleAuthChange);
|
|
142
|
+
|
|
143
|
+
return () => {
|
|
144
|
+
window.removeEventListener(
|
|
145
|
+
AUTH_STATUS_CHANGE_EVENT,
|
|
146
|
+
handleAuthChange,
|
|
147
|
+
);
|
|
148
|
+
};
|
|
149
|
+
}, [fetchAuth]);
|
|
150
|
+
|
|
151
|
+
return {
|
|
152
|
+
...authResult,
|
|
153
|
+
loading,
|
|
154
|
+
error,
|
|
155
|
+
refetch: fetchAuth,
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
|