@blackcode_sa/metaestetics-api 1.14.5 → 1.14.7

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
@@ -8321,6 +8321,16 @@ declare class AuthService extends BaseService {
8321
8321
  private googleProvider;
8322
8322
  private userService;
8323
8323
  constructor(db: Firestore, auth: Auth, app: FirebaseApp, userService: UserService);
8324
+ /**
8325
+ * Waits for Firebase Auth state to settle after sign-in.
8326
+ * In React Native with Firebase JS SDK, auth.currentUser doesn't update synchronously
8327
+ * after signInWithCredential. This causes Firestore permission issues.
8328
+ *
8329
+ * @param expectedUid - The UID we expect to see in auth.currentUser
8330
+ * @param timeoutMs - Maximum time to wait (default 10 seconds)
8331
+ * @returns Promise that resolves when auth state is ready
8332
+ */
8333
+ private waitForAuthState;
8324
8334
  /**
8325
8335
  * Registruje novog korisnika sa email-om i lozinkom
8326
8336
  */
package/dist/index.d.ts CHANGED
@@ -8321,6 +8321,16 @@ declare class AuthService extends BaseService {
8321
8321
  private googleProvider;
8322
8322
  private userService;
8323
8323
  constructor(db: Firestore, auth: Auth, app: FirebaseApp, userService: UserService);
8324
+ /**
8325
+ * Waits for Firebase Auth state to settle after sign-in.
8326
+ * In React Native with Firebase JS SDK, auth.currentUser doesn't update synchronously
8327
+ * after signInWithCredential. This causes Firestore permission issues.
8328
+ *
8329
+ * @param expectedUid - The UID we expect to see in auth.currentUser
8330
+ * @param timeoutMs - Maximum time to wait (default 10 seconds)
8331
+ * @returns Promise that resolves when auth state is ready
8332
+ */
8333
+ private waitForAuthState;
8324
8334
  /**
8325
8335
  * Registruje novog korisnika sa email-om i lozinkom
8326
8336
  */
package/dist/index.js CHANGED
@@ -12906,6 +12906,23 @@ var UserService = class extends BaseService {
12906
12906
  * Kreira novog korisnika na osnovu Firebase korisnika
12907
12907
  */
12908
12908
  async createUser(firebaseUser, roles = ["patient" /* PATIENT */], options) {
12909
+ var _a, _b, _c, _d, _e, _f, _g, _h;
12910
+ console.log("[USER_SERVICE] ====== CREATE USER DEBUG ======");
12911
+ console.log("[USER_SERVICE] Current auth state:", {
12912
+ currentUser: ((_b = (_a = this.auth) == null ? void 0 : _a.currentUser) == null ? void 0 : _b.uid) || "NULL",
12913
+ currentUserEmail: ((_d = (_c = this.auth) == null ? void 0 : _c.currentUser) == null ? void 0 : _d.email) || "NULL",
12914
+ currentUserProvider: ((_f = (_e = this.auth) == null ? void 0 : _e.currentUser) == null ? void 0 : _f.providerId) || "NULL"
12915
+ });
12916
+ console.log("[USER_SERVICE] Firebase user passed to createUser:", {
12917
+ uid: (firebaseUser == null ? void 0 : firebaseUser.uid) || "NULL",
12918
+ email: (firebaseUser == null ? void 0 : firebaseUser.email) || "NULL",
12919
+ providerId: (firebaseUser == null ? void 0 : firebaseUser.providerId) || "NULL",
12920
+ isAnonymous: firebaseUser == null ? void 0 : firebaseUser.isAnonymous
12921
+ });
12922
+ console.log("[USER_SERVICE] Auth instances match:", ((_h = (_g = this.auth) == null ? void 0 : _g.currentUser) == null ? void 0 : _h.uid) === (firebaseUser == null ? void 0 : firebaseUser.uid));
12923
+ console.log("[USER_SERVICE] Document path:", `${USERS_COLLECTION}/${firebaseUser == null ? void 0 : firebaseUser.uid}`);
12924
+ console.log("[USER_SERVICE] Roles:", roles);
12925
+ console.log("[USER_SERVICE] ================================");
12909
12926
  const userData = {
12910
12927
  uid: firebaseUser.uid,
12911
12928
  email: firebaseUser.email,
@@ -12915,7 +12932,22 @@ var UserService = class extends BaseService {
12915
12932
  updatedAt: (0, import_firestore33.serverTimestamp)(),
12916
12933
  lastLoginAt: (0, import_firestore33.serverTimestamp)()
12917
12934
  };
12918
- await (0, import_firestore33.setDoc)((0, import_firestore33.doc)(this.db, USERS_COLLECTION, userData.uid), userData);
12935
+ console.log("[USER_SERVICE] Attempting setDoc with userData:", {
12936
+ uid: userData.uid,
12937
+ email: userData.email,
12938
+ roles: userData.roles
12939
+ });
12940
+ try {
12941
+ await (0, import_firestore33.setDoc)((0, import_firestore33.doc)(this.db, USERS_COLLECTION, userData.uid), userData);
12942
+ console.log("[USER_SERVICE] \u2705 setDoc SUCCEEDED for:", userData.uid);
12943
+ } catch (error) {
12944
+ console.error("[USER_SERVICE] \u274C setDoc FAILED:", {
12945
+ errorCode: error == null ? void 0 : error.code,
12946
+ errorMessage: error == null ? void 0 : error.message,
12947
+ uid: userData.uid
12948
+ });
12949
+ throw error;
12950
+ }
12919
12951
  if (options == null ? void 0 : options.skipProfileCreation) {
12920
12952
  return this.getUserById(userData.uid);
12921
12953
  }
@@ -15349,6 +15381,40 @@ var AuthService = class extends BaseService {
15349
15381
  this.googleProvider = new import_auth8.GoogleAuthProvider();
15350
15382
  this.userService = userService || new UserService(db, auth, app);
15351
15383
  }
15384
+ /**
15385
+ * Waits for Firebase Auth state to settle after sign-in.
15386
+ * In React Native with Firebase JS SDK, auth.currentUser doesn't update synchronously
15387
+ * after signInWithCredential. This causes Firestore permission issues.
15388
+ *
15389
+ * @param expectedUid - The UID we expect to see in auth.currentUser
15390
+ * @param timeoutMs - Maximum time to wait (default 10 seconds)
15391
+ * @returns Promise that resolves when auth state is ready
15392
+ */
15393
+ waitForAuthState(expectedUid, timeoutMs = 1e4) {
15394
+ return new Promise((resolve, reject) => {
15395
+ var _a;
15396
+ if (((_a = this.auth.currentUser) == null ? void 0 : _a.uid) === expectedUid) {
15397
+ console.log("[AUTH] Auth state already settled for:", expectedUid);
15398
+ resolve();
15399
+ return;
15400
+ }
15401
+ console.log("[AUTH] Waiting for auth state to settle for:", expectedUid);
15402
+ const timeout = setTimeout(() => {
15403
+ unsubscribe();
15404
+ console.error("[AUTH] Timeout waiting for auth state");
15405
+ reject(new Error("Timeout waiting for auth state to settle"));
15406
+ }, timeoutMs);
15407
+ const unsubscribe = (0, import_auth8.onAuthStateChanged)(this.auth, (user) => {
15408
+ console.log("[AUTH] Auth state changed:", (user == null ? void 0 : user.uid) || "null");
15409
+ if ((user == null ? void 0 : user.uid) === expectedUid) {
15410
+ clearTimeout(timeout);
15411
+ unsubscribe();
15412
+ console.log("[AUTH] Auth state settled successfully for:", expectedUid);
15413
+ resolve();
15414
+ }
15415
+ });
15416
+ });
15417
+ }
15352
15418
  /**
15353
15419
  * Registruje novog korisnika sa email-om i lozinkom
15354
15420
  */
@@ -15917,6 +15983,7 @@ var AuthService = class extends BaseService {
15917
15983
  * @returns Object containing user and claimed practitioner
15918
15984
  */
15919
15985
  async claimDraftProfilesWithGoogle(idToken, practitionerIds) {
15986
+ var _a;
15920
15987
  try {
15921
15988
  console.log("[AUTH] Starting claim draft profiles with Google", {
15922
15989
  practitionerIdsCount: practitionerIds.length,
@@ -15933,9 +16000,11 @@ var AuthService = class extends BaseService {
15933
16000
  firebaseUser = result.user;
15934
16001
  }
15935
16002
  console.log("[AUTH] Using Firebase user:", firebaseUser.uid);
16003
+ await this.waitForAuthState(firebaseUser.uid);
15936
16004
  console.log("[AUTH] Forcing token refresh...");
15937
16005
  await firebaseUser.getIdToken(true);
15938
16006
  console.log("[AUTH] Token refreshed successfully");
16007
+ console.log("[AUTH] Verifying auth.currentUser:", ((_a = this.auth.currentUser) == null ? void 0 : _a.uid) || "NULL");
15939
16008
  const practitionerService = new PractitionerService(this.db, this.auth, this.app);
15940
16009
  let user = null;
15941
16010
  try {
package/dist/index.mjs CHANGED
@@ -12929,6 +12929,23 @@ var UserService = class extends BaseService {
12929
12929
  * Kreira novog korisnika na osnovu Firebase korisnika
12930
12930
  */
12931
12931
  async createUser(firebaseUser, roles = ["patient" /* PATIENT */], options) {
12932
+ var _a, _b, _c, _d, _e, _f, _g, _h;
12933
+ console.log("[USER_SERVICE] ====== CREATE USER DEBUG ======");
12934
+ console.log("[USER_SERVICE] Current auth state:", {
12935
+ currentUser: ((_b = (_a = this.auth) == null ? void 0 : _a.currentUser) == null ? void 0 : _b.uid) || "NULL",
12936
+ currentUserEmail: ((_d = (_c = this.auth) == null ? void 0 : _c.currentUser) == null ? void 0 : _d.email) || "NULL",
12937
+ currentUserProvider: ((_f = (_e = this.auth) == null ? void 0 : _e.currentUser) == null ? void 0 : _f.providerId) || "NULL"
12938
+ });
12939
+ console.log("[USER_SERVICE] Firebase user passed to createUser:", {
12940
+ uid: (firebaseUser == null ? void 0 : firebaseUser.uid) || "NULL",
12941
+ email: (firebaseUser == null ? void 0 : firebaseUser.email) || "NULL",
12942
+ providerId: (firebaseUser == null ? void 0 : firebaseUser.providerId) || "NULL",
12943
+ isAnonymous: firebaseUser == null ? void 0 : firebaseUser.isAnonymous
12944
+ });
12945
+ console.log("[USER_SERVICE] Auth instances match:", ((_h = (_g = this.auth) == null ? void 0 : _g.currentUser) == null ? void 0 : _h.uid) === (firebaseUser == null ? void 0 : firebaseUser.uid));
12946
+ console.log("[USER_SERVICE] Document path:", `${USERS_COLLECTION}/${firebaseUser == null ? void 0 : firebaseUser.uid}`);
12947
+ console.log("[USER_SERVICE] Roles:", roles);
12948
+ console.log("[USER_SERVICE] ================================");
12932
12949
  const userData = {
12933
12950
  uid: firebaseUser.uid,
12934
12951
  email: firebaseUser.email,
@@ -12938,7 +12955,22 @@ var UserService = class extends BaseService {
12938
12955
  updatedAt: serverTimestamp18(),
12939
12956
  lastLoginAt: serverTimestamp18()
12940
12957
  };
12941
- await setDoc12(doc21(this.db, USERS_COLLECTION, userData.uid), userData);
12958
+ console.log("[USER_SERVICE] Attempting setDoc with userData:", {
12959
+ uid: userData.uid,
12960
+ email: userData.email,
12961
+ roles: userData.roles
12962
+ });
12963
+ try {
12964
+ await setDoc12(doc21(this.db, USERS_COLLECTION, userData.uid), userData);
12965
+ console.log("[USER_SERVICE] \u2705 setDoc SUCCEEDED for:", userData.uid);
12966
+ } catch (error) {
12967
+ console.error("[USER_SERVICE] \u274C setDoc FAILED:", {
12968
+ errorCode: error == null ? void 0 : error.code,
12969
+ errorMessage: error == null ? void 0 : error.message,
12970
+ uid: userData.uid
12971
+ });
12972
+ throw error;
12973
+ }
12942
12974
  if (options == null ? void 0 : options.skipProfileCreation) {
12943
12975
  return this.getUserById(userData.uid);
12944
12976
  }
@@ -15436,6 +15468,40 @@ var AuthService = class extends BaseService {
15436
15468
  this.googleProvider = new GoogleAuthProvider();
15437
15469
  this.userService = userService || new UserService(db, auth, app);
15438
15470
  }
15471
+ /**
15472
+ * Waits for Firebase Auth state to settle after sign-in.
15473
+ * In React Native with Firebase JS SDK, auth.currentUser doesn't update synchronously
15474
+ * after signInWithCredential. This causes Firestore permission issues.
15475
+ *
15476
+ * @param expectedUid - The UID we expect to see in auth.currentUser
15477
+ * @param timeoutMs - Maximum time to wait (default 10 seconds)
15478
+ * @returns Promise that resolves when auth state is ready
15479
+ */
15480
+ waitForAuthState(expectedUid, timeoutMs = 1e4) {
15481
+ return new Promise((resolve, reject) => {
15482
+ var _a;
15483
+ if (((_a = this.auth.currentUser) == null ? void 0 : _a.uid) === expectedUid) {
15484
+ console.log("[AUTH] Auth state already settled for:", expectedUid);
15485
+ resolve();
15486
+ return;
15487
+ }
15488
+ console.log("[AUTH] Waiting for auth state to settle for:", expectedUid);
15489
+ const timeout = setTimeout(() => {
15490
+ unsubscribe();
15491
+ console.error("[AUTH] Timeout waiting for auth state");
15492
+ reject(new Error("Timeout waiting for auth state to settle"));
15493
+ }, timeoutMs);
15494
+ const unsubscribe = onAuthStateChanged(this.auth, (user) => {
15495
+ console.log("[AUTH] Auth state changed:", (user == null ? void 0 : user.uid) || "null");
15496
+ if ((user == null ? void 0 : user.uid) === expectedUid) {
15497
+ clearTimeout(timeout);
15498
+ unsubscribe();
15499
+ console.log("[AUTH] Auth state settled successfully for:", expectedUid);
15500
+ resolve();
15501
+ }
15502
+ });
15503
+ });
15504
+ }
15439
15505
  /**
15440
15506
  * Registruje novog korisnika sa email-om i lozinkom
15441
15507
  */
@@ -16004,6 +16070,7 @@ var AuthService = class extends BaseService {
16004
16070
  * @returns Object containing user and claimed practitioner
16005
16071
  */
16006
16072
  async claimDraftProfilesWithGoogle(idToken, practitionerIds) {
16073
+ var _a;
16007
16074
  try {
16008
16075
  console.log("[AUTH] Starting claim draft profiles with Google", {
16009
16076
  practitionerIdsCount: practitionerIds.length,
@@ -16020,9 +16087,11 @@ var AuthService = class extends BaseService {
16020
16087
  firebaseUser = result.user;
16021
16088
  }
16022
16089
  console.log("[AUTH] Using Firebase user:", firebaseUser.uid);
16090
+ await this.waitForAuthState(firebaseUser.uid);
16023
16091
  console.log("[AUTH] Forcing token refresh...");
16024
16092
  await firebaseUser.getIdToken(true);
16025
16093
  console.log("[AUTH] Token refreshed successfully");
16094
+ console.log("[AUTH] Verifying auth.currentUser:", ((_a = this.auth.currentUser) == null ? void 0 : _a.uid) || "NULL");
16026
16095
  const practitionerService = new PractitionerService(this.db, this.auth, this.app);
16027
16096
  let user = null;
16028
16097
  try {
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.5",
4
+ "version": "1.14.7",
5
5
  "description": "Firebase authentication service with anonymous upgrade support",
6
6
  "main": "dist/index.js",
7
7
  "module": "dist/index.mjs",
@@ -92,6 +92,44 @@ export class AuthService extends BaseService {
92
92
  this.userService = userService || new UserService(db, auth, app);
93
93
  }
94
94
 
95
+ /**
96
+ * Waits for Firebase Auth state to settle after sign-in.
97
+ * In React Native with Firebase JS SDK, auth.currentUser doesn't update synchronously
98
+ * after signInWithCredential. This causes Firestore permission issues.
99
+ *
100
+ * @param expectedUid - The UID we expect to see in auth.currentUser
101
+ * @param timeoutMs - Maximum time to wait (default 10 seconds)
102
+ * @returns Promise that resolves when auth state is ready
103
+ */
104
+ private waitForAuthState(expectedUid: string, timeoutMs: number = 10000): Promise<void> {
105
+ return new Promise((resolve, reject) => {
106
+ // If already correct, resolve immediately
107
+ if (this.auth.currentUser?.uid === expectedUid) {
108
+ console.log('[AUTH] Auth state already settled for:', expectedUid);
109
+ resolve();
110
+ return;
111
+ }
112
+
113
+ console.log('[AUTH] Waiting for auth state to settle for:', expectedUid);
114
+
115
+ const timeout = setTimeout(() => {
116
+ unsubscribe();
117
+ console.error('[AUTH] Timeout waiting for auth state');
118
+ reject(new Error('Timeout waiting for auth state to settle'));
119
+ }, timeoutMs);
120
+
121
+ const unsubscribe = onAuthStateChanged(this.auth, (user) => {
122
+ console.log('[AUTH] Auth state changed:', user?.uid || 'null');
123
+ if (user?.uid === expectedUid) {
124
+ clearTimeout(timeout);
125
+ unsubscribe();
126
+ console.log('[AUTH] Auth state settled successfully for:', expectedUid);
127
+ resolve();
128
+ }
129
+ });
130
+ });
131
+ }
132
+
95
133
  /**
96
134
  * Registruje novog korisnika sa email-om i lozinkom
97
135
  */
@@ -846,10 +884,18 @@ export class AuthService extends BaseService {
846
884
  }
847
885
  console.log('[AUTH] Using Firebase user:', firebaseUser.uid);
848
886
 
887
+ // CRITICAL: Wait for auth state to settle in React Native
888
+ // The Firebase JS SDK doesn't update auth.currentUser synchronously after signInWithCredential
889
+ // This causes Firestore to not see the authenticated user, resulting in permission denied
890
+ await this.waitForAuthState(firebaseUser.uid);
891
+
849
892
  // Force token refresh to ensure Firestore has fresh auth context
850
893
  console.log('[AUTH] Forcing token refresh...');
851
894
  await firebaseUser.getIdToken(true);
852
895
  console.log('[AUTH] Token refreshed successfully');
896
+
897
+ // Verify auth state is correct
898
+ console.log('[AUTH] Verifying auth.currentUser:', this.auth.currentUser?.uid || 'NULL');
853
899
 
854
900
  const practitionerService = new PractitionerService(this.db, this.auth, this.app);
855
901
 
@@ -11,7 +11,6 @@ import {
11
11
  Timestamp,
12
12
  setDoc,
13
13
  serverTimestamp,
14
- FieldValue,
15
14
  } from 'firebase/firestore';
16
15
  import { initializeFirebase } from '../../config/firebase';
17
16
  import { User, UserRole, USERS_COLLECTION, CreateUserData } from '../../types';
@@ -90,6 +89,24 @@ export class UserService extends BaseService {
90
89
  skipProfileCreation?: boolean;
91
90
  },
92
91
  ): Promise<User> {
92
+ // DEBUG LOGGING - Check auth state before creating user document
93
+ console.log('[USER_SERVICE] ====== CREATE USER DEBUG ======');
94
+ console.log('[USER_SERVICE] Current auth state:', {
95
+ currentUser: this.auth?.currentUser?.uid || 'NULL',
96
+ currentUserEmail: this.auth?.currentUser?.email || 'NULL',
97
+ currentUserProvider: this.auth?.currentUser?.providerId || 'NULL',
98
+ });
99
+ console.log('[USER_SERVICE] Firebase user passed to createUser:', {
100
+ uid: firebaseUser?.uid || 'NULL',
101
+ email: firebaseUser?.email || 'NULL',
102
+ providerId: firebaseUser?.providerId || 'NULL',
103
+ isAnonymous: firebaseUser?.isAnonymous,
104
+ });
105
+ console.log('[USER_SERVICE] Auth instances match:', this.auth?.currentUser?.uid === firebaseUser?.uid);
106
+ console.log('[USER_SERVICE] Document path:', `${USERS_COLLECTION}/${firebaseUser?.uid}`);
107
+ console.log('[USER_SERVICE] Roles:', roles);
108
+ console.log('[USER_SERVICE] ================================');
109
+
93
110
  const userData: CreateUserData = {
94
111
  uid: firebaseUser.uid,
95
112
  email: firebaseUser.email,
@@ -100,8 +117,24 @@ export class UserService extends BaseService {
100
117
  lastLoginAt: serverTimestamp(),
101
118
  };
102
119
 
120
+ console.log('[USER_SERVICE] Attempting setDoc with userData:', {
121
+ uid: userData.uid,
122
+ email: userData.email,
123
+ roles: userData.roles,
124
+ });
125
+
103
126
  // Kreiramo osnovnog korisnika
104
- await setDoc(doc(this.db, USERS_COLLECTION, userData.uid), userData);
127
+ try {
128
+ await setDoc(doc(this.db, USERS_COLLECTION, userData.uid), userData);
129
+ console.log('[USER_SERVICE] ✅ setDoc SUCCEEDED for:', userData.uid);
130
+ } catch (error: any) {
131
+ console.error('[USER_SERVICE] ❌ setDoc FAILED:', {
132
+ errorCode: error?.code,
133
+ errorMessage: error?.message,
134
+ uid: userData.uid,
135
+ });
136
+ throw error;
137
+ }
105
138
 
106
139
  // Kreiramo odgovarajuće profile na osnovu rola
107
140
  if (options?.skipProfileCreation) {