@blackcode_sa/metaestetics-api 1.14.14 → 1.14.15

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/dist/index.js CHANGED
@@ -15996,7 +15996,7 @@ var AuthService = class extends BaseService {
15996
15996
  * @returns Object containing user and claimed practitioner
15997
15997
  */
15998
15998
  async claimDraftProfilesWithGoogle(idToken, practitionerIds) {
15999
- var _a, _b, _c, _d, _e;
15999
+ var _a, _b;
16000
16000
  try {
16001
16001
  console.log("[AUTH] Starting claim draft profiles with Google", {
16002
16002
  practitionerIdsCount: practitionerIds.length,
@@ -16013,61 +16013,18 @@ var AuthService = class extends BaseService {
16013
16013
  console.log("[AUTH] currentUser IMMEDIATELY AFTER sign-in:", ((_b = this.auth.currentUser) == null ? void 0 : _b.uid) || "NULL");
16014
16014
  console.log("[AUTH] User returned from signInWithCredential:", firebaseUser.uid);
16015
16015
  const practitionerService = new PractitionerService(this.db, this.auth, this.app);
16016
- console.log("[AUTH] currentUser after intializing practitionerService:", ((_c = this.auth.currentUser) == null ? void 0 : _c.uid) || "NULL");
16017
- let user = null;
16016
+ let user;
16018
16017
  try {
16019
16018
  user = await this.userService.getUserById(firebaseUser.uid);
16020
- console.log("[AUTH] User document already exists:", user.uid);
16019
+ console.log("[AUTH] User document found:", user.uid);
16021
16020
  } catch (userError) {
16022
- console.log("[AUTH] User document does not exist, creating it...");
16023
- }
16024
- if (!user) {
16025
- console.log("[AUTH] Creating user document DIRECTLY for:", firebaseUser.uid);
16026
- console.log("[AUTH] Checking auth.currentUser BEFORE refresh:", ((_d = this.auth.currentUser) == null ? void 0 : _d.uid) || "NULL");
16027
- try {
16028
- const token = await firebaseUser.getIdToken(true);
16029
- console.log("[AUTH] Got ID token, length:", (token == null ? void 0 : token.length) || 0);
16030
- } catch (tokenError) {
16031
- console.error("[AUTH] Error getting token:", tokenError);
16032
- }
16033
- await new Promise((resolve) => setTimeout(resolve, 100));
16034
- console.log("[AUTH] Using THIS.auth.currentUser AFTER refresh:", ((_e = this.auth.currentUser) == null ? void 0 : _e.uid) || "NULL");
16035
- console.log("[AUTH] Using THIS.db:", this.db ? "EXISTS" : "NULL");
16036
- if (!this.auth.currentUser) {
16037
- console.error("[AUTH] \u26A0\uFE0F auth.currentUser is STILL NULL after refresh! This is a React Native Firebase JS SDK issue.");
16038
- console.error("[AUTH] Firestore will deny permission because request.auth will be null.");
16039
- }
16040
- const userData = {
16041
- uid: firebaseUser.uid,
16042
- email: firebaseUser.email,
16043
- roles: ["practitioner" /* PRACTITIONER */],
16044
- isAnonymous: firebaseUser.isAnonymous || false,
16045
- createdAt: (0, import_firestore40.serverTimestamp)(),
16046
- updatedAt: (0, import_firestore40.serverTimestamp)(),
16047
- lastLoginAt: (0, import_firestore40.serverTimestamp)()
16048
- };
16049
- console.log("[AUTH] Attempting setDoc directly with userData:", {
16050
- uid: userData.uid,
16051
- email: userData.email,
16052
- roles: userData.roles
16053
- });
16054
- try {
16055
- await (0, import_firestore40.setDoc)((0, import_firestore40.doc)(this.db, USERS_COLLECTION, firebaseUser.uid), userData);
16056
- console.log("[AUTH] \u2705 setDoc SUCCEEDED directly!");
16057
- const userDoc = await (0, import_firestore40.getDoc)((0, import_firestore40.doc)(this.db, USERS_COLLECTION, firebaseUser.uid));
16058
- if (!userDoc.exists()) {
16059
- throw new Error("User document was not created");
16060
- }
16061
- user = userDoc.data();
16062
- console.log("[AUTH] User document created successfully:", user.uid);
16063
- } catch (error) {
16064
- console.error("[AUTH] \u274C setDoc FAILED directly:", {
16065
- errorCode: error == null ? void 0 : error.code,
16066
- errorMessage: error == null ? void 0 : error.message,
16067
- uid: firebaseUser.uid
16068
- });
16069
- throw error;
16070
- }
16021
+ console.error("[AUTH] \u274C User document should already exist! It should have been created in signUpPractitionerWithGoogle.");
16022
+ console.error("[AUTH] This indicates a bug - User doc creation failed in the initial Google sign-in flow.");
16023
+ throw new AuthError(
16024
+ "User account not properly initialized. Please try signing in again.",
16025
+ "AUTH/USER_NOT_INITIALIZED",
16026
+ 500
16027
+ );
16071
16028
  }
16072
16029
  let practitioner;
16073
16030
  if (practitionerIds.length === 1) {
@@ -16248,7 +16205,7 @@ var AuthService = class extends BaseService {
16248
16205
  * @returns Object containing user, practitioner (if exists), and draft profiles (if any)
16249
16206
  */
16250
16207
  async signUpPractitionerWithGoogle(idToken) {
16251
- var _a, _b;
16208
+ var _a, _b, _c;
16252
16209
  try {
16253
16210
  console.log("[AUTH] Starting practitioner Google Sign-In/Sign-Up");
16254
16211
  let email;
@@ -16313,14 +16270,31 @@ var AuthService = class extends BaseService {
16313
16270
  404
16314
16271
  );
16315
16272
  }
16316
- console.log("[AUTH] Draft profiles found, returning to client for selection");
16317
- const draftProfilesFound = await practitionerService2.getDraftProfilesByEmail(normalizedEmail);
16318
- return {
16319
- user: null,
16320
- // Will be created when claiming profiles
16321
- practitioner: null,
16322
- draftProfiles: draftProfilesFound
16323
- };
16273
+ console.log("[AUTH] Draft profiles found, creating User document IMMEDIATELY after sign-in");
16274
+ console.log("[AUTH] auth.currentUser at User creation time:", ((_b = this.auth.currentUser) == null ? void 0 : _b.uid) || "NULL");
16275
+ try {
16276
+ const newUser = await this.userService.createUser(firebaseUser2, ["practitioner" /* PRACTITIONER */], {
16277
+ skipProfileCreation: true
16278
+ });
16279
+ console.log("[AUTH] \u2705 User document created successfully:", newUser.uid);
16280
+ return {
16281
+ user: newUser,
16282
+ practitioner: null,
16283
+ draftProfiles: draftProfiles3
16284
+ };
16285
+ } catch (createUserError) {
16286
+ console.error("[AUTH] \u274C Failed to create User document:", {
16287
+ errorCode: createUserError == null ? void 0 : createUserError.code,
16288
+ errorMessage: createUserError == null ? void 0 : createUserError.message,
16289
+ uid: firebaseUser2.uid
16290
+ });
16291
+ try {
16292
+ await (0, import_auth8.signOut)(this.auth);
16293
+ } catch (signOutError) {
16294
+ console.warn("[AUTH] Error signing out Firebase user (non-critical):", signOutError);
16295
+ }
16296
+ throw createUserError;
16297
+ }
16324
16298
  }
16325
16299
  if (!existingUser2) {
16326
16300
  await (0, import_auth8.signOut)(this.auth);
@@ -16434,7 +16408,7 @@ var AuthService = class extends BaseService {
16434
16408
  };
16435
16409
  } catch (error) {
16436
16410
  console.error("[AUTH] Error in signUpPractitionerWithGoogle:", error);
16437
- console.error("[AUTH] Error type:", (_b = error == null ? void 0 : error.constructor) == null ? void 0 : _b.name);
16411
+ console.error("[AUTH] Error type:", (_c = error == null ? void 0 : error.constructor) == null ? void 0 : _c.name);
16438
16412
  console.error("[AUTH] Error instanceof AuthError:", error instanceof AuthError);
16439
16413
  console.error("[AUTH] Error code:", error == null ? void 0 : error.code);
16440
16414
  console.error("[AUTH] Error message:", error == null ? void 0 : error.message);
package/dist/index.mjs CHANGED
@@ -7122,13 +7122,9 @@ import {
7122
7122
  } from "firebase/auth";
7123
7123
  import {
7124
7124
  collection as collection21,
7125
- doc as doc25,
7126
- getDoc as getDoc27,
7127
- setDoc as setDoc16,
7128
7125
  query as query21,
7129
7126
  getDocs as getDocs21,
7130
- runTransaction,
7131
- serverTimestamp as serverTimestamp21
7127
+ runTransaction
7132
7128
  } from "firebase/firestore";
7133
7129
 
7134
7130
  // src/types/user/index.ts
@@ -16087,7 +16083,7 @@ var AuthService = class extends BaseService {
16087
16083
  * @returns Object containing user and claimed practitioner
16088
16084
  */
16089
16085
  async claimDraftProfilesWithGoogle(idToken, practitionerIds) {
16090
- var _a, _b, _c, _d, _e;
16086
+ var _a, _b;
16091
16087
  try {
16092
16088
  console.log("[AUTH] Starting claim draft profiles with Google", {
16093
16089
  practitionerIdsCount: practitionerIds.length,
@@ -16104,61 +16100,18 @@ var AuthService = class extends BaseService {
16104
16100
  console.log("[AUTH] currentUser IMMEDIATELY AFTER sign-in:", ((_b = this.auth.currentUser) == null ? void 0 : _b.uid) || "NULL");
16105
16101
  console.log("[AUTH] User returned from signInWithCredential:", firebaseUser.uid);
16106
16102
  const practitionerService = new PractitionerService(this.db, this.auth, this.app);
16107
- console.log("[AUTH] currentUser after intializing practitionerService:", ((_c = this.auth.currentUser) == null ? void 0 : _c.uid) || "NULL");
16108
- let user = null;
16103
+ let user;
16109
16104
  try {
16110
16105
  user = await this.userService.getUserById(firebaseUser.uid);
16111
- console.log("[AUTH] User document already exists:", user.uid);
16106
+ console.log("[AUTH] User document found:", user.uid);
16112
16107
  } catch (userError) {
16113
- console.log("[AUTH] User document does not exist, creating it...");
16114
- }
16115
- if (!user) {
16116
- console.log("[AUTH] Creating user document DIRECTLY for:", firebaseUser.uid);
16117
- console.log("[AUTH] Checking auth.currentUser BEFORE refresh:", ((_d = this.auth.currentUser) == null ? void 0 : _d.uid) || "NULL");
16118
- try {
16119
- const token = await firebaseUser.getIdToken(true);
16120
- console.log("[AUTH] Got ID token, length:", (token == null ? void 0 : token.length) || 0);
16121
- } catch (tokenError) {
16122
- console.error("[AUTH] Error getting token:", tokenError);
16123
- }
16124
- await new Promise((resolve) => setTimeout(resolve, 100));
16125
- console.log("[AUTH] Using THIS.auth.currentUser AFTER refresh:", ((_e = this.auth.currentUser) == null ? void 0 : _e.uid) || "NULL");
16126
- console.log("[AUTH] Using THIS.db:", this.db ? "EXISTS" : "NULL");
16127
- if (!this.auth.currentUser) {
16128
- console.error("[AUTH] \u26A0\uFE0F auth.currentUser is STILL NULL after refresh! This is a React Native Firebase JS SDK issue.");
16129
- console.error("[AUTH] Firestore will deny permission because request.auth will be null.");
16130
- }
16131
- const userData = {
16132
- uid: firebaseUser.uid,
16133
- email: firebaseUser.email,
16134
- roles: ["practitioner" /* PRACTITIONER */],
16135
- isAnonymous: firebaseUser.isAnonymous || false,
16136
- createdAt: serverTimestamp21(),
16137
- updatedAt: serverTimestamp21(),
16138
- lastLoginAt: serverTimestamp21()
16139
- };
16140
- console.log("[AUTH] Attempting setDoc directly with userData:", {
16141
- uid: userData.uid,
16142
- email: userData.email,
16143
- roles: userData.roles
16144
- });
16145
- try {
16146
- await setDoc16(doc25(this.db, USERS_COLLECTION, firebaseUser.uid), userData);
16147
- console.log("[AUTH] \u2705 setDoc SUCCEEDED directly!");
16148
- const userDoc = await getDoc27(doc25(this.db, USERS_COLLECTION, firebaseUser.uid));
16149
- if (!userDoc.exists()) {
16150
- throw new Error("User document was not created");
16151
- }
16152
- user = userDoc.data();
16153
- console.log("[AUTH] User document created successfully:", user.uid);
16154
- } catch (error) {
16155
- console.error("[AUTH] \u274C setDoc FAILED directly:", {
16156
- errorCode: error == null ? void 0 : error.code,
16157
- errorMessage: error == null ? void 0 : error.message,
16158
- uid: firebaseUser.uid
16159
- });
16160
- throw error;
16161
- }
16108
+ console.error("[AUTH] \u274C User document should already exist! It should have been created in signUpPractitionerWithGoogle.");
16109
+ console.error("[AUTH] This indicates a bug - User doc creation failed in the initial Google sign-in flow.");
16110
+ throw new AuthError(
16111
+ "User account not properly initialized. Please try signing in again.",
16112
+ "AUTH/USER_NOT_INITIALIZED",
16113
+ 500
16114
+ );
16162
16115
  }
16163
16116
  let practitioner;
16164
16117
  if (practitionerIds.length === 1) {
@@ -16339,7 +16292,7 @@ var AuthService = class extends BaseService {
16339
16292
  * @returns Object containing user, practitioner (if exists), and draft profiles (if any)
16340
16293
  */
16341
16294
  async signUpPractitionerWithGoogle(idToken) {
16342
- var _a, _b;
16295
+ var _a, _b, _c;
16343
16296
  try {
16344
16297
  console.log("[AUTH] Starting practitioner Google Sign-In/Sign-Up");
16345
16298
  let email;
@@ -16404,14 +16357,31 @@ var AuthService = class extends BaseService {
16404
16357
  404
16405
16358
  );
16406
16359
  }
16407
- console.log("[AUTH] Draft profiles found, returning to client for selection");
16408
- const draftProfilesFound = await practitionerService2.getDraftProfilesByEmail(normalizedEmail);
16409
- return {
16410
- user: null,
16411
- // Will be created when claiming profiles
16412
- practitioner: null,
16413
- draftProfiles: draftProfilesFound
16414
- };
16360
+ console.log("[AUTH] Draft profiles found, creating User document IMMEDIATELY after sign-in");
16361
+ console.log("[AUTH] auth.currentUser at User creation time:", ((_b = this.auth.currentUser) == null ? void 0 : _b.uid) || "NULL");
16362
+ try {
16363
+ const newUser = await this.userService.createUser(firebaseUser2, ["practitioner" /* PRACTITIONER */], {
16364
+ skipProfileCreation: true
16365
+ });
16366
+ console.log("[AUTH] \u2705 User document created successfully:", newUser.uid);
16367
+ return {
16368
+ user: newUser,
16369
+ practitioner: null,
16370
+ draftProfiles: draftProfiles3
16371
+ };
16372
+ } catch (createUserError) {
16373
+ console.error("[AUTH] \u274C Failed to create User document:", {
16374
+ errorCode: createUserError == null ? void 0 : createUserError.code,
16375
+ errorMessage: createUserError == null ? void 0 : createUserError.message,
16376
+ uid: firebaseUser2.uid
16377
+ });
16378
+ try {
16379
+ await firebaseSignOut(this.auth);
16380
+ } catch (signOutError) {
16381
+ console.warn("[AUTH] Error signing out Firebase user (non-critical):", signOutError);
16382
+ }
16383
+ throw createUserError;
16384
+ }
16415
16385
  }
16416
16386
  if (!existingUser2) {
16417
16387
  await firebaseSignOut(this.auth);
@@ -16525,7 +16495,7 @@ var AuthService = class extends BaseService {
16525
16495
  };
16526
16496
  } catch (error) {
16527
16497
  console.error("[AUTH] Error in signUpPractitionerWithGoogle:", error);
16528
- console.error("[AUTH] Error type:", (_b = error == null ? void 0 : error.constructor) == null ? void 0 : _b.name);
16498
+ console.error("[AUTH] Error type:", (_c = error == null ? void 0 : error.constructor) == null ? void 0 : _c.name);
16529
16499
  console.error("[AUTH] Error instanceof AuthError:", error instanceof AuthError);
16530
16500
  console.error("[AUTH] Error code:", error == null ? void 0 : error.code);
16531
16501
  console.error("[AUTH] Error message:", error == null ? void 0 : error.message);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@blackcode_sa/metaestetics-api",
3
3
  "private": false,
4
- "version": "1.14.14",
4
+ "version": "1.14.15",
5
5
  "description": "Firebase authentication service with anonymous upgrade support",
6
6
  "main": "dist/index.js",
7
7
  "module": "dist/index.mjs",
@@ -900,81 +900,22 @@ export class AuthService extends BaseService {
900
900
  console.log('[AUTH] User returned from signInWithCredential:', firebaseUser.uid);
901
901
 
902
902
  const practitionerService = new PractitionerService(this.db, this.auth, this.app);
903
- console.log('[AUTH] currentUser after intializing practitionerService:', this.auth.currentUser?.uid || 'NULL');
904
903
 
905
- // Step 1: Check if User document already exists
906
- let user: User | null = null;
904
+ // Step 1: Get User document (should already exist from signUpPractitionerWithGoogle)
905
+ // The User document is created IMMEDIATELY after Google sign-in in signUpPractitionerWithGoogle
906
+ // This matches the token flow pattern - no delays between sign-in and User doc creation
907
+ let user: User;
907
908
  try {
908
909
  user = await this.userService.getUserById(firebaseUser.uid);
909
- console.log('[AUTH] User document already exists:', user.uid);
910
+ console.log('[AUTH] User document found:', user.uid);
910
911
  } catch (userError) {
911
- console.log('[AUTH] User document does not exist, creating it...');
912
- }
913
-
914
- // Step 2: Create User document directly (bypassing UserService to test auth instance issue)
915
- if (!user) {
916
- console.log('[AUTH] Creating user document DIRECTLY for:', firebaseUser.uid);
917
-
918
- // CRITICAL: Force auth state refresh right before setDoc
919
- // React Native Firebase JS SDK has issues where auth.currentUser becomes NULL
920
- // We need to ensure it's set before Firestore operations
921
- console.log('[AUTH] Checking auth.currentUser BEFORE refresh:', this.auth.currentUser?.uid || 'NULL');
922
-
923
- // Force token refresh to ensure auth state is set
924
- try {
925
- const token = await firebaseUser.getIdToken(true);
926
- console.log('[AUTH] Got ID token, length:', token?.length || 0);
927
- } catch (tokenError) {
928
- console.error('[AUTH] Error getting token:', tokenError);
929
- }
930
-
931
- // Wait a tiny bit for auth state to propagate
932
- await new Promise(resolve => setTimeout(resolve, 100));
933
-
934
- console.log('[AUTH] Using THIS.auth.currentUser AFTER refresh:', this.auth.currentUser?.uid || 'NULL');
935
- console.log('[AUTH] Using THIS.db:', this.db ? 'EXISTS' : 'NULL');
936
-
937
- // If still NULL, this is a React Native Firebase JS SDK bug
938
- if (!this.auth.currentUser) {
939
- console.error('[AUTH] ⚠️ auth.currentUser is STILL NULL after refresh! This is a React Native Firebase JS SDK issue.');
940
- console.error('[AUTH] Firestore will deny permission because request.auth will be null.');
941
- }
942
-
943
- const userData = {
944
- uid: firebaseUser.uid,
945
- email: firebaseUser.email,
946
- roles: [UserRole.PRACTITIONER],
947
- isAnonymous: firebaseUser.isAnonymous || false,
948
- createdAt: serverTimestamp(),
949
- updatedAt: serverTimestamp(),
950
- lastLoginAt: serverTimestamp(),
951
- };
952
-
953
- console.log('[AUTH] Attempting setDoc directly with userData:', {
954
- uid: userData.uid,
955
- email: userData.email,
956
- roles: userData.roles,
957
- });
958
-
959
- try {
960
- await setDoc(doc(this.db, USERS_COLLECTION, firebaseUser.uid), userData);
961
- console.log('[AUTH] ✅ setDoc SUCCEEDED directly!');
962
-
963
- // Fetch the created user
964
- const userDoc = await getDoc(doc(this.db, USERS_COLLECTION, firebaseUser.uid));
965
- if (!userDoc.exists()) {
966
- throw new Error('User document was not created');
967
- }
968
- user = userDoc.data() as User;
969
- console.log('[AUTH] User document created successfully:', user.uid);
970
- } catch (error: any) {
971
- console.error('[AUTH] ❌ setDoc FAILED directly:', {
972
- errorCode: error?.code,
973
- errorMessage: error?.message,
974
- uid: firebaseUser.uid,
975
- });
976
- throw error;
977
- }
912
+ console.error('[AUTH] User document should already exist! It should have been created in signUpPractitionerWithGoogle.');
913
+ console.error('[AUTH] This indicates a bug - User doc creation failed in the initial Google sign-in flow.');
914
+ throw new AuthError(
915
+ 'User account not properly initialized. Please try signing in again.',
916
+ 'AUTH/USER_NOT_INITIALIZED',
917
+ 500,
918
+ );
978
919
  }
979
920
 
980
921
  // Step 3: Claim the draft profiles
@@ -1308,19 +1249,38 @@ export class AuthService extends BaseService {
1308
1249
  );
1309
1250
  }
1310
1251
 
1311
- // Draft profiles exist - return them to client
1312
- // User document creation will happen server-side when claiming profiles
1313
- // This avoids permission issues with client-side User document creation
1314
- console.log('[AUTH] Draft profiles found, returning to client for selection');
1315
- const draftProfilesFound = await practitionerService.getDraftProfilesByEmail(normalizedEmail);
1252
+ // Draft profiles exist - CREATE USER DOCUMENT IMMEDIATELY (like token flow)
1253
+ // This matches the token flow pattern: create User doc right after sign-in, before any delays
1254
+ console.log('[AUTH] Draft profiles found, creating User document IMMEDIATELY after sign-in');
1255
+ console.log('[AUTH] auth.currentUser at User creation time:', this.auth.currentUser?.uid || 'NULL');
1316
1256
 
1317
- // Return draft profiles without creating User document
1318
- // The User document will be created server-side via Cloud Function when claiming profiles
1319
- return {
1320
- user: null, // Will be created when claiming profiles
1321
- practitioner: null,
1322
- draftProfiles: draftProfilesFound,
1323
- };
1257
+ try {
1258
+ // Create User document immediately (same pattern as token flow)
1259
+ const newUser = await this.userService.createUser(firebaseUser, [UserRole.PRACTITIONER], {
1260
+ skipProfileCreation: true,
1261
+ });
1262
+ console.log('[AUTH] ✅ User document created successfully:', newUser.uid);
1263
+
1264
+ // Return user + draft profiles (user doc already exists, just need to claim profiles)
1265
+ return {
1266
+ user: newUser,
1267
+ practitioner: null,
1268
+ draftProfiles: draftProfiles,
1269
+ };
1270
+ } catch (createUserError: any) {
1271
+ console.error('[AUTH] ❌ Failed to create User document:', {
1272
+ errorCode: createUserError?.code,
1273
+ errorMessage: createUserError?.message,
1274
+ uid: firebaseUser.uid,
1275
+ });
1276
+ // Sign out on failure
1277
+ try {
1278
+ await firebaseSignOut(this.auth);
1279
+ } catch (signOutError) {
1280
+ console.warn('[AUTH] Error signing out Firebase user (non-critical):', signOutError);
1281
+ }
1282
+ throw createUserError;
1283
+ }
1324
1284
  }
1325
1285
 
1326
1286
  // User document exists - check for practitioner profile and draft profiles