@blackcode_sa/metaestetics-api 1.14.4 → 1.14.5

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.d.mts CHANGED
@@ -8412,9 +8412,9 @@ declare class AuthService extends BaseService {
8412
8412
  }>;
8413
8413
  /**
8414
8414
  * Claims draft practitioner profiles after Google Sign-In.
8415
- * Similar to the token flow - creates User document and claims profiles in one transaction.
8415
+ * Uses the current authenticated user (from initial Google Sign-In).
8416
8416
  *
8417
- * @param idToken - The Google ID token
8417
+ * @param idToken - The Google ID token (used to re-authenticate if needed)
8418
8418
  * @param practitionerIds - Array of draft practitioner profile IDs to claim
8419
8419
  * @returns Object containing user and claimed practitioner
8420
8420
  */
package/dist/index.d.ts CHANGED
@@ -8412,9 +8412,9 @@ declare class AuthService extends BaseService {
8412
8412
  }>;
8413
8413
  /**
8414
8414
  * Claims draft practitioner profiles after Google Sign-In.
8415
- * Similar to the token flow - creates User document and claims profiles in one transaction.
8415
+ * Uses the current authenticated user (from initial Google Sign-In).
8416
8416
  *
8417
- * @param idToken - The Google ID token
8417
+ * @param idToken - The Google ID token (used to re-authenticate if needed)
8418
8418
  * @param practitionerIds - Array of draft practitioner profile IDs to claim
8419
8419
  * @returns Object containing user and claimed practitioner
8420
8420
  */
package/dist/index.js CHANGED
@@ -15910,9 +15910,9 @@ var AuthService = class extends BaseService {
15910
15910
  }
15911
15911
  /**
15912
15912
  * Claims draft practitioner profiles after Google Sign-In.
15913
- * Similar to the token flow - creates User document and claims profiles in one transaction.
15913
+ * Uses the current authenticated user (from initial Google Sign-In).
15914
15914
  *
15915
- * @param idToken - The Google ID token
15915
+ * @param idToken - The Google ID token (used to re-authenticate if needed)
15916
15916
  * @param practitionerIds - Array of draft practitioner profile IDs to claim
15917
15917
  * @returns Object containing user and claimed practitioner
15918
15918
  */
@@ -15925,60 +15925,61 @@ var AuthService = class extends BaseService {
15925
15925
  if (practitionerIds.length === 0) {
15926
15926
  throw new AuthError("No practitioner profiles selected to claim", "AUTH/NO_PROFILES_SELECTED", 400);
15927
15927
  }
15928
- const credential = import_auth8.GoogleAuthProvider.credential(idToken);
15929
- const { user: firebaseUser } = await (0, import_auth8.signInWithCredential)(this.auth, credential);
15930
- console.log("[AUTH] Signed in with Google:", firebaseUser.uid);
15928
+ let firebaseUser = this.auth.currentUser;
15929
+ if (!firebaseUser) {
15930
+ console.log("[AUTH] No current user, re-authenticating with Google credential");
15931
+ const credential = import_auth8.GoogleAuthProvider.credential(idToken);
15932
+ const result = await (0, import_auth8.signInWithCredential)(this.auth, credential);
15933
+ firebaseUser = result.user;
15934
+ }
15935
+ console.log("[AUTH] Using Firebase user:", firebaseUser.uid);
15936
+ console.log("[AUTH] Forcing token refresh...");
15937
+ await firebaseUser.getIdToken(true);
15938
+ console.log("[AUTH] Token refreshed successfully");
15931
15939
  const practitionerService = new PractitionerService(this.db, this.auth, this.app);
15932
- let existingUser = null;
15940
+ let user = null;
15933
15941
  try {
15934
- existingUser = await this.userService.getUserById(firebaseUser.uid);
15935
- console.log("[AUTH] User document already exists:", existingUser.uid);
15942
+ user = await this.userService.getUserById(firebaseUser.uid);
15943
+ console.log("[AUTH] User document already exists:", user.uid);
15936
15944
  } catch (userError) {
15937
- console.log("[AUTH] User document does not exist, will create it");
15945
+ console.log("[AUTH] User document does not exist, creating it...");
15938
15946
  }
15939
- const transactionResult = await (0, import_firestore40.runTransaction)(this.db, async () => {
15940
- console.log("[AUTH] Transaction started - creating user and claiming profiles");
15941
- let user;
15942
- if (existingUser) {
15943
- user = existingUser;
15944
- } else {
15945
- console.log("[AUTH] Creating user document");
15946
- user = await this.userService.createUser(firebaseUser, ["practitioner" /* PRACTITIONER */], {
15947
- skipProfileCreation: true
15948
- });
15949
- console.log("[AUTH] User document created:", user.uid);
15950
- }
15951
- let practitioner;
15952
- if (practitionerIds.length === 1) {
15953
- console.log("[AUTH] Claiming single draft profile");
15954
- practitioner = await practitionerService.claimDraftProfileWithGoogle(
15955
- practitionerIds[0],
15956
- firebaseUser.uid
15957
- );
15958
- } else {
15959
- console.log("[AUTH] Claiming multiple draft profiles");
15960
- practitioner = await practitionerService.claimMultipleDraftProfilesWithGoogle(
15961
- practitionerIds,
15962
- firebaseUser.uid
15963
- );
15964
- }
15965
- if (!user.practitionerProfile || user.practitionerProfile !== practitioner.id) {
15966
- console.log("[AUTH] Linking practitioner to user");
15967
- await this.userService.updateUser(firebaseUser.uid, {
15968
- practitionerProfile: practitioner.id
15969
- });
15970
- }
15971
- console.log("[AUTH] Transaction completed successfully");
15972
- return { user, practitioner };
15973
- });
15947
+ if (!user) {
15948
+ console.log("[AUTH] Creating user document for:", firebaseUser.uid);
15949
+ user = await this.userService.createUser(firebaseUser, ["practitioner" /* PRACTITIONER */], {
15950
+ skipProfileCreation: true
15951
+ });
15952
+ console.log("[AUTH] User document created successfully:", user.uid);
15953
+ }
15954
+ let practitioner;
15955
+ if (practitionerIds.length === 1) {
15956
+ console.log("[AUTH] Claiming single draft profile:", practitionerIds[0]);
15957
+ practitioner = await practitionerService.claimDraftProfileWithGoogle(
15958
+ practitionerIds[0],
15959
+ firebaseUser.uid
15960
+ );
15961
+ } else {
15962
+ console.log("[AUTH] Claiming multiple draft profiles:", practitionerIds);
15963
+ practitioner = await practitionerService.claimMultipleDraftProfilesWithGoogle(
15964
+ practitionerIds,
15965
+ firebaseUser.uid
15966
+ );
15967
+ }
15968
+ console.log("[AUTH] Draft profiles claimed:", practitioner.id);
15969
+ if (!user.practitionerProfile || user.practitionerProfile !== practitioner.id) {
15970
+ console.log("[AUTH] Linking practitioner to user");
15971
+ await this.userService.updateUser(firebaseUser.uid, {
15972
+ practitionerProfile: practitioner.id
15973
+ });
15974
+ }
15975
+ const updatedUser = await this.userService.getUserById(firebaseUser.uid);
15974
15976
  console.log("[AUTH] Draft profiles claimed successfully", {
15975
- userId: transactionResult.user.uid,
15976
- practitionerId: transactionResult.practitioner.id
15977
+ userId: updatedUser.uid,
15978
+ practitionerId: practitioner.id
15977
15979
  });
15978
- const updatedUser = await this.userService.getUserById(firebaseUser.uid);
15979
15980
  return {
15980
15981
  user: updatedUser,
15981
- practitioner: transactionResult.practitioner
15982
+ practitioner
15982
15983
  };
15983
15984
  } catch (error) {
15984
15985
  console.error("[AUTH] Error claiming draft profiles with Google:", error);
package/dist/index.mjs CHANGED
@@ -15997,9 +15997,9 @@ var AuthService = class extends BaseService {
15997
15997
  }
15998
15998
  /**
15999
15999
  * Claims draft practitioner profiles after Google Sign-In.
16000
- * Similar to the token flow - creates User document and claims profiles in one transaction.
16000
+ * Uses the current authenticated user (from initial Google Sign-In).
16001
16001
  *
16002
- * @param idToken - The Google ID token
16002
+ * @param idToken - The Google ID token (used to re-authenticate if needed)
16003
16003
  * @param practitionerIds - Array of draft practitioner profile IDs to claim
16004
16004
  * @returns Object containing user and claimed practitioner
16005
16005
  */
@@ -16012,60 +16012,61 @@ var AuthService = class extends BaseService {
16012
16012
  if (practitionerIds.length === 0) {
16013
16013
  throw new AuthError("No practitioner profiles selected to claim", "AUTH/NO_PROFILES_SELECTED", 400);
16014
16014
  }
16015
- const credential = GoogleAuthProvider.credential(idToken);
16016
- const { user: firebaseUser } = await signInWithCredential(this.auth, credential);
16017
- console.log("[AUTH] Signed in with Google:", firebaseUser.uid);
16015
+ let firebaseUser = this.auth.currentUser;
16016
+ if (!firebaseUser) {
16017
+ console.log("[AUTH] No current user, re-authenticating with Google credential");
16018
+ const credential = GoogleAuthProvider.credential(idToken);
16019
+ const result = await signInWithCredential(this.auth, credential);
16020
+ firebaseUser = result.user;
16021
+ }
16022
+ console.log("[AUTH] Using Firebase user:", firebaseUser.uid);
16023
+ console.log("[AUTH] Forcing token refresh...");
16024
+ await firebaseUser.getIdToken(true);
16025
+ console.log("[AUTH] Token refreshed successfully");
16018
16026
  const practitionerService = new PractitionerService(this.db, this.auth, this.app);
16019
- let existingUser = null;
16027
+ let user = null;
16020
16028
  try {
16021
- existingUser = await this.userService.getUserById(firebaseUser.uid);
16022
- console.log("[AUTH] User document already exists:", existingUser.uid);
16029
+ user = await this.userService.getUserById(firebaseUser.uid);
16030
+ console.log("[AUTH] User document already exists:", user.uid);
16023
16031
  } catch (userError) {
16024
- console.log("[AUTH] User document does not exist, will create it");
16032
+ console.log("[AUTH] User document does not exist, creating it...");
16025
16033
  }
16026
- const transactionResult = await runTransaction(this.db, async () => {
16027
- console.log("[AUTH] Transaction started - creating user and claiming profiles");
16028
- let user;
16029
- if (existingUser) {
16030
- user = existingUser;
16031
- } else {
16032
- console.log("[AUTH] Creating user document");
16033
- user = await this.userService.createUser(firebaseUser, ["practitioner" /* PRACTITIONER */], {
16034
- skipProfileCreation: true
16035
- });
16036
- console.log("[AUTH] User document created:", user.uid);
16037
- }
16038
- let practitioner;
16039
- if (practitionerIds.length === 1) {
16040
- console.log("[AUTH] Claiming single draft profile");
16041
- practitioner = await practitionerService.claimDraftProfileWithGoogle(
16042
- practitionerIds[0],
16043
- firebaseUser.uid
16044
- );
16045
- } else {
16046
- console.log("[AUTH] Claiming multiple draft profiles");
16047
- practitioner = await practitionerService.claimMultipleDraftProfilesWithGoogle(
16048
- practitionerIds,
16049
- firebaseUser.uid
16050
- );
16051
- }
16052
- if (!user.practitionerProfile || user.practitionerProfile !== practitioner.id) {
16053
- console.log("[AUTH] Linking practitioner to user");
16054
- await this.userService.updateUser(firebaseUser.uid, {
16055
- practitionerProfile: practitioner.id
16056
- });
16057
- }
16058
- console.log("[AUTH] Transaction completed successfully");
16059
- return { user, practitioner };
16060
- });
16034
+ if (!user) {
16035
+ console.log("[AUTH] Creating user document for:", firebaseUser.uid);
16036
+ user = await this.userService.createUser(firebaseUser, ["practitioner" /* PRACTITIONER */], {
16037
+ skipProfileCreation: true
16038
+ });
16039
+ console.log("[AUTH] User document created successfully:", user.uid);
16040
+ }
16041
+ let practitioner;
16042
+ if (practitionerIds.length === 1) {
16043
+ console.log("[AUTH] Claiming single draft profile:", practitionerIds[0]);
16044
+ practitioner = await practitionerService.claimDraftProfileWithGoogle(
16045
+ practitionerIds[0],
16046
+ firebaseUser.uid
16047
+ );
16048
+ } else {
16049
+ console.log("[AUTH] Claiming multiple draft profiles:", practitionerIds);
16050
+ practitioner = await practitionerService.claimMultipleDraftProfilesWithGoogle(
16051
+ practitionerIds,
16052
+ firebaseUser.uid
16053
+ );
16054
+ }
16055
+ console.log("[AUTH] Draft profiles claimed:", practitioner.id);
16056
+ if (!user.practitionerProfile || user.practitionerProfile !== practitioner.id) {
16057
+ console.log("[AUTH] Linking practitioner to user");
16058
+ await this.userService.updateUser(firebaseUser.uid, {
16059
+ practitionerProfile: practitioner.id
16060
+ });
16061
+ }
16062
+ const updatedUser = await this.userService.getUserById(firebaseUser.uid);
16061
16063
  console.log("[AUTH] Draft profiles claimed successfully", {
16062
- userId: transactionResult.user.uid,
16063
- practitionerId: transactionResult.practitioner.id
16064
+ userId: updatedUser.uid,
16065
+ practitionerId: practitioner.id
16064
16066
  });
16065
- const updatedUser = await this.userService.getUserById(firebaseUser.uid);
16066
16067
  return {
16067
16068
  user: updatedUser,
16068
- practitioner: transactionResult.practitioner
16069
+ practitioner
16069
16070
  };
16070
16071
  } catch (error) {
16071
16072
  console.error("[AUTH] Error claiming draft profiles with Google:", error);
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.4",
4
+ "version": "1.14.5",
5
5
  "description": "Firebase authentication service with anonymous upgrade support",
6
6
  "main": "dist/index.js",
7
7
  "module": "dist/index.mjs",
@@ -813,9 +813,9 @@ export class AuthService extends BaseService {
813
813
 
814
814
  /**
815
815
  * Claims draft practitioner profiles after Google Sign-In.
816
- * Similar to the token flow - creates User document and claims profiles in one transaction.
816
+ * Uses the current authenticated user (from initial Google Sign-In).
817
817
  *
818
- * @param idToken - The Google ID token
818
+ * @param idToken - The Google ID token (used to re-authenticate if needed)
819
819
  * @param practitionerIds - Array of draft practitioner profile IDs to claim
820
820
  * @returns Object containing user and claimed practitioner
821
821
  */
@@ -836,77 +836,77 @@ export class AuthService extends BaseService {
836
836
  throw new AuthError('No practitioner profiles selected to claim', 'AUTH/NO_PROFILES_SELECTED', 400);
837
837
  }
838
838
 
839
- // Sign in with Google credential
840
- const credential = GoogleAuthProvider.credential(idToken);
841
- const { user: firebaseUser } = await signInWithCredential(this.auth, credential);
842
- console.log('[AUTH] Signed in with Google:', firebaseUser.uid);
839
+ // Check if user is currently signed in, if not re-authenticate
840
+ let firebaseUser = this.auth.currentUser;
841
+ if (!firebaseUser) {
842
+ console.log('[AUTH] No current user, re-authenticating with Google credential');
843
+ const credential = GoogleAuthProvider.credential(idToken);
844
+ const result = await signInWithCredential(this.auth, credential);
845
+ firebaseUser = result.user;
846
+ }
847
+ console.log('[AUTH] Using Firebase user:', firebaseUser.uid);
848
+
849
+ // Force token refresh to ensure Firestore has fresh auth context
850
+ console.log('[AUTH] Forcing token refresh...');
851
+ await firebaseUser.getIdToken(true);
852
+ console.log('[AUTH] Token refreshed successfully');
843
853
 
844
854
  const practitionerService = new PractitionerService(this.db, this.auth, this.app);
845
855
 
846
- // Check if User document already exists
847
- let existingUser: User | null = null;
856
+ // Step 1: Check if User document already exists
857
+ let user: User | null = null;
848
858
  try {
849
- existingUser = await this.userService.getUserById(firebaseUser.uid);
850
- console.log('[AUTH] User document already exists:', existingUser.uid);
859
+ user = await this.userService.getUserById(firebaseUser.uid);
860
+ console.log('[AUTH] User document already exists:', user.uid);
851
861
  } catch (userError) {
852
- console.log('[AUTH] User document does not exist, will create it');
862
+ console.log('[AUTH] User document does not exist, creating it...');
853
863
  }
854
864
 
855
- // Execute all operations in a transaction (similar to token flow)
856
- const transactionResult = await runTransaction(this.db, async () => {
857
- console.log('[AUTH] Transaction started - creating user and claiming profiles');
858
-
859
- // Create user document if it doesn't exist
860
- let user: User;
861
- if (existingUser) {
862
- user = existingUser;
863
- } else {
864
- console.log('[AUTH] Creating user document');
865
- user = await this.userService.createUser(firebaseUser, [UserRole.PRACTITIONER], {
866
- skipProfileCreation: true,
867
- });
868
- console.log('[AUTH] User document created:', user.uid);
869
- }
865
+ // Step 2: Create User document if it doesn't exist (NOT in transaction - matches token flow)
866
+ if (!user) {
867
+ console.log('[AUTH] Creating user document for:', firebaseUser.uid);
868
+ user = await this.userService.createUser(firebaseUser, [UserRole.PRACTITIONER], {
869
+ skipProfileCreation: true,
870
+ });
871
+ console.log('[AUTH] User document created successfully:', user.uid);
872
+ }
870
873
 
871
- // Claim the draft profiles
872
- let practitioner: Practitioner;
873
- if (practitionerIds.length === 1) {
874
- console.log('[AUTH] Claiming single draft profile');
875
- practitioner = await practitionerService.claimDraftProfileWithGoogle(
876
- practitionerIds[0],
877
- firebaseUser.uid
878
- );
879
- } else {
880
- console.log('[AUTH] Claiming multiple draft profiles');
881
- practitioner = await practitionerService.claimMultipleDraftProfilesWithGoogle(
882
- practitionerIds,
883
- firebaseUser.uid
884
- );
885
- }
874
+ // Step 3: Claim the draft profiles
875
+ let practitioner: Practitioner;
876
+ if (practitionerIds.length === 1) {
877
+ console.log('[AUTH] Claiming single draft profile:', practitionerIds[0]);
878
+ practitioner = await practitionerService.claimDraftProfileWithGoogle(
879
+ practitionerIds[0],
880
+ firebaseUser.uid
881
+ );
882
+ } else {
883
+ console.log('[AUTH] Claiming multiple draft profiles:', practitionerIds);
884
+ practitioner = await practitionerService.claimMultipleDraftProfilesWithGoogle(
885
+ practitionerIds,
886
+ firebaseUser.uid
887
+ );
888
+ }
889
+ console.log('[AUTH] Draft profiles claimed:', practitioner.id);
886
890
 
887
- // Link practitioner to user (if not already linked)
888
- if (!user.practitionerProfile || user.practitionerProfile !== practitioner.id) {
889
- console.log('[AUTH] Linking practitioner to user');
890
- await this.userService.updateUser(firebaseUser.uid, {
891
- practitionerProfile: practitioner.id,
892
- });
893
- }
891
+ // Step 4: Link practitioner to user
892
+ if (!user.practitionerProfile || user.practitionerProfile !== practitioner.id) {
893
+ console.log('[AUTH] Linking practitioner to user');
894
+ await this.userService.updateUser(firebaseUser.uid, {
895
+ practitionerProfile: practitioner.id,
896
+ });
897
+ }
894
898
 
895
- console.log('[AUTH] Transaction completed successfully');
896
- return { user, practitioner };
897
- });
899
+ // Fetch updated user (with practitionerProfile reference)
900
+ const updatedUser = await this.userService.getUserById(firebaseUser.uid);
898
901
 
899
902
  console.log('[AUTH] Draft profiles claimed successfully', {
900
- userId: transactionResult.user.uid,
901
- practitionerId: transactionResult.practitioner.id,
903
+ userId: updatedUser.uid,
904
+ practitionerId: practitioner.id,
902
905
  });
903
906
 
904
- // Fetch updated user (with practitionerProfile reference)
905
- const updatedUser = await this.userService.getUserById(firebaseUser.uid);
906
-
907
907
  return {
908
908
  user: updatedUser,
909
- practitioner: transactionResult.practitioner,
909
+ practitioner,
910
910
  };
911
911
  } catch (error: any) {
912
912
  console.error('[AUTH] Error claiming draft profiles with Google:', error);