@blackcode_sa/metaestetics-api 1.14.13 → 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;
15999
+ var _a, _b;
16000
16000
  try {
16001
16001
  console.log("[AUTH] Starting claim draft profiles with Google", {
16002
16002
  practitionerIdsCount: practitionerIds.length,
@@ -16013,49 +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] Using THIS.auth.currentUser:", ((_d = this.auth.currentUser) == null ? void 0 : _d.uid) || "NULL");
16027
- console.log("[AUTH] Using THIS.db:", this.db ? "EXISTS" : "NULL");
16028
- const userData = {
16029
- uid: firebaseUser.uid,
16030
- email: firebaseUser.email,
16031
- roles: ["practitioner" /* PRACTITIONER */],
16032
- isAnonymous: firebaseUser.isAnonymous || false,
16033
- createdAt: (0, import_firestore40.serverTimestamp)(),
16034
- updatedAt: (0, import_firestore40.serverTimestamp)(),
16035
- lastLoginAt: (0, import_firestore40.serverTimestamp)()
16036
- };
16037
- console.log("[AUTH] Attempting setDoc directly with userData:", {
16038
- uid: userData.uid,
16039
- email: userData.email,
16040
- roles: userData.roles
16041
- });
16042
- try {
16043
- await (0, import_firestore40.setDoc)((0, import_firestore40.doc)(this.db, USERS_COLLECTION, firebaseUser.uid), userData);
16044
- console.log("[AUTH] \u2705 setDoc SUCCEEDED directly!");
16045
- const userDoc = await (0, import_firestore40.getDoc)((0, import_firestore40.doc)(this.db, USERS_COLLECTION, firebaseUser.uid));
16046
- if (!userDoc.exists()) {
16047
- throw new Error("User document was not created");
16048
- }
16049
- user = userDoc.data();
16050
- console.log("[AUTH] User document created successfully:", user.uid);
16051
- } catch (error) {
16052
- console.error("[AUTH] \u274C setDoc FAILED directly:", {
16053
- errorCode: error == null ? void 0 : error.code,
16054
- errorMessage: error == null ? void 0 : error.message,
16055
- uid: firebaseUser.uid
16056
- });
16057
- throw error;
16058
- }
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
+ );
16059
16028
  }
16060
16029
  let practitioner;
16061
16030
  if (practitionerIds.length === 1) {
@@ -16236,7 +16205,7 @@ var AuthService = class extends BaseService {
16236
16205
  * @returns Object containing user, practitioner (if exists), and draft profiles (if any)
16237
16206
  */
16238
16207
  async signUpPractitionerWithGoogle(idToken) {
16239
- var _a, _b;
16208
+ var _a, _b, _c;
16240
16209
  try {
16241
16210
  console.log("[AUTH] Starting practitioner Google Sign-In/Sign-Up");
16242
16211
  let email;
@@ -16301,14 +16270,31 @@ var AuthService = class extends BaseService {
16301
16270
  404
16302
16271
  );
16303
16272
  }
16304
- console.log("[AUTH] Draft profiles found, returning to client for selection");
16305
- const draftProfilesFound = await practitionerService2.getDraftProfilesByEmail(normalizedEmail);
16306
- return {
16307
- user: null,
16308
- // Will be created when claiming profiles
16309
- practitioner: null,
16310
- draftProfiles: draftProfilesFound
16311
- };
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
+ }
16312
16298
  }
16313
16299
  if (!existingUser2) {
16314
16300
  await (0, import_auth8.signOut)(this.auth);
@@ -16422,7 +16408,7 @@ var AuthService = class extends BaseService {
16422
16408
  };
16423
16409
  } catch (error) {
16424
16410
  console.error("[AUTH] Error in signUpPractitionerWithGoogle:", error);
16425
- 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);
16426
16412
  console.error("[AUTH] Error instanceof AuthError:", error instanceof AuthError);
16427
16413
  console.error("[AUTH] Error code:", error == null ? void 0 : error.code);
16428
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;
16086
+ var _a, _b;
16091
16087
  try {
16092
16088
  console.log("[AUTH] Starting claim draft profiles with Google", {
16093
16089
  practitionerIdsCount: practitionerIds.length,
@@ -16104,49 +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] Using THIS.auth.currentUser:", ((_d = this.auth.currentUser) == null ? void 0 : _d.uid) || "NULL");
16118
- console.log("[AUTH] Using THIS.db:", this.db ? "EXISTS" : "NULL");
16119
- const userData = {
16120
- uid: firebaseUser.uid,
16121
- email: firebaseUser.email,
16122
- roles: ["practitioner" /* PRACTITIONER */],
16123
- isAnonymous: firebaseUser.isAnonymous || false,
16124
- createdAt: serverTimestamp21(),
16125
- updatedAt: serverTimestamp21(),
16126
- lastLoginAt: serverTimestamp21()
16127
- };
16128
- console.log("[AUTH] Attempting setDoc directly with userData:", {
16129
- uid: userData.uid,
16130
- email: userData.email,
16131
- roles: userData.roles
16132
- });
16133
- try {
16134
- await setDoc16(doc25(this.db, USERS_COLLECTION, firebaseUser.uid), userData);
16135
- console.log("[AUTH] \u2705 setDoc SUCCEEDED directly!");
16136
- const userDoc = await getDoc27(doc25(this.db, USERS_COLLECTION, firebaseUser.uid));
16137
- if (!userDoc.exists()) {
16138
- throw new Error("User document was not created");
16139
- }
16140
- user = userDoc.data();
16141
- console.log("[AUTH] User document created successfully:", user.uid);
16142
- } catch (error) {
16143
- console.error("[AUTH] \u274C setDoc FAILED directly:", {
16144
- errorCode: error == null ? void 0 : error.code,
16145
- errorMessage: error == null ? void 0 : error.message,
16146
- uid: firebaseUser.uid
16147
- });
16148
- throw error;
16149
- }
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
+ );
16150
16115
  }
16151
16116
  let practitioner;
16152
16117
  if (practitionerIds.length === 1) {
@@ -16327,7 +16292,7 @@ var AuthService = class extends BaseService {
16327
16292
  * @returns Object containing user, practitioner (if exists), and draft profiles (if any)
16328
16293
  */
16329
16294
  async signUpPractitionerWithGoogle(idToken) {
16330
- var _a, _b;
16295
+ var _a, _b, _c;
16331
16296
  try {
16332
16297
  console.log("[AUTH] Starting practitioner Google Sign-In/Sign-Up");
16333
16298
  let email;
@@ -16392,14 +16357,31 @@ var AuthService = class extends BaseService {
16392
16357
  404
16393
16358
  );
16394
16359
  }
16395
- console.log("[AUTH] Draft profiles found, returning to client for selection");
16396
- const draftProfilesFound = await practitionerService2.getDraftProfilesByEmail(normalizedEmail);
16397
- return {
16398
- user: null,
16399
- // Will be created when claiming profiles
16400
- practitioner: null,
16401
- draftProfiles: draftProfilesFound
16402
- };
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
+ }
16403
16385
  }
16404
16386
  if (!existingUser2) {
16405
16387
  await firebaseSignOut(this.auth);
@@ -16513,7 +16495,7 @@ var AuthService = class extends BaseService {
16513
16495
  };
16514
16496
  } catch (error) {
16515
16497
  console.error("[AUTH] Error in signUpPractitionerWithGoogle:", error);
16516
- 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);
16517
16499
  console.error("[AUTH] Error instanceof AuthError:", error instanceof AuthError);
16518
16500
  console.error("[AUTH] Error code:", error == null ? void 0 : error.code);
16519
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.13",
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,58 +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
- console.log('[AUTH] Using THIS.auth.currentUser:', this.auth.currentUser?.uid || 'NULL');
918
- console.log('[AUTH] Using THIS.db:', this.db ? 'EXISTS' : 'NULL');
919
-
920
- const userData = {
921
- uid: firebaseUser.uid,
922
- email: firebaseUser.email,
923
- roles: [UserRole.PRACTITIONER],
924
- isAnonymous: firebaseUser.isAnonymous || false,
925
- createdAt: serverTimestamp(),
926
- updatedAt: serverTimestamp(),
927
- lastLoginAt: serverTimestamp(),
928
- };
929
-
930
- console.log('[AUTH] Attempting setDoc directly with userData:', {
931
- uid: userData.uid,
932
- email: userData.email,
933
- roles: userData.roles,
934
- });
935
-
936
- try {
937
- await setDoc(doc(this.db, USERS_COLLECTION, firebaseUser.uid), userData);
938
- console.log('[AUTH] ✅ setDoc SUCCEEDED directly!');
939
-
940
- // Fetch the created user
941
- const userDoc = await getDoc(doc(this.db, USERS_COLLECTION, firebaseUser.uid));
942
- if (!userDoc.exists()) {
943
- throw new Error('User document was not created');
944
- }
945
- user = userDoc.data() as User;
946
- console.log('[AUTH] User document created successfully:', user.uid);
947
- } catch (error: any) {
948
- console.error('[AUTH] ❌ setDoc FAILED directly:', {
949
- errorCode: error?.code,
950
- errorMessage: error?.message,
951
- uid: firebaseUser.uid,
952
- });
953
- throw error;
954
- }
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
+ );
955
919
  }
956
920
 
957
921
  // Step 3: Claim the draft profiles
@@ -1285,19 +1249,38 @@ export class AuthService extends BaseService {
1285
1249
  );
1286
1250
  }
1287
1251
 
1288
- // Draft profiles exist - return them to client
1289
- // User document creation will happen server-side when claiming profiles
1290
- // This avoids permission issues with client-side User document creation
1291
- console.log('[AUTH] Draft profiles found, returning to client for selection');
1292
- 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');
1293
1256
 
1294
- // Return draft profiles without creating User document
1295
- // The User document will be created server-side via Cloud Function when claiming profiles
1296
- return {
1297
- user: null, // Will be created when claiming profiles
1298
- practitioner: null,
1299
- draftProfiles: draftProfilesFound,
1300
- };
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
+ }
1301
1284
  }
1302
1285
 
1303
1286
  // User document exists - check for practitioner profile and draft profiles