@umituz/react-native-auth 3.6.86 → 3.6.88

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 (33) hide show
  1. package/package.json +1 -1
  2. package/src/infrastructure/services/AnonymousModeService.ts +5 -2
  3. package/src/infrastructure/services/AuthService.ts +13 -1
  4. package/src/infrastructure/services/UserDocument.types.ts +60 -0
  5. package/src/infrastructure/services/UserDocumentService.ts +85 -0
  6. package/src/infrastructure/utils/AuthValidation.ts +8 -4
  7. package/src/infrastructure/utils/UserMapper.ts +7 -3
  8. package/src/infrastructure/utils/authStateHandler.ts +7 -0
  9. package/src/infrastructure/utils/error/errorCodeMapping.constants.ts +12 -109
  10. package/src/infrastructure/utils/error/mappings/actionCodeErrorMappings.ts +26 -0
  11. package/src/infrastructure/utils/error/mappings/authErrorMappings.ts +69 -0
  12. package/src/infrastructure/utils/error/mappings/configErrorMappings.ts +31 -0
  13. package/src/infrastructure/utils/error/mappings/errorMapping.types.ts +12 -0
  14. package/src/infrastructure/utils/error/mappings/networkErrorMappings.ts +22 -0
  15. package/src/infrastructure/utils/listener/anonymousHandler.ts +31 -0
  16. package/src/infrastructure/utils/listener/authStateHandler.ts +59 -0
  17. package/src/infrastructure/utils/listener/cleanupHandlers.ts +46 -0
  18. package/src/infrastructure/utils/listener/initializationHandlers.ts +26 -0
  19. package/src/infrastructure/utils/listener/listenerLifecycle.util.ts +19 -161
  20. package/src/infrastructure/utils/listener/listenerState.util.ts +21 -3
  21. package/src/infrastructure/utils/listener/setupListener.ts +63 -0
  22. package/src/infrastructure/utils/userDocumentBuilder.util.ts +110 -0
  23. package/src/presentation/hooks/registerForm/registerFormHandlers.ts +59 -0
  24. package/src/presentation/hooks/registerForm/registerFormSubmit.ts +64 -0
  25. package/src/presentation/hooks/registerForm/useRegisterForm.types.ts +39 -0
  26. package/src/presentation/hooks/useProfileEdit.ts +7 -4
  27. package/src/presentation/hooks/useRegisterForm.ts +31 -109
  28. package/src/presentation/utils/form/formValidation.util.ts +23 -114
  29. package/src/presentation/utils/form/useFormField.hook.ts +2 -2
  30. package/src/presentation/utils/form/validation/formValidation.hook.ts +30 -0
  31. package/src/presentation/utils/form/validation/formValidation.types.ts +31 -0
  32. package/src/presentation/utils/form/validation/formValidation.utils.ts +17 -0
  33. package/src/presentation/utils/form/validation/formValidators.ts +86 -0
@@ -1,47 +1,24 @@
1
+ /**
2
+ * Register Form Hook
3
+ * Main hook that combines all register form logic
4
+ */
5
+
1
6
  import { useState, useCallback } from "react";
2
7
  import { DEFAULT_PASSWORD_CONFIG } from "../../domain/value-objects/AuthConfig";
3
8
  import { useAuth } from "./useAuth";
4
- import { getAuthErrorLocalizationKey, resolveErrorMessage } from "../utils/getAuthErrorMessage";
5
- import { validateRegisterForm, errorsToFieldErrors } from "../utils/form/formValidation.util";
6
- import { alertService } from "@umituz/react-native-design-system";
9
+ import { resolveErrorMessage } from "../utils/getAuthErrorMessage";
7
10
  import { useFormFields } from "../utils/form/useFormField.hook";
8
11
  import { usePasswordValidation } from "../utils/form/usePasswordValidation.hook";
9
- import { clearFieldError, clearFieldErrors } from "../utils/form/formErrorUtils";
10
-
11
- export type FieldErrors = {
12
- displayName?: string;
13
- email?: string;
14
- password?: string;
15
- confirmPassword?: string;
16
- };
17
-
18
- export interface RegisterFormTranslations {
19
- successTitle: string;
20
- signUpSuccess: string;
21
- errors: Record<string, string>;
22
- }
23
-
24
- export interface UseRegisterFormConfig {
25
- translations: RegisterFormTranslations;
26
- }
12
+ import { useRegisterFormHandlers } from "./registerForm/registerFormHandlers";
13
+ import { useRegisterFormSubmit } from "./registerForm/registerFormSubmit";
14
+ import type {
15
+ FieldErrors,
16
+ UseRegisterFormConfig,
17
+ UseRegisterFormResult,
18
+ } from "./registerForm/useRegisterForm.types";
27
19
 
28
- export interface UseRegisterFormResult {
29
- displayName: string;
30
- email: string;
31
- password: string;
32
- confirmPassword: string;
33
- fieldErrors: FieldErrors;
34
- localError: string | null;
35
- loading: boolean;
36
- passwordRequirements: { hasMinLength: boolean };
37
- passwordsMatch: boolean;
38
- handleDisplayNameChange: (text: string) => void;
39
- handleEmailChange: (text: string) => void;
40
- handlePasswordChange: (text: string) => void;
41
- handleConfirmPasswordChange: (text: string) => void;
42
- handleSignUp: () => Promise<void>;
43
- displayError: string | null;
44
- }
20
+ // Re-export types for backward compatibility
21
+ export type { FieldErrors, RegisterFormTranslations, UseRegisterFormConfig, UseRegisterFormResult } from "./registerForm/useRegisterForm.types";
45
22
 
46
23
  export function useRegisterForm(config?: UseRegisterFormConfig): UseRegisterFormResult {
47
24
  const { signUp, loading, error } = useAuth();
@@ -70,9 +47,12 @@ export function useRegisterForm(config?: UseRegisterFormConfig): UseRegisterForm
70
47
  { clearLocalError }
71
48
  );
72
49
 
73
- const getErrorMessage = useCallback((key: string) => {
74
- return resolveErrorMessage(key, translations?.errors);
75
- }, [translations]);
50
+ const getErrorMessage = useCallback(
51
+ (key: string) => {
52
+ return resolveErrorMessage(key, translations?.errors);
53
+ },
54
+ [translations]
55
+ );
76
56
 
77
57
  const { passwordRequirements, passwordsMatch } = usePasswordValidation(
78
58
  fields.password,
@@ -80,73 +60,18 @@ export function useRegisterForm(config?: UseRegisterFormConfig): UseRegisterForm
80
60
  { passwordConfig: DEFAULT_PASSWORD_CONFIG }
81
61
  );
82
62
 
83
- const handleDisplayNameChange = useCallback(
84
- (text: string) => {
85
- updateField("displayName", text);
86
- clearFieldError(setFieldErrors, "displayName");
87
- clearLocalError();
88
- },
89
- [updateField, clearLocalError]
90
- );
91
-
92
- const handleEmailChange = useCallback(
93
- (text: string) => {
94
- updateField("email", text);
95
- clearFieldError(setFieldErrors, "email");
96
- clearLocalError();
97
- },
98
- [updateField, clearLocalError]
99
- );
100
-
101
- const handlePasswordChange = useCallback(
102
- (text: string) => {
103
- updateField("password", text);
104
- clearFieldErrors(setFieldErrors, ["password", "confirmPassword"]);
105
- clearLocalError();
106
- },
107
- [updateField, clearLocalError]
108
- );
63
+ const handlers = useRegisterFormHandlers(updateField, setFieldErrors, clearLocalError);
109
64
 
110
- const handleConfirmPasswordChange = useCallback(
111
- (text: string) => {
112
- updateField("confirmPassword", text);
113
- clearFieldError(setFieldErrors, "confirmPassword");
114
- clearLocalError();
115
- },
116
- [updateField, clearLocalError]
65
+ const { handleSignUp } = useRegisterFormSubmit(
66
+ fields,
67
+ signUp,
68
+ setFieldErrors,
69
+ setLocalError,
70
+ clearFormErrors,
71
+ getErrorMessage,
72
+ translations
117
73
  );
118
74
 
119
- const handleSignUp = useCallback(async () => {
120
- clearFormErrors();
121
-
122
- const validation = validateRegisterForm(
123
- {
124
- displayName: fields.displayName.trim() || undefined,
125
- email: fields.email.trim(),
126
- password: fields.password,
127
- confirmPassword: fields.confirmPassword,
128
- },
129
- getErrorMessage,
130
- DEFAULT_PASSWORD_CONFIG
131
- );
132
-
133
- if (!validation.isValid) {
134
- setFieldErrors(errorsToFieldErrors(validation.errors));
135
- return;
136
- }
137
-
138
- try {
139
- await signUp(fields.email.trim(), fields.password, fields.displayName.trim() || undefined);
140
-
141
- if (translations) {
142
- alertService.success(translations.successTitle, translations.signUpSuccess);
143
- }
144
- } catch (err: unknown) {
145
- const localizationKey = getAuthErrorLocalizationKey(err);
146
- setLocalError(getErrorMessage(localizationKey));
147
- }
148
- }, [fields, signUp, translations, getErrorMessage, clearFormErrors, updateField]);
149
-
150
75
  const displayError = localError || error;
151
76
 
152
77
  return {
@@ -159,10 +84,7 @@ export function useRegisterForm(config?: UseRegisterFormConfig): UseRegisterForm
159
84
  loading,
160
85
  passwordRequirements,
161
86
  passwordsMatch,
162
- handleDisplayNameChange,
163
- handleEmailChange,
164
- handlePasswordChange,
165
- handleConfirmPasswordChange,
87
+ ...handlers,
166
88
  handleSignUp,
167
89
  displayError,
168
90
  };
@@ -1,117 +1,26 @@
1
1
  /**
2
- * Form Validation Utilities
3
- * Shared validation logic for all auth forms
2
+ * Form Validation - Main Export
3
+ * Re-exports all form validation utilities
4
4
  */
5
5
 
6
- import { useCallback } from "react";
7
- import {
8
- validateEmail,
9
- validatePasswordForLogin,
10
- validatePasswordForRegister,
11
- validatePasswordConfirmation,
12
- } from "../../../infrastructure/utils/AuthValidation";
13
- import type { PasswordConfig } from "../../../domain/value-objects/AuthConfig";
14
-
15
- export interface FormValidationError {
16
- field: string;
17
- message: string;
18
- }
19
-
20
- export interface FormValidationResult {
21
- isValid: boolean;
22
- errors: FormValidationError[];
23
- }
24
-
25
- export interface LoginFormValues {
26
- email: string;
27
- password: string;
28
- }
29
-
30
- export interface RegisterFormValues {
31
- displayName?: string;
32
- email: string;
33
- password: string;
34
- confirmPassword: string;
35
- }
36
-
37
- export interface ProfileFormValues {
38
- displayName: string;
39
- email: string;
40
- }
41
-
42
- export function validateLoginForm(values: LoginFormValues, getErrorMessage: (key: string) => string): FormValidationResult {
43
- const errors: FormValidationError[] = [];
44
-
45
- const emailResult = validateEmail(values.email.trim());
46
- if (!emailResult.isValid && emailResult.error) {
47
- errors.push({ field: "email", message: getErrorMessage(emailResult.error) });
48
- }
49
-
50
- const passwordResult = validatePasswordForLogin(values.password);
51
- if (!passwordResult.isValid && passwordResult.error) {
52
- errors.push({ field: "password", message: getErrorMessage(passwordResult.error) });
53
- }
54
-
55
- return { isValid: errors.length === 0, errors };
56
- }
57
-
58
- export function validateRegisterForm(
59
- values: RegisterFormValues,
60
- getErrorMessage: (key: string) => string,
61
- passwordConfig: PasswordConfig
62
- ): FormValidationResult {
63
- const errors: FormValidationError[] = [];
64
-
65
- const emailResult = validateEmail(values.email.trim());
66
- if (!emailResult.isValid && emailResult.error) {
67
- errors.push({ field: "email", message: getErrorMessage(emailResult.error) });
68
- }
69
-
70
- const passwordResult = validatePasswordForRegister(values.password, passwordConfig);
71
- if (!passwordResult.isValid && passwordResult.error) {
72
- errors.push({ field: "password", message: getErrorMessage(passwordResult.error) });
73
- }
74
-
75
- const confirmResult = validatePasswordConfirmation(values.password, values.confirmPassword);
76
- if (!confirmResult.isValid && confirmResult.error) {
77
- errors.push({ field: "confirmPassword", message: getErrorMessage(confirmResult.error) });
78
- }
79
-
80
- return { isValid: errors.length === 0, errors };
81
- }
82
-
83
- export function validateProfileForm(values: ProfileFormValues): FormValidationResult {
84
- const errors: FormValidationError[] = [];
85
-
86
- if (!values.displayName.trim()) {
87
- errors.push({ field: "displayName", message: "Display name is required" });
88
- }
89
-
90
- if (values.email) {
91
- const emailResult = validateEmail(values.email);
92
- if (!emailResult.isValid && emailResult.error) {
93
- errors.push({ field: "email", message: emailResult.error });
94
- }
95
- }
96
-
97
- return { isValid: errors.length === 0, errors };
98
- }
99
-
100
- export function errorsToFieldErrors(errors: FormValidationError[]): Record<string, string> {
101
- const result: Record<string, string> = {};
102
- for (const error of errors) {
103
- result[error.field] = error.message;
104
- }
105
- return result;
106
- }
107
-
108
- export function useFormValidation(getErrorMessage: (key: string) => string) {
109
- const validateLogin = useCallback((values: LoginFormValues) => validateLoginForm(values, getErrorMessage), [getErrorMessage]);
110
- const validateRegister = useCallback(
111
- (values: RegisterFormValues, passwordConfig: PasswordConfig) => validateRegisterForm(values, getErrorMessage, passwordConfig),
112
- [getErrorMessage]
113
- );
114
- const validateProfile = useCallback((values: ProfileFormValues) => validateProfileForm(values), []);
115
-
116
- return { validateLogin, validateRegister, validateProfile, errorsToFieldErrors };
117
- }
6
+ // Types
7
+ export type {
8
+ FormValidationError,
9
+ FormValidationResult,
10
+ LoginFormValues,
11
+ RegisterFormValues,
12
+ ProfileFormValues,
13
+ } from "./validation/formValidation.types";
14
+
15
+ // Validators
16
+ export {
17
+ validateLoginForm,
18
+ validateRegisterForm,
19
+ validateProfileForm,
20
+ } from "./validation/formValidators";
21
+
22
+ // Utilities
23
+ export { errorsToFieldErrors } from "./validation/formValidation.utils";
24
+
25
+ // Hook
26
+ export { useFormValidation } from "./validation/formValidation.hook";
@@ -43,11 +43,11 @@ export function useFormField(
43
43
  const handleChange = useCallback(
44
44
  (text: string) => {
45
45
  setValue(text);
46
- if (error || options?.clearLocalError) {
46
+ if (error) {
47
47
  clearError();
48
48
  }
49
49
  },
50
- [error, options, clearError]
50
+ [error, clearError]
51
51
  );
52
52
 
53
53
  return {
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Form Validation Hook
3
+ * React hook for form validation
4
+ */
5
+
6
+ import { useCallback } from "react";
7
+ import type { PasswordConfig } from "../../../../domain/value-objects/AuthConfig";
8
+ import type { LoginFormValues, RegisterFormValues, ProfileFormValues } from "./formValidation.types";
9
+ import { validateLoginForm, validateRegisterForm, validateProfileForm } from "./formValidators";
10
+ import { errorsToFieldErrors } from "./formValidation.utils";
11
+
12
+ export function useFormValidation(getErrorMessage: (key: string) => string) {
13
+ const validateLogin = useCallback(
14
+ (values: LoginFormValues) => validateLoginForm(values, getErrorMessage),
15
+ [getErrorMessage]
16
+ );
17
+
18
+ const validateRegister = useCallback(
19
+ (values: RegisterFormValues, passwordConfig: PasswordConfig) =>
20
+ validateRegisterForm(values, getErrorMessage, passwordConfig),
21
+ [getErrorMessage]
22
+ );
23
+
24
+ const validateProfile = useCallback(
25
+ (values: ProfileFormValues) => validateProfileForm(values, getErrorMessage),
26
+ [getErrorMessage]
27
+ );
28
+
29
+ return { validateLogin, validateRegister, validateProfile, errorsToFieldErrors };
30
+ }
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Form Validation Types
3
+ * Type definitions for form validation
4
+ */
5
+
6
+ export interface FormValidationError {
7
+ field: string;
8
+ message: string;
9
+ }
10
+
11
+ export interface FormValidationResult {
12
+ isValid: boolean;
13
+ errors: FormValidationError[];
14
+ }
15
+
16
+ export interface LoginFormValues {
17
+ email: string;
18
+ password: string;
19
+ }
20
+
21
+ export interface RegisterFormValues {
22
+ displayName?: string;
23
+ email: string;
24
+ password: string;
25
+ confirmPassword: string;
26
+ }
27
+
28
+ export interface ProfileFormValues {
29
+ displayName: string;
30
+ email: string;
31
+ }
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Form Validation Utilities
3
+ * Helper functions for form validation
4
+ */
5
+
6
+ import type { FormValidationError } from "./formValidation.types";
7
+
8
+ /**
9
+ * Convert validation errors array to field errors object
10
+ */
11
+ export function errorsToFieldErrors(errors: FormValidationError[]): Record<string, string> {
12
+ const result: Record<string, string> = {};
13
+ for (const error of errors) {
14
+ result[error.field] = error.message;
15
+ }
16
+ return result;
17
+ }
@@ -0,0 +1,86 @@
1
+ /**
2
+ * Form Validators
3
+ * Validation functions for different auth forms
4
+ */
5
+
6
+ import {
7
+ validateEmail,
8
+ validatePasswordForLogin,
9
+ validatePasswordForRegister,
10
+ validatePasswordConfirmation,
11
+ } from "../../../../infrastructure/utils/AuthValidation";
12
+ import type { PasswordConfig } from "../../../../domain/value-objects/AuthConfig";
13
+ import type {
14
+ FormValidationResult,
15
+ LoginFormValues,
16
+ RegisterFormValues,
17
+ ProfileFormValues,
18
+ FormValidationError,
19
+ } from "./formValidation.types";
20
+
21
+ export function validateLoginForm(
22
+ values: LoginFormValues,
23
+ getErrorMessage: (key: string) => string
24
+ ): FormValidationResult {
25
+ const errors: FormValidationError[] = [];
26
+
27
+ const emailResult = validateEmail(values.email.trim());
28
+ if (!emailResult.isValid && emailResult.error) {
29
+ errors.push({ field: "email", message: getErrorMessage(emailResult.error) });
30
+ }
31
+
32
+ const passwordResult = validatePasswordForLogin(values.password);
33
+ if (!passwordResult.isValid && passwordResult.error) {
34
+ errors.push({ field: "password", message: getErrorMessage(passwordResult.error) });
35
+ }
36
+
37
+ return { isValid: errors.length === 0, errors };
38
+ }
39
+
40
+ export function validateRegisterForm(
41
+ values: RegisterFormValues,
42
+ getErrorMessage: (key: string) => string,
43
+ passwordConfig: PasswordConfig
44
+ ): FormValidationResult {
45
+ const errors: FormValidationError[] = [];
46
+
47
+ const emailResult = validateEmail(values.email.trim());
48
+ if (!emailResult.isValid && emailResult.error) {
49
+ errors.push({ field: "email", message: getErrorMessage(emailResult.error) });
50
+ }
51
+
52
+ const passwordResult = validatePasswordForRegister(values.password, passwordConfig);
53
+ if (!passwordResult.isValid && passwordResult.error) {
54
+ errors.push({ field: "password", message: getErrorMessage(passwordResult.error) });
55
+ }
56
+
57
+ const confirmResult = validatePasswordConfirmation(values.password, values.confirmPassword);
58
+ if (!confirmResult.isValid && confirmResult.error) {
59
+ errors.push({ field: "confirmPassword", message: getErrorMessage(confirmResult.error) });
60
+ }
61
+
62
+ return { isValid: errors.length === 0, errors };
63
+ }
64
+
65
+ export function validateProfileForm(
66
+ values: ProfileFormValues,
67
+ getErrorMessage: (key: string) => string
68
+ ): FormValidationResult {
69
+ const errors: FormValidationError[] = [];
70
+
71
+ if (!values.displayName || !values.displayName.trim()) {
72
+ errors.push({
73
+ field: "displayName",
74
+ message: getErrorMessage("auth.validation.displayNameRequired"),
75
+ });
76
+ }
77
+
78
+ if (values.email) {
79
+ const emailResult = validateEmail(values.email.trim());
80
+ if (!emailResult.isValid && emailResult.error) {
81
+ errors.push({ field: "email", message: getErrorMessage(emailResult.error) });
82
+ }
83
+ }
84
+
85
+ return { isValid: errors.length === 0, errors };
86
+ }