@umituz/react-native-auth 3.6.87 → 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.
- package/package.json +1 -1
- package/src/infrastructure/services/UserDocument.types.ts +60 -0
- package/src/infrastructure/services/UserDocumentService.ts +85 -0
- package/src/infrastructure/utils/authStateHandler.ts +7 -0
- package/src/infrastructure/utils/userDocumentBuilder.util.ts +110 -0
- package/src/presentation/hooks/registerForm/registerFormHandlers.ts +3 -1
- package/src/presentation/hooks/useProfileEdit.ts +7 -4
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.88",
|
|
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,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* User Document Types and Configuration
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Minimal user interface for document creation
|
|
7
|
+
* Compatible with both Firebase User and AuthUser
|
|
8
|
+
*/
|
|
9
|
+
export interface UserDocumentUser {
|
|
10
|
+
uid: string;
|
|
11
|
+
displayName?: string | null;
|
|
12
|
+
email?: string | null;
|
|
13
|
+
photoURL?: string | null;
|
|
14
|
+
isAnonymous?: boolean;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Configuration for user document service
|
|
19
|
+
*/
|
|
20
|
+
export interface UserDocumentConfig {
|
|
21
|
+
/** Firestore collection name (default: "users") */
|
|
22
|
+
collectionName?: string;
|
|
23
|
+
/** Additional fields to store with user document */
|
|
24
|
+
extraFields?: Record<string, unknown>;
|
|
25
|
+
/** Callback to collect device/app info */
|
|
26
|
+
collectExtras?: () => Promise<UserDocumentExtras>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* User document extras from device/app
|
|
31
|
+
*/
|
|
32
|
+
export interface UserDocumentExtras {
|
|
33
|
+
[key: string]: string | number | boolean | null | undefined;
|
|
34
|
+
deviceId?: string;
|
|
35
|
+
persistentDeviceId?: string;
|
|
36
|
+
nativeDeviceId?: string;
|
|
37
|
+
platform?: string;
|
|
38
|
+
deviceModel?: string;
|
|
39
|
+
deviceBrand?: string;
|
|
40
|
+
deviceName?: string;
|
|
41
|
+
deviceType?: number | string;
|
|
42
|
+
deviceYearClass?: number | string;
|
|
43
|
+
isDevice?: boolean;
|
|
44
|
+
osName?: string;
|
|
45
|
+
osVersion?: string;
|
|
46
|
+
osBuildId?: string;
|
|
47
|
+
totalMemory?: number | string;
|
|
48
|
+
appVersion?: string;
|
|
49
|
+
buildNumber?: string;
|
|
50
|
+
locale?: string;
|
|
51
|
+
region?: string;
|
|
52
|
+
timezone?: string;
|
|
53
|
+
screenWidth?: number;
|
|
54
|
+
screenHeight?: number;
|
|
55
|
+
screenScale?: number;
|
|
56
|
+
fontScale?: number;
|
|
57
|
+
isLandscape?: boolean;
|
|
58
|
+
previousAnonymousUserId?: string;
|
|
59
|
+
signUpMethod?: string;
|
|
60
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* User Document Service
|
|
3
|
+
* Generic service for creating/updating user documents in Firestore
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { doc, getDoc, setDoc, serverTimestamp } from "firebase/firestore";
|
|
7
|
+
import type { User } from "firebase/auth";
|
|
8
|
+
import { getFirestore } from "@umituz/react-native-firebase";
|
|
9
|
+
import type {
|
|
10
|
+
UserDocumentUser,
|
|
11
|
+
UserDocumentConfig,
|
|
12
|
+
UserDocumentExtras,
|
|
13
|
+
} from "./UserDocument.types";
|
|
14
|
+
import {
|
|
15
|
+
getSignUpMethod,
|
|
16
|
+
buildBaseData,
|
|
17
|
+
buildCreateData,
|
|
18
|
+
buildUpdateData,
|
|
19
|
+
} from "../utils/userDocumentBuilder.util";
|
|
20
|
+
|
|
21
|
+
export type {
|
|
22
|
+
UserDocumentUser,
|
|
23
|
+
UserDocumentConfig,
|
|
24
|
+
UserDocumentExtras,
|
|
25
|
+
} from "./UserDocument.types";
|
|
26
|
+
|
|
27
|
+
declare const __DEV__: boolean;
|
|
28
|
+
|
|
29
|
+
let userDocumentConfig: UserDocumentConfig = {};
|
|
30
|
+
|
|
31
|
+
export function configureUserDocumentService(config: UserDocumentConfig): void {
|
|
32
|
+
userDocumentConfig = { ...config };
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export async function ensureUserDocument(
|
|
36
|
+
user: UserDocumentUser | User,
|
|
37
|
+
extras?: UserDocumentExtras,
|
|
38
|
+
): Promise<boolean> {
|
|
39
|
+
const db = getFirestore();
|
|
40
|
+
if (!db || !user.uid) return false;
|
|
41
|
+
|
|
42
|
+
try {
|
|
43
|
+
let allExtras = extras || {};
|
|
44
|
+
if (userDocumentConfig.collectExtras) {
|
|
45
|
+
const collectedExtras = await userDocumentConfig.collectExtras();
|
|
46
|
+
allExtras = { ...collectedExtras, ...allExtras };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (!allExtras.signUpMethod) allExtras.signUpMethod = getSignUpMethod(user);
|
|
50
|
+
|
|
51
|
+
const collectionName = userDocumentConfig.collectionName || "users";
|
|
52
|
+
const userRef = doc(db, collectionName, user.uid);
|
|
53
|
+
const userDoc = await getDoc(userRef);
|
|
54
|
+
const baseData = buildBaseData(user, allExtras);
|
|
55
|
+
|
|
56
|
+
const docData = !userDoc.exists()
|
|
57
|
+
? buildCreateData(baseData, userDocumentConfig.extraFields, allExtras)
|
|
58
|
+
: buildUpdateData(baseData, allExtras);
|
|
59
|
+
|
|
60
|
+
await setDoc(userRef, docData, { merge: true });
|
|
61
|
+
return true;
|
|
62
|
+
} catch (error) {
|
|
63
|
+
if (typeof __DEV__ !== "undefined" && __DEV__) {
|
|
64
|
+
console.error("[UserDocumentService] Failed:", error);
|
|
65
|
+
}
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export async function markUserDeleted(userId: string): Promise<boolean> {
|
|
71
|
+
const db = getFirestore();
|
|
72
|
+
if (!db || !userId) return false;
|
|
73
|
+
|
|
74
|
+
try {
|
|
75
|
+
const userRef = doc(db, userDocumentConfig.collectionName || "users", userId);
|
|
76
|
+
await setDoc(userRef, {
|
|
77
|
+
isDeleted: true,
|
|
78
|
+
deletedAt: serverTimestamp(),
|
|
79
|
+
updatedAt: serverTimestamp(),
|
|
80
|
+
}, { merge: true });
|
|
81
|
+
return true;
|
|
82
|
+
} catch {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
import type { User } from "firebase/auth";
|
|
7
|
+
import { ensureUserDocument } from "../services/UserDocumentService";
|
|
7
8
|
import { detectConversion, type ConversionState } from "./authConversionDetector";
|
|
8
9
|
|
|
9
10
|
export interface AuthStateHandlerOptions {
|
|
@@ -40,6 +41,12 @@ export function createAuthStateHandler(
|
|
|
40
41
|
}
|
|
41
42
|
}
|
|
42
43
|
|
|
44
|
+
const extras = conversion.isConversion && state.current.previousUserId
|
|
45
|
+
? { previousAnonymousUserId: state.current.previousUserId }
|
|
46
|
+
: undefined;
|
|
47
|
+
|
|
48
|
+
await ensureUserDocument(user, extras);
|
|
49
|
+
|
|
43
50
|
state.current = {
|
|
44
51
|
previousUserId: currentUserId,
|
|
45
52
|
wasAnonymous: isCurrentlyAnonymous,
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* User Document Builder Utility
|
|
3
|
+
* Builds user document data for Firestore (max 100 lines)
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { serverTimestamp } from "firebase/firestore";
|
|
7
|
+
import type {
|
|
8
|
+
UserDocumentUser,
|
|
9
|
+
UserDocumentExtras,
|
|
10
|
+
} from "../services/UserDocument.types";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Gets the sign-up method from user provider data
|
|
14
|
+
*/
|
|
15
|
+
export function getSignUpMethod(user: UserDocumentUser): string | undefined {
|
|
16
|
+
if (user.isAnonymous) return "anonymous";
|
|
17
|
+
if (user.email) {
|
|
18
|
+
const providerData = (
|
|
19
|
+
user as unknown as { providerData?: { providerId: string }[] }
|
|
20
|
+
).providerData;
|
|
21
|
+
if (providerData && providerData.length > 0) {
|
|
22
|
+
const providerId = providerData[0]?.providerId;
|
|
23
|
+
if (providerId === "google.com") return "google";
|
|
24
|
+
if (providerId === "apple.com") return "apple";
|
|
25
|
+
if (providerId === "password") return "email";
|
|
26
|
+
}
|
|
27
|
+
return "email";
|
|
28
|
+
}
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Builds base user data from user object and extras
|
|
34
|
+
*/
|
|
35
|
+
export function buildBaseData(
|
|
36
|
+
user: UserDocumentUser,
|
|
37
|
+
extras?: UserDocumentExtras,
|
|
38
|
+
): Record<string, unknown> {
|
|
39
|
+
const data: Record<string, unknown> = {
|
|
40
|
+
uid: user.uid,
|
|
41
|
+
displayName: user.displayName,
|
|
42
|
+
email: user.email,
|
|
43
|
+
photoURL: user.photoURL,
|
|
44
|
+
isAnonymous: user.isAnonymous,
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
if (extras) {
|
|
48
|
+
const internalFields = ["signUpMethod", "previousAnonymousUserId"];
|
|
49
|
+
Object.keys(extras).forEach((key) => {
|
|
50
|
+
if (!internalFields.includes(key)) {
|
|
51
|
+
const val = extras[key];
|
|
52
|
+
if (val !== undefined) {
|
|
53
|
+
data[key] = val;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return data;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Builds user document data for creation
|
|
64
|
+
*/
|
|
65
|
+
export function buildCreateData(
|
|
66
|
+
baseData: Record<string, unknown>,
|
|
67
|
+
extraFields: Record<string, unknown> | undefined,
|
|
68
|
+
extras?: UserDocumentExtras,
|
|
69
|
+
): Record<string, unknown> {
|
|
70
|
+
const createData: Record<string, unknown> = {
|
|
71
|
+
...baseData,
|
|
72
|
+
...extraFields,
|
|
73
|
+
createdAt: serverTimestamp(),
|
|
74
|
+
updatedAt: serverTimestamp(),
|
|
75
|
+
lastLoginAt: serverTimestamp(),
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
if (extras?.previousAnonymousUserId) {
|
|
79
|
+
createData.previousAnonymousUserId = extras.previousAnonymousUserId;
|
|
80
|
+
createData.convertedFromAnonymous = true;
|
|
81
|
+
createData.convertedAt = serverTimestamp();
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (extras?.signUpMethod) createData.signUpMethod = extras.signUpMethod;
|
|
85
|
+
|
|
86
|
+
return createData;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Builds user document data for update
|
|
91
|
+
*/
|
|
92
|
+
export function buildUpdateData(
|
|
93
|
+
baseData: Record<string, unknown>,
|
|
94
|
+
extras?: UserDocumentExtras,
|
|
95
|
+
): Record<string, unknown> {
|
|
96
|
+
const updateData: Record<string, unknown> = {
|
|
97
|
+
...baseData,
|
|
98
|
+
lastLoginAt: serverTimestamp(),
|
|
99
|
+
updatedAt: serverTimestamp(),
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
if (extras?.previousAnonymousUserId) {
|
|
103
|
+
updateData.previousAnonymousUserId = extras.previousAnonymousUserId;
|
|
104
|
+
updateData.convertedFromAnonymous = true;
|
|
105
|
+
updateData.convertedAt = serverTimestamp();
|
|
106
|
+
if (extras?.signUpMethod) updateData.signUpMethod = extras.signUpMethod;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return updateData;
|
|
110
|
+
}
|
|
@@ -7,8 +7,10 @@ import { useCallback } from "react";
|
|
|
7
7
|
import type { FieldErrors } from "./useRegisterForm.types";
|
|
8
8
|
import { clearFieldError, clearFieldErrors } from "../../utils/form/formErrorUtils";
|
|
9
9
|
|
|
10
|
+
type RegisterFieldKey = "displayName" | "email" | "password" | "confirmPassword";
|
|
11
|
+
|
|
10
12
|
export function useRegisterFormHandlers(
|
|
11
|
-
updateField: (field:
|
|
13
|
+
updateField: (field: RegisterFieldKey, value: string) => void,
|
|
12
14
|
setFieldErrors: React.Dispatch<React.SetStateAction<FieldErrors>>,
|
|
13
15
|
clearLocalError: () => void
|
|
14
16
|
) {
|
|
@@ -58,10 +58,13 @@ export const useProfileEdit = (
|
|
|
58
58
|
isValid: boolean;
|
|
59
59
|
errors: string[];
|
|
60
60
|
} => {
|
|
61
|
-
const result = validateProfileForm(
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
61
|
+
const result = validateProfileForm(
|
|
62
|
+
{
|
|
63
|
+
displayName: formState.displayName,
|
|
64
|
+
email: formState.email,
|
|
65
|
+
},
|
|
66
|
+
(key) => key // Pass-through function - returns key as-is since caller handles translation
|
|
67
|
+
);
|
|
65
68
|
|
|
66
69
|
return {
|
|
67
70
|
isValid: result.isValid,
|