@umituz/react-native-auth 3.6.85 → 3.6.86
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.6.
|
|
3
|
+
"version": "3.6.86",
|
|
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",
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* User Document Repository
|
|
3
|
+
* Manages Firestore user documents using Firebase Auth UID
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { doc, setDoc, getDoc, serverTimestamp, getFirestore } from "firebase/firestore";
|
|
7
|
+
import type { Firestore } from "firebase/firestore";
|
|
8
|
+
import type { User } from "firebase/auth";
|
|
9
|
+
import type { UserProfile } from "../../domain/entities/UserProfile";
|
|
10
|
+
|
|
11
|
+
const USERS_COLLECTION = "users";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Create or update user document in Firestore
|
|
15
|
+
* Uses Firebase Auth UID as document ID (best practice)
|
|
16
|
+
*/
|
|
17
|
+
export async function createOrUpdateUserDocument(
|
|
18
|
+
firestore: Firestore,
|
|
19
|
+
user: User
|
|
20
|
+
): Promise<void> {
|
|
21
|
+
try {
|
|
22
|
+
const userRef = doc(firestore, USERS_COLLECTION, user.uid);
|
|
23
|
+
const userDoc = await getDoc(userRef);
|
|
24
|
+
|
|
25
|
+
const userData: Partial<UserProfile> = {
|
|
26
|
+
uid: user.uid,
|
|
27
|
+
email: user.email,
|
|
28
|
+
displayName: user.displayName,
|
|
29
|
+
photoURL: user.photoURL,
|
|
30
|
+
isAnonymous: user.isAnonymous,
|
|
31
|
+
lastLoginAt: new Date(),
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
if (!userDoc.exists()) {
|
|
35
|
+
// New user - create document with createdAt
|
|
36
|
+
await setDoc(userRef, {
|
|
37
|
+
...userData,
|
|
38
|
+
createdAt: serverTimestamp(),
|
|
39
|
+
lastLoginAt: serverTimestamp(),
|
|
40
|
+
});
|
|
41
|
+
} else {
|
|
42
|
+
// Existing user - update lastLoginAt only
|
|
43
|
+
await setDoc(
|
|
44
|
+
userRef,
|
|
45
|
+
{
|
|
46
|
+
lastLoginAt: serverTimestamp(),
|
|
47
|
+
// Update these in case they changed (e.g., anonymous → registered)
|
|
48
|
+
email: userData.email,
|
|
49
|
+
displayName: userData.displayName,
|
|
50
|
+
photoURL: userData.photoURL,
|
|
51
|
+
isAnonymous: userData.isAnonymous,
|
|
52
|
+
},
|
|
53
|
+
{ merge: true }
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
} catch (error) {
|
|
57
|
+
console.error("[UserDocumentRepository] Failed to create/update user document:", error);
|
|
58
|
+
// Don't throw - allow auth to continue even if Firestore fails
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Check if user document exists
|
|
64
|
+
*/
|
|
65
|
+
export async function userDocumentExists(
|
|
66
|
+
firestore: Firestore,
|
|
67
|
+
uid: string
|
|
68
|
+
): Promise<boolean> {
|
|
69
|
+
try {
|
|
70
|
+
const userRef = doc(firestore, USERS_COLLECTION, uid);
|
|
71
|
+
const userDoc = await getDoc(userRef);
|
|
72
|
+
return userDoc.exists();
|
|
73
|
+
} catch (error) {
|
|
74
|
+
console.error("[UserDocumentRepository] Failed to check user document:", error);
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Get Firestore instance
|
|
81
|
+
* Exported for testing and external use
|
|
82
|
+
*/
|
|
83
|
+
export function getFirestoreInstance(): Firestore {
|
|
84
|
+
return getFirestore();
|
|
85
|
+
}
|
|
@@ -18,6 +18,10 @@ import {
|
|
|
18
18
|
} from "./listenerState.util";
|
|
19
19
|
import { onIdTokenChanged } from "firebase/auth";
|
|
20
20
|
import { getAuthService } from "../../../infrastructure/services/AuthService";
|
|
21
|
+
import {
|
|
22
|
+
createOrUpdateUserDocument,
|
|
23
|
+
getFirestoreInstance
|
|
24
|
+
} from "../../repositories/UserDocumentRepository";
|
|
21
25
|
|
|
22
26
|
type Store = AuthActions & { isAnonymous: boolean };
|
|
23
27
|
|
|
@@ -113,6 +117,11 @@ function handleAuthStateChange(
|
|
|
113
117
|
store.setFirebaseUser(user);
|
|
114
118
|
store.setInitialized(true);
|
|
115
119
|
|
|
120
|
+
// Create or update Firestore user document (best practice)
|
|
121
|
+
if (user) {
|
|
122
|
+
void createOrUpdateUserDocument(getFirestoreInstance(), user);
|
|
123
|
+
}
|
|
124
|
+
|
|
116
125
|
// Handle conversion from anonymous
|
|
117
126
|
if (user && !user.isAnonymous && store.isAnonymous) {
|
|
118
127
|
store.setIsAnonymous(false);
|