@umituz/react-native-auth 3.2.16 → 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.
|
|
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,6 +93,15 @@ 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,
|
|
@@ -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
|
+
}
|