@umituz/react-native-auth 3.2.15 → 3.3.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umituz/react-native-auth",
3
- "version": "3.2.15",
3
+ "version": "3.3.0",
4
4
  "description": "Authentication service for React Native apps - Secure, type-safe, and production-ready. Provider-agnostic design with dependency injection, configurable validation, and comprehensive error handling.",
5
5
  "main": "./src/index.ts",
6
6
  "types": "./src/index.ts",
package/src/index.ts CHANGED
@@ -93,9 +93,19 @@ export {
93
93
  configureUserDocumentService,
94
94
  } from './infrastructure/services/UserDocumentService';
95
95
 
96
+ // Unified Auth Initialization (RECOMMENDED)
97
+ export {
98
+ initializeAuth,
99
+ isAuthInitialized,
100
+ resetAuthInitialization,
101
+ } from './infrastructure/services/initializeAuth';
102
+
103
+ export type { InitializeAuthOptions } from './infrastructure/services/initializeAuth';
104
+
96
105
  export type {
97
106
  UserDocumentConfig,
98
107
  UserDocumentExtras,
108
+ UserDocumentUser,
99
109
  } from './infrastructure/services/UserDocumentService';
100
110
 
101
111
  // =============================================================================
@@ -5,11 +5,23 @@
5
5
  */
6
6
 
7
7
  import { doc, getDoc, setDoc, serverTimestamp } from "firebase/firestore";
8
+ import type { User } from "firebase/auth";
8
9
  import { getFirestore } from "@umituz/react-native-firebase";
9
- import type { AuthUser } from "../../domain/entities/AuthUser";
10
10
 
11
11
  declare const __DEV__: boolean;
12
12
 
13
+ /**
14
+ * Minimal user interface for document creation
15
+ * Compatible with both Firebase User and AuthUser
16
+ */
17
+ export interface UserDocumentUser {
18
+ uid: string;
19
+ displayName?: string | null;
20
+ email?: string | null;
21
+ photoURL?: string | null;
22
+ isAnonymous?: boolean;
23
+ }
24
+
13
25
  /**
14
26
  * Configuration for user document service
15
27
  */
@@ -51,7 +63,7 @@ export function configureUserDocumentService(config: UserDocumentConfig): void {
51
63
  /**
52
64
  * Get sign-up method from auth user
53
65
  */
54
- function getSignUpMethod(user: AuthUser): string | undefined {
66
+ function getSignUpMethod(user: UserDocumentUser): string | undefined {
55
67
  if (user.isAnonymous) return "anonymous";
56
68
  if (user.email) {
57
69
  const providerData = (
@@ -72,7 +84,7 @@ function getSignUpMethod(user: AuthUser): string | undefined {
72
84
  * Build base user data from auth user
73
85
  */
74
86
  function buildBaseData(
75
- user: AuthUser,
87
+ user: UserDocumentUser,
76
88
  extras?: UserDocumentExtras,
77
89
  ): Record<string, unknown> {
78
90
  const data: Record<string, unknown> = {
@@ -153,7 +165,7 @@ function buildUpdateData(
153
165
  * Creates new document or updates existing one
154
166
  */
155
167
  export async function ensureUserDocument(
156
- user: AuthUser,
168
+ user: UserDocumentUser | User,
157
169
  extras?: UserDocumentExtras,
158
170
  ): Promise<boolean> {
159
171
  const db = getFirestore();
@@ -0,0 +1,134 @@
1
+ /**
2
+ * Unified Auth Initialization
3
+ * Single function to initialize all auth services
4
+ *
5
+ * Combines:
6
+ * - AuthService (email/password auth)
7
+ * - Auth Listener (state management)
8
+ * - User Document Service (Firestore)
9
+ */
10
+
11
+ import type { Auth, User } from "firebase/auth";
12
+ import { getFirebaseAuth } from "@umituz/react-native-firebase";
13
+ import { initializeAuthService } from "./AuthService";
14
+ import { configureUserDocumentService, ensureUserDocument } from "./UserDocumentService";
15
+ import type { UserDocumentConfig } from "./UserDocumentService";
16
+ import { initializeAuthListener } from "../../presentation/stores/initializeAuthListener";
17
+ import type { AuthConfig } from "../../domain/value-objects/AuthConfig";
18
+
19
+ /**
20
+ * Unified auth initialization options
21
+ */
22
+ export interface InitializeAuthOptions {
23
+ /** User document collection name (default: "users") */
24
+ userCollection?: string;
25
+
26
+ /** Additional fields to store with user documents */
27
+ extraFields?: Record<string, unknown>;
28
+
29
+ /** Callback to collect device/app info for user documents */
30
+ collectExtras?: () => Promise<Record<string, unknown>>;
31
+
32
+ /** Enable auto anonymous sign-in (default: true) */
33
+ autoAnonymousSignIn?: boolean;
34
+
35
+ /** Callback when auth state changes */
36
+ onAuthStateChange?: (user: User | null) => void | Promise<void>;
37
+
38
+ /** Auth configuration (password rules, etc.) */
39
+ authConfig?: Partial<AuthConfig>;
40
+ }
41
+
42
+ let isInitialized = false;
43
+
44
+ /**
45
+ * Initialize all auth services with a single call
46
+ *
47
+ * @example
48
+ * ```typescript
49
+ * import { initializeAuth, ensureUserDocument } from '@umituz/react-native-auth';
50
+ *
51
+ * await initializeAuth({
52
+ * userCollection: 'users',
53
+ * autoAnonymousSignIn: true,
54
+ * onAuthStateChange: async (user) => {
55
+ * if (user) {
56
+ * await ensureUserDocument(user);
57
+ * }
58
+ * },
59
+ * });
60
+ * ```
61
+ */
62
+ export async function initializeAuth(
63
+ options: InitializeAuthOptions = {}
64
+ ): Promise<{ success: boolean; auth: Auth | null }> {
65
+ if (isInitialized) {
66
+ const auth = getFirebaseAuth();
67
+ return { success: true, auth };
68
+ }
69
+
70
+ const {
71
+ userCollection = "users",
72
+ extraFields,
73
+ collectExtras,
74
+ autoAnonymousSignIn = true,
75
+ onAuthStateChange,
76
+ authConfig,
77
+ } = options;
78
+
79
+ // 1. Configure user document service
80
+ const userDocConfig: UserDocumentConfig = {
81
+ collectionName: userCollection,
82
+ };
83
+ if (extraFields) userDocConfig.extraFields = extraFields;
84
+ if (collectExtras) userDocConfig.collectExtras = collectExtras;
85
+
86
+ configureUserDocumentService(userDocConfig);
87
+
88
+ // 2. Get Firebase Auth
89
+ const auth = getFirebaseAuth();
90
+ if (!auth) {
91
+ return { success: false, auth: null };
92
+ }
93
+
94
+ // 3. Initialize AuthService (for email/password auth)
95
+ try {
96
+ await initializeAuthService(auth, authConfig);
97
+ } catch {
98
+ // AuthService initialization failed, but we can continue
99
+ // Email/password auth won't work, but social/anonymous will
100
+ }
101
+
102
+ // 4. Initialize Auth Listener (for state management)
103
+ initializeAuthListener({
104
+ autoAnonymousSignIn,
105
+ onAuthStateChange: async (user) => {
106
+ // Auto-create/update user document
107
+ if (user) {
108
+ await ensureUserDocument(user);
109
+ }
110
+
111
+ // Call app's custom callback
112
+ if (onAuthStateChange) {
113
+ await onAuthStateChange(user);
114
+ }
115
+ },
116
+ });
117
+
118
+ isInitialized = true;
119
+ return { success: true, auth };
120
+ }
121
+
122
+ /**
123
+ * Check if auth is initialized
124
+ */
125
+ export function isAuthInitialized(): boolean {
126
+ return isInitialized;
127
+ }
128
+
129
+ /**
130
+ * Reset auth initialization state (for testing)
131
+ */
132
+ export function resetAuthInitialization(): void {
133
+ isInitialized = false;
134
+ }