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.
Files changed (89) hide show
  1. package/hazo_auth_config.example.ini +75 -0
  2. package/instrumentation.ts +1 -1
  3. package/next.config.mjs +1 -1
  4. package/package.json +4 -1
  5. package/src/app/api/{auth → hazo_auth/auth}/upload_profile_picture/route.ts +2 -2
  6. package/src/app/api/{auth → hazo_auth}/change_password/route.ts +23 -0
  7. package/src/app/api/hazo_auth/get_auth/route.ts +89 -0
  8. package/src/app/api/hazo_auth/invalidate_cache/route.ts +139 -0
  9. package/src/app/api/{auth → hazo_auth}/library_photos/route.ts +3 -0
  10. package/src/app/api/{auth → hazo_auth}/logout/route.ts +27 -0
  11. package/src/app/api/hazo_auth/upload_profile_picture/route.ts +268 -0
  12. package/src/app/api/hazo_auth/user_management/permissions/route.ts +367 -0
  13. package/src/app/api/hazo_auth/user_management/roles/route.ts +442 -0
  14. package/src/app/api/hazo_auth/user_management/users/roles/route.ts +367 -0
  15. package/src/app/api/hazo_auth/user_management/users/route.ts +239 -0
  16. package/src/app/api/{auth → hazo_auth}/validate_reset_token/route.ts +3 -0
  17. package/src/app/api/{auth → hazo_auth}/verify_email/route.ts +3 -0
  18. package/src/app/globals.css +1 -1
  19. package/src/app/hazo_auth/user_management/page.tsx +14 -0
  20. package/src/app/hazo_auth/user_management/user_management_page_client.tsx +16 -0
  21. package/src/app/hazo_connect/api/sqlite/data/route.ts +7 -1
  22. package/src/app/hazo_connect/api/sqlite/schema/route.ts +14 -4
  23. package/src/app/hazo_connect/api/sqlite/tables/route.ts +14 -4
  24. package/src/app/hazo_connect/sqlite_admin/sqlite-admin-client.tsx +40 -3
  25. package/src/app/layout.tsx +1 -1
  26. package/src/app/page.tsx +4 -4
  27. package/src/components/layouts/email_verification/hooks/use_email_verification.ts +4 -4
  28. package/src/components/layouts/email_verification/index.tsx +1 -1
  29. package/src/components/layouts/forgot_password/hooks/use_forgot_password_form.ts +1 -1
  30. package/src/components/layouts/login/hooks/use_login_form.ts +2 -2
  31. package/src/components/layouts/my_settings/components/profile_picture_dialog.tsx +1 -1
  32. package/src/components/layouts/my_settings/components/profile_picture_library_tab.tsx +35 -6
  33. package/src/components/layouts/my_settings/hooks/use_my_settings.ts +5 -5
  34. package/src/components/layouts/my_settings/index.tsx +1 -1
  35. package/src/components/layouts/register/hooks/use_register_form.ts +1 -1
  36. package/src/components/layouts/reset_password/hooks/use_reset_password_form.ts +3 -3
  37. package/src/components/layouts/reset_password/index.tsx +2 -2
  38. package/src/components/layouts/shared/components/logout_button.tsx +1 -1
  39. package/src/components/layouts/shared/components/profile_pic_menu.tsx +321 -0
  40. package/src/components/layouts/shared/components/profile_pic_menu_wrapper.tsx +40 -0
  41. package/src/components/layouts/shared/components/sidebar_layout_wrapper.tsx +22 -72
  42. package/src/components/layouts/shared/components/unauthorized_guard.tsx +1 -1
  43. package/src/components/layouts/shared/hooks/use_auth_status.ts +1 -1
  44. package/src/components/layouts/shared/hooks/use_hazo_auth.ts +158 -0
  45. package/src/components/layouts/user_management/components/roles_matrix.tsx +607 -0
  46. package/src/components/layouts/user_management/index.tsx +1295 -0
  47. package/src/components/ui/alert-dialog.tsx +141 -0
  48. package/src/components/ui/checkbox.tsx +30 -0
  49. package/src/components/ui/dropdown-menu.tsx +201 -0
  50. package/src/components/ui/table.tsx +120 -0
  51. package/src/lib/auth/auth_cache.ts +220 -0
  52. package/src/lib/auth/auth_rate_limiter.ts +121 -0
  53. package/src/lib/auth/auth_types.ts +65 -0
  54. package/src/lib/auth/hazo_get_auth.server.ts +333 -0
  55. package/src/lib/auth_utility_config.server.ts +136 -0
  56. package/src/lib/hazo_connect_setup.server.ts +2 -3
  57. package/src/lib/my_settings_config.server.ts +1 -1
  58. package/src/lib/profile_pic_menu_config.server.ts +138 -0
  59. package/src/lib/reset_password_config.server.ts +5 -5
  60. package/src/lib/services/email_service.ts +2 -2
  61. package/src/lib/services/profile_picture_remove_service.ts +1 -1
  62. package/src/lib/services/token_service.ts +2 -2
  63. package/src/lib/user_management_config.server.ts +40 -0
  64. package/src/lib/utils.ts +1 -1
  65. package/src/middleware.ts +15 -13
  66. package/src/server/types/express.d.ts +1 -0
  67. package/src/stories/project_overview.stories.tsx +1 -1
  68. package/tailwind.config.ts +1 -1
  69. /package/src/app/api/{auth → hazo_auth}/forgot_password/route.ts +0 -0
  70. /package/src/app/api/{auth → hazo_auth}/login/route.ts +0 -0
  71. /package/src/app/api/{auth → hazo_auth}/me/route.ts +0 -0
  72. /package/src/app/api/{auth → hazo_auth}/profile_picture/[filename]/route.ts +0 -0
  73. /package/src/app/api/{auth → hazo_auth}/register/route.ts +0 -0
  74. /package/src/app/api/{auth → hazo_auth}/remove_profile_picture/route.ts +0 -0
  75. /package/src/app/api/{auth → hazo_auth}/resend_verification/route.ts +0 -0
  76. /package/src/app/api/{auth → hazo_auth}/reset_password/route.ts +0 -0
  77. /package/src/app/api/{auth → hazo_auth}/update_user/route.ts +0 -0
  78. /package/src/app/{forgot_password → hazo_auth/forgot_password}/forgot_password_page_client.tsx +0 -0
  79. /package/src/app/{forgot_password → hazo_auth/forgot_password}/page.tsx +0 -0
  80. /package/src/app/{login → hazo_auth/login}/login_page_client.tsx +0 -0
  81. /package/src/app/{login → hazo_auth/login}/page.tsx +0 -0
  82. /package/src/app/{my_settings → hazo_auth/my_settings}/my_settings_page_client.tsx +0 -0
  83. /package/src/app/{my_settings → hazo_auth/my_settings}/page.tsx +0 -0
  84. /package/src/app/{register → hazo_auth/register}/page.tsx +0 -0
  85. /package/src/app/{register → hazo_auth/register}/register_page_client.tsx +0 -0
  86. /package/src/app/{reset_password → hazo_auth/reset_password}/page.tsx +0 -0
  87. /package/src/app/{reset_password → hazo_auth/reset_password}/reset_password_page_client.tsx +0 -0
  88. /package/src/app/{verify_email → hazo_auth/verify_email}/page.tsx +0 -0
  89. /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
+