@umituz/web-dashboard 2.0.9 → 2.1.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.
@@ -0,0 +1,301 @@
1
+ /**
2
+ * useAuth Hook
3
+ *
4
+ * Core authentication hook for managing auth state and actions
5
+ */
6
+
7
+ import { useState, useCallback, useEffect } from "react";
8
+ import type {
9
+ AuthState,
10
+ User,
11
+ AuthActions,
12
+ LoginCredentials,
13
+ RegisterData,
14
+ ForgotPasswordData,
15
+ ResetPasswordData,
16
+ } from "../types/auth";
17
+ import {
18
+ validateLogin,
19
+ validateRegister,
20
+ validateForgotPassword,
21
+ validateResetPassword,
22
+ } from "../utils/auth";
23
+
24
+ interface UseAuthOptions {
25
+ /** Initial authenticated state */
26
+ initialAuthenticated?: boolean;
27
+ /** Current user data */
28
+ initialUser?: User | null;
29
+ /** Auth implementation (connects to your backend) */
30
+ authProvider?: {
31
+ login: (credentials: LoginCredentials) => Promise<User>;
32
+ register: (data: RegisterData) => Promise<User>;
33
+ logout: () => Promise<void>;
34
+ forgotPassword: (data: ForgotPasswordData) => Promise<void>;
35
+ resetPassword: (data: ResetPasswordData) => Promise<void>;
36
+ refreshAuth: () => Promise<User | null>;
37
+ };
38
+ }
39
+
40
+ /**
41
+ * useAuth hook
42
+ *
43
+ * Manages authentication state and provides auth actions
44
+ *
45
+ * @param options - Hook options
46
+ * @returns Auth state and actions
47
+ */
48
+ export function useAuth(options: UseAuthOptions = {}) {
49
+ const {
50
+ initialAuthenticated = false,
51
+ initialUser = null,
52
+ authProvider,
53
+ } = options;
54
+
55
+ // State
56
+ const [authState, setAuthState] = useState<AuthState>({
57
+ isAuthenticated: initialAuthenticated,
58
+ user: initialUser,
59
+ isLoading: false,
60
+ error: null,
61
+ });
62
+
63
+ // Update auth state helper
64
+ const updateState = useCallback((updates: Partial<AuthState>) => {
65
+ setAuthState((prev) => ({ ...prev, ...updates }));
66
+ }, []);
67
+
68
+ // Login action
69
+ const login = useCallback(async (credentials: LoginCredentials) => {
70
+ // Validate credentials
71
+ const validation = validateLogin(credentials);
72
+ if (!validation.valid) {
73
+ updateState({ error: validation.error });
74
+ throw new Error(validation.error);
75
+ }
76
+
77
+ updateState({ isLoading: true, error: null });
78
+
79
+ try {
80
+ if (!authProvider) {
81
+ throw new Error("Auth provider not configured");
82
+ }
83
+
84
+ const user = await authProvider.login(credentials);
85
+ updateState({
86
+ isAuthenticated: true,
87
+ user,
88
+ isLoading: false,
89
+ error: null,
90
+ });
91
+
92
+ return user;
93
+ } catch (error) {
94
+ const errorMessage = error instanceof Error ? error.message : "Login failed";
95
+ updateState({
96
+ isAuthenticated: false,
97
+ user: null,
98
+ isLoading: false,
99
+ error: errorMessage,
100
+ });
101
+ throw error;
102
+ }
103
+ }, [authProvider, updateState]);
104
+
105
+ // Register action
106
+ const register = useCallback(async (data: RegisterData) => {
107
+ // Validate registration data
108
+ const validation = validateRegister(data, false);
109
+ if (!validation.valid) {
110
+ updateState({ error: validation.error });
111
+ throw new Error(validation.error);
112
+ }
113
+
114
+ updateState({ isLoading: true, error: null });
115
+
116
+ try {
117
+ if (!authProvider) {
118
+ throw new Error("Auth provider not configured");
119
+ }
120
+
121
+ const user = await authProvider.register(data);
122
+ updateState({
123
+ isAuthenticated: true,
124
+ user,
125
+ isLoading: false,
126
+ error: null,
127
+ });
128
+
129
+ return user;
130
+ } catch (error) {
131
+ const errorMessage = error instanceof Error ? error.message : "Registration failed";
132
+ updateState({
133
+ isAuthenticated: false,
134
+ user: null,
135
+ isLoading: false,
136
+ error: errorMessage,
137
+ });
138
+ throw error;
139
+ }
140
+ }, [authProvider, updateState]);
141
+
142
+ // Logout action
143
+ const logout = useCallback(async () => {
144
+ updateState({ isLoading: true, error: null });
145
+
146
+ try {
147
+ if (!authProvider) {
148
+ throw new Error("Auth provider not configured");
149
+ }
150
+
151
+ await authProvider.logout();
152
+
153
+ updateState({
154
+ isAuthenticated: false,
155
+ user: null,
156
+ isLoading: false,
157
+ error: null,
158
+ });
159
+ } catch (error) {
160
+ const errorMessage = error instanceof Error ? error.message : "Logout failed";
161
+ updateState({
162
+ isLoading: false,
163
+ error: errorMessage,
164
+ });
165
+ throw error;
166
+ }
167
+ }, [authProvider, updateState]);
168
+
169
+ // Forgot password action
170
+ const forgotPassword = useCallback(async (data: ForgotPasswordData) => {
171
+ // Validate email
172
+ const validation = validateForgotPassword(data);
173
+ if (!validation.valid) {
174
+ updateState({ error: validation.error });
175
+ throw new Error(validation.error);
176
+ }
177
+
178
+ updateState({ isLoading: true, error: null });
179
+
180
+ try {
181
+ if (!authProvider) {
182
+ throw new Error("Auth provider not configured");
183
+ }
184
+
185
+ await authProvider.forgotPassword(data);
186
+ updateState({ isLoading: false });
187
+ } catch (error) {
188
+ const errorMessage = error instanceof Error ? error.message : "Failed to send reset email";
189
+ updateState({
190
+ isLoading: false,
191
+ error: errorMessage,
192
+ });
193
+ throw error;
194
+ }
195
+ }, [authProvider, updateState]);
196
+
197
+ // Reset password action
198
+ const resetPassword = useCallback(async (data: ResetPasswordData) => {
199
+ // Validate reset data
200
+ const validation = validateResetPassword(data);
201
+ if (!validation.valid) {
202
+ updateState({ error: validation.error });
203
+ throw new Error(validation.error);
204
+ }
205
+
206
+ updateState({ isLoading: true, error: null });
207
+
208
+ try {
209
+ if (!authProvider) {
210
+ throw new Error("Auth provider not configured");
211
+ }
212
+
213
+ await authProvider.resetPassword(data);
214
+ updateState({ isLoading: false });
215
+ } catch (error) {
216
+ const errorMessage = error instanceof Error ? error.message : "Failed to reset password";
217
+ updateState({
218
+ isLoading: false,
219
+ error: errorMessage,
220
+ });
221
+ throw error;
222
+ }
223
+ }, [authProvider, updateState]);
224
+
225
+ // Update profile action
226
+ const updateProfile = useCallback(async (data: Partial<User>) => {
227
+ updateState({ isLoading: true, error: null });
228
+
229
+ try {
230
+ // Update local user state
231
+ setAuthState((prev) => ({
232
+ ...prev,
233
+ user: prev.user ? { ...prev.user, ...data } : null,
234
+ isLoading: false,
235
+ error: null,
236
+ }));
237
+
238
+ // In production, call your API here
239
+ // await authProvider?.updateProfile(data);
240
+ } catch (error) {
241
+ const errorMessage = error instanceof Error ? error.message : "Failed to update profile";
242
+ updateState({
243
+ isLoading: false,
244
+ error: errorMessage,
245
+ });
246
+ throw error;
247
+ }
248
+ }, [updateState]);
249
+
250
+ // Refresh auth action
251
+ const refresh = useCallback(async () => {
252
+ updateState({ isLoading: true, error: null });
253
+
254
+ try {
255
+ if (!authProvider) {
256
+ throw new Error("Auth provider not configured");
257
+ }
258
+
259
+ const user = await authProvider.refreshAuth();
260
+
261
+ updateState({
262
+ isAuthenticated: !!user,
263
+ user,
264
+ isLoading: false,
265
+ error: null,
266
+ });
267
+
268
+ return user;
269
+ } catch (error) {
270
+ updateState({
271
+ isAuthenticated: false,
272
+ user: null,
273
+ isLoading: false,
274
+ error: null,
275
+ });
276
+ throw error;
277
+ }
278
+ }, [authProvider, updateState]);
279
+
280
+ // Clear error action
281
+ const clearError = useCallback(() => {
282
+ updateState({ error: null });
283
+ }, [updateState]);
284
+
285
+ // Auth actions object
286
+ const authActions: AuthActions = {
287
+ login,
288
+ register,
289
+ logout,
290
+ forgotPassword,
291
+ resetPassword,
292
+ updateProfile,
293
+ refresh,
294
+ };
295
+
296
+ return {
297
+ ...authState,
298
+ ...authActions,
299
+ clearError,
300
+ };
301
+ }
@@ -0,0 +1,58 @@
1
+ /**
2
+ * Auth Domain
3
+ *
4
+ * Main entry point for authentication domain
5
+ */
6
+
7
+ // Components
8
+ export {
9
+ AuthLayout,
10
+ LoginForm,
11
+ RegisterForm,
12
+ ForgotPasswordForm,
13
+ ResetPasswordForm,
14
+ } from "./components";
15
+
16
+ // Hooks
17
+ export {
18
+ useAuth,
19
+ } from "./hooks";
20
+
21
+ // Utils
22
+ export {
23
+ isValidEmail,
24
+ isValidPassword,
25
+ validateLogin,
26
+ validateRegister,
27
+ validateForgotPassword,
28
+ validateResetPassword,
29
+ getUserDisplayName,
30
+ getUserInitials,
31
+ isEmailVerified,
32
+ formatUserCreatedAt,
33
+ maskEmail,
34
+ generateResetToken,
35
+ calculatePasswordStrength,
36
+ getPasswordStrengthLabel,
37
+ sanitizeInput,
38
+ } from "./utils";
39
+
40
+ // Types
41
+ export type {
42
+ LoginCredentials,
43
+ RegisterData,
44
+ ForgotPasswordData,
45
+ ResetPasswordData,
46
+ AuthState,
47
+ User,
48
+ AuthActions,
49
+ AuthComponentProps,
50
+ AuthStep,
51
+ AuthConfig,
52
+ SocialProvider,
53
+ AuthLayoutProps,
54
+ LoginFormProps,
55
+ RegisterFormProps,
56
+ ForgotPasswordFormProps,
57
+ ResetPasswordFormProps,
58
+ } from "./types";
@@ -0,0 +1,265 @@
1
+ /**
2
+ * Auth Types
3
+ *
4
+ * Type definitions for authentication system
5
+ */
6
+
7
+ import type { ComponentType, ReactElement } from "react";
8
+
9
+ /**
10
+ * User credentials for login
11
+ */
12
+ export interface LoginCredentials {
13
+ /** Email address */
14
+ email: string;
15
+ /** Password */
16
+ password: string;
17
+ }
18
+
19
+ /**
20
+ * Registration data
21
+ */
22
+ export interface RegisterData {
23
+ /** Email address */
24
+ email: string;
25
+ /** Password */
26
+ password: string;
27
+ /** Full name */
28
+ name?: string;
29
+ /** Additional user data */
30
+ metadata?: Record<string, unknown>;
31
+ }
32
+
33
+ /**
34
+ * Password reset request
35
+ */
36
+ export interface ForgotPasswordData {
37
+ /** Email address */
38
+ email: string;
39
+ }
40
+
41
+ /**
42
+ * Password reset confirmation
43
+ */
44
+ export interface ResetPasswordData {
45
+ /** Reset token from email */
46
+ token: string;
47
+ /** New password */
48
+ password: string;
49
+ /** Password confirmation */
50
+ confirmPassword: string;
51
+ }
52
+
53
+ /**
54
+ * Auth state
55
+ */
56
+ export interface AuthState {
57
+ /** Is user authenticated */
58
+ isAuthenticated: boolean;
59
+ /** Current user data */
60
+ user: User | null;
61
+ /** Is authentication loading */
62
+ isLoading: boolean;
63
+ /** Authentication error */
64
+ error: string | null;
65
+ }
66
+
67
+ /**
68
+ * User profile
69
+ */
70
+ export interface User {
71
+ /** Unique user ID */
72
+ id: string;
73
+ /** Email address */
74
+ email: string;
75
+ /** Display name */
76
+ name?: string;
77
+ /** Profile avatar URL */
78
+ avatar?: string;
79
+ /** Email verification status */
80
+ emailVerified?: boolean;
81
+ /** Additional user data */
82
+ metadata?: Record<string, unknown>;
83
+ /** Account creation date */
84
+ createdAt?: string;
85
+ /** Last login date */
86
+ lastLoginAt?: string;
87
+ }
88
+
89
+ /**
90
+ * Auth actions
91
+ */
92
+ export interface AuthActions {
93
+ /** Login with email/password */
94
+ login: (credentials: LoginCredentials) => Promise<void>;
95
+ /** Register new account */
96
+ register: (data: RegisterData) => Promise<void>;
97
+ /** Logout current user */
98
+ logout: () => Promise<void>;
99
+ /** Send password reset email */
100
+ forgotPassword: (data: ForgotPasswordData) => Promise<void>;
101
+ /** Reset password with token */
102
+ resetPassword: (data: ResetPasswordData) => Promise<void>;
103
+ /** Update user profile */
104
+ updateProfile: (data: Partial<User>) => Promise<void>;
105
+ /** Refresh authentication session */
106
+ refresh: () => Promise<void>;
107
+ }
108
+
109
+ /**
110
+ * Auth component props
111
+ */
112
+ export interface AuthComponentProps {
113
+ /** Current auth state */
114
+ authState: AuthState;
115
+ /** Auth actions */
116
+ authActions: AuthActions;
117
+ /** Update auth state */
118
+ setAuthState: (state: Partial<AuthState>) => void;
119
+ }
120
+
121
+ /**
122
+ * Auth step configuration
123
+ */
124
+ export interface AuthStep {
125
+ /** Step identifier */
126
+ id: string;
127
+ /** Step title */
128
+ title: string;
129
+ /** Step description */
130
+ description?: string;
131
+ /** Custom component */
132
+ component?: ComponentType<AuthComponentProps> | ReactElement;
133
+ /** Validation function */
134
+ validate?: (data: Record<string, unknown>) => boolean;
135
+ }
136
+
137
+ /**
138
+ * Auth configuration
139
+ */
140
+ export interface AuthConfig {
141
+ /** Brand/application name */
142
+ brandName: string;
143
+ /** Brand tagline */
144
+ brandTagline?: string;
145
+ /** Login route */
146
+ loginRoute: string;
147
+ /** Register route */
148
+ registerRoute: string;
149
+ /** Forgot password route */
150
+ forgotPasswordRoute?: string;
151
+ /** Route after successful login */
152
+ afterLoginRoute: string;
153
+ /** Route after logout */
154
+ afterLogoutRoute: string;
155
+ /** Show social login options */
156
+ showSocialLogin?: boolean;
157
+ /** Available social providers */
158
+ socialProviders?: SocialProvider[];
159
+ /** Enable remember me */
160
+ enableRememberMe?: boolean;
161
+ /** Enable email verification */
162
+ requireEmailVerification?: boolean;
163
+ /** Auth steps (for multi-step onboarding) */
164
+ steps?: AuthStep[];
165
+ /** Additional configuration */
166
+ metadata?: Record<string, unknown>;
167
+ }
168
+
169
+ /**
170
+ * Social login provider
171
+ */
172
+ export interface SocialProvider {
173
+ /** Provider ID */
174
+ id: string;
175
+ /** Display name */
176
+ name: string;
177
+ /** Icon/emoji */
178
+ icon: string;
179
+ /** Color theme */
180
+ color?: string;
181
+ /** Scopes to request */
182
+ scopes?: string[];
183
+ }
184
+
185
+ /**
186
+ * Auth layout props
187
+ */
188
+ export interface AuthLayoutProps {
189
+ /** Auth configuration */
190
+ config: AuthConfig;
191
+ /** Current auth state */
192
+ authState?: AuthState;
193
+ /** Auth actions */
194
+ authActions?: AuthActions;
195
+ /** Children content */
196
+ children?: React.ReactNode;
197
+ }
198
+
199
+ /**
200
+ * Login form props
201
+ */
202
+ export interface LoginFormProps {
203
+ /** Auth configuration */
204
+ config: AuthConfig;
205
+ /** Initial credentials */
206
+ defaultCredentials?: Partial<LoginCredentials>;
207
+ /** Show remember me checkbox */
208
+ showRememberMe?: boolean;
209
+ /** Show forgot password link */
210
+ showForgotPassword?: boolean;
211
+ /** Show register link */
212
+ showRegisterLink?: boolean;
213
+ /** On successful login */
214
+ onLoginSuccess?: (user: User) => void | Promise<void>;
215
+ /** On login error */
216
+ onLoginError?: (error: string) => void;
217
+ }
218
+
219
+ /**
220
+ * Register form props
221
+ */
222
+ export interface RegisterFormProps {
223
+ /** Auth configuration */
224
+ config: AuthConfig;
225
+ /** Initial registration data */
226
+ defaultData?: Partial<RegisterData>;
227
+ /** Show terms checkbox */
228
+ showTerms?: boolean;
229
+ /** Show login link */
230
+ showLoginLink?: boolean;
231
+ /** Require password confirmation */
232
+ requirePasswordConfirm?: boolean;
233
+ /** On successful registration */
234
+ onRegisterSuccess?: (user: User) => void | Promise<void>;
235
+ /** On registration error */
236
+ onRegisterError?: (error: string) => void;
237
+ }
238
+
239
+ /**
240
+ * Forgot password form props
241
+ */
242
+ export interface ForgotPasswordFormProps {
243
+ /** Auth configuration */
244
+ config: AuthConfig;
245
+ /** On success */
246
+ onSuccess?: () => void | Promise<void>;
247
+ /** On error */
248
+ onError?: (error: string) => void;
249
+ /** Show back to login link */
250
+ showBackLink?: boolean;
251
+ }
252
+
253
+ /**
254
+ * Reset password form props
255
+ */
256
+ export interface ResetPasswordFormProps {
257
+ /** Auth configuration */
258
+ config: AuthConfig;
259
+ /** Reset token */
260
+ token: string;
261
+ /** On success */
262
+ onSuccess?: () => void | Promise<void>;
263
+ /** On error */
264
+ onError?: (error: string) => void;
265
+ }
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Auth Types
3
+ *
4
+ * Export all auth-related types
5
+ */
6
+
7
+ export type {
8
+ LoginCredentials,
9
+ RegisterData,
10
+ ForgotPasswordData,
11
+ ResetPasswordData,
12
+ AuthState,
13
+ User,
14
+ AuthActions,
15
+ AuthComponentProps,
16
+ AuthStep,
17
+ AuthConfig,
18
+ SocialProvider,
19
+ AuthLayoutProps,
20
+ LoginFormProps,
21
+ RegisterFormProps,
22
+ ForgotPasswordFormProps,
23
+ ResetPasswordFormProps,
24
+ } from "./auth";