edmaxlabs-core 1.2.3 → 1.3.4

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.mjs CHANGED
@@ -79,112 +79,18 @@ var HttpsRequest = class {
79
79
  }
80
80
  };
81
81
 
82
- // src/utils/waiter.ts
83
- var listeners = /* @__PURE__ */ new Map();
84
- var waiters = /* @__PURE__ */ new Map();
85
- function emitValue(key, value) {
86
- const keyListeners = listeners.get(key) || [];
87
- keyListeners.forEach((listener) => listener(value));
88
- const keyWaiters = waiters.get(key) || [];
89
- keyWaiters.forEach((resolve) => resolve(value));
90
- waiters.delete(key);
91
- }
92
- function onValue(key, callback) {
93
- const current = listeners.get(key) || [];
94
- current.push(callback);
95
- listeners.set(key, current);
96
- return () => {
97
- const next = (listeners.get(key) || []).filter((cb) => cb !== callback);
98
- listeners.set(key, next);
99
- };
100
- }
101
-
102
82
  // src/authentication/Authentication.ts
103
- var Authentication = class {
83
+ var _Authentication = class _Authentication {
104
84
  constructor() {
105
- this.init = () => {
106
- const _currentUser = this.currentUser();
107
- if (!_currentUser)
108
- return void 0;
109
- this.eUser = _currentUser;
110
- return this.eUser;
111
- };
112
- this.saveCredentials = (credentials) => {
113
- const ldb = localStorage;
114
- if (!credentials) {
115
- ldb.removeItem("eauth");
116
- return;
117
- }
118
- ldb.setItem("eauth", JSON.stringify(credentials));
119
- emitValue("credentials", credentials);
120
- };
121
- this.currentUser = () => {
122
- const ldb = localStorage;
123
- const data = ldb.getItem("eauth");
124
- if (data === void 0 || data === null) {
125
- return void 0;
126
- }
127
- const result = JSON.parse(data ?? "{}");
128
- return Credentials.fromMap(result);
129
- };
130
- this.authState = ({
131
- onChange,
132
- onSignOut,
133
- onDeleted
134
- }) => {
135
- let userDocUnsubscribe;
136
- const attachUserListener = (creds) => {
137
- if (userDocUnsubscribe) {
138
- userDocUnsubscribe();
139
- userDocUnsubscribe = void 0;
140
- }
141
- if (!creds) {
142
- this.eUser = void 0;
143
- onSignOut?.();
144
- return;
145
- }
146
- const userRef = this.app?.database.collection("users").doc(creds.uid);
147
- if (!userRef)
148
- return;
149
- userDocUnsubscribe = userRef.onSnapshot(
150
- (snapshot, change) => {
151
- if (change === "insert" || change === "update") {
152
- if (snapshot.data.logged === "false" || snapshot.data.logged === false) {
153
- this.eUser = void 0;
154
- this.saveCredentials(null);
155
- onSignOut?.();
156
- return;
157
- }
158
- const result = Credentials.fromMap(snapshot.toMap());
159
- this.eUser = result;
160
- this.saveCredentials(result);
161
- onChange(result);
162
- }
163
- if (change === "delete") {
164
- this.eUser = void 0;
165
- this.saveCredentials(null);
166
- onSignOut?.();
167
- onDeleted?.();
168
- }
169
- }
170
- );
171
- };
172
- const current = this.currentUser();
173
- if (current) {
174
- this.eUser = current;
175
- onChange(current);
176
- attachUserListener(current);
177
- }
178
- const unsubscribeCredentials = onValue(
179
- "credentials",
180
- (creds) => {
181
- attachUserListener(creds);
182
- }
183
- );
184
- return () => {
185
- unsubscribeCredentials?.();
186
- userDocUnsubscribe?.();
187
- };
85
+ this.eventListeners = /* @__PURE__ */ new Map();
86
+ this.eventWaiters = /* @__PURE__ */ new Map();
87
+ this.unsubscribers = /* @__PURE__ */ new Set();
88
+ this.isSameCredentials = (a, b) => {
89
+ if (!a && !b)
90
+ return true;
91
+ if (!a || !b)
92
+ return false;
93
+ return JSON.stringify(a.toMap()) === JSON.stringify(b.toMap());
188
94
  };
189
95
  this.createUserWithEmailAndPassword = async ({
190
96
  email,
@@ -236,13 +142,12 @@ var Authentication = class {
236
142
  throw new Error("User Not Found.");
237
143
  }
238
144
  const _data = data[0];
239
- console.log("Signing ...");
240
- this.eUser = Credentials.fromMap(_data);
241
- this.saveCredentials(this.eUser);
242
145
  await db?.collection("users").doc(data[0].id).update({
243
146
  logged: true,
244
147
  lastLogged: Date.now()
245
148
  });
149
+ this.eUser = Credentials.fromMap(_data);
150
+ this.saveCredentials(this.eUser);
246
151
  return Credentials.fromMap(data[0].toMap());
247
152
  };
248
153
  this.deleteUser = async () => {
@@ -283,16 +188,127 @@ var Authentication = class {
283
188
  }).sendRequest();
284
189
  return res;
285
190
  };
191
+ if (_Authentication.instance)
192
+ return _Authentication.instance;
286
193
  this.client = EdmaxLabs.instance;
287
194
  this.app = new EdmaxLabs({
288
- token: this.client.getAuth.authToken,
289
- authToken: this.client.getAuth.authToken,
195
+ token: "auth",
290
196
  project: this.client.getAuth.project
291
197
  });
292
- this.init();
198
+ _Authentication.instance = this;
199
+ this.restoreSession();
200
+ }
201
+ restoreSession() {
202
+ const saved = this.currentUser();
203
+ if (saved) {
204
+ this.eUser = saved;
205
+ this.emitValue("creds", saved);
206
+ }
207
+ }
208
+ // Better singleton
209
+ static getInstance() {
210
+ if (!_Authentication.instance) {
211
+ _Authentication.instance = new _Authentication();
212
+ }
213
+ return _Authentication.instance;
214
+ }
215
+ emitValue(key, value) {
216
+ const listeners = this.eventListeners.get(key) || [];
217
+ listeners.forEach((l) => l(value));
218
+ const waiters = this.eventWaiters.get(key) || [];
219
+ waiters.forEach((resolve) => resolve(value));
220
+ this.eventWaiters.delete(key);
221
+ }
222
+ onValue(key, callback) {
223
+ const listeners = this.eventListeners.get(key) || [];
224
+ listeners.push(callback);
225
+ this.eventListeners.set(key, listeners);
226
+ return () => {
227
+ const filtered = listeners.filter((cb) => cb !== callback);
228
+ this.eventListeners.set(key, filtered);
229
+ };
230
+ }
231
+ currentUser() {
232
+ const data = localStorage.getItem("eauth");
233
+ if (!data)
234
+ return null;
235
+ try {
236
+ return Credentials.fromMap(JSON.parse(data));
237
+ } catch {
238
+ localStorage.removeItem("eauth");
239
+ return null;
240
+ }
241
+ }
242
+ saveCredentials(credentials) {
243
+ if (!credentials) {
244
+ localStorage.removeItem("eauth");
245
+ this.eUser = null;
246
+ this.emitValue("creds", null);
247
+ return;
248
+ }
249
+ localStorage.setItem("eauth", JSON.stringify(credentials.toMap()));
250
+ this.eUser = credentials;
251
+ this.emitValue("creds", credentials);
252
+ }
253
+ // Main auth state listener - should be called ONCE at app root
254
+ authState({
255
+ onChange,
256
+ onSignOut,
257
+ onDeleted
258
+ }) {
259
+ let userDocUnsubscribe;
260
+ const handleCredsChange = (creds) => {
261
+ if (userDocUnsubscribe) {
262
+ userDocUnsubscribe();
263
+ userDocUnsubscribe = void 0;
264
+ }
265
+ if (!creds) {
266
+ this.eUser = null;
267
+ onSignOut?.();
268
+ return;
269
+ }
270
+ const userRef = this.app?.database.collection("users").doc(creds.uid);
271
+ if (!userRef)
272
+ return;
273
+ userDocUnsubscribe = userRef.onSnapshot(
274
+ (snapshot, change) => {
275
+ if (change === "delete") {
276
+ this.saveCredentials(null);
277
+ onSignOut?.();
278
+ onDeleted?.();
279
+ return;
280
+ }
281
+ if (change === "insert" || change === "update") {
282
+ if (snapshot.data.logged === false || snapshot.data.logged === "false") {
283
+ this.saveCredentials(null);
284
+ onSignOut?.();
285
+ return;
286
+ }
287
+ const newCreds = Credentials.fromMap(snapshot.toMap());
288
+ if (!this.isSameCredentials(this.eUser, newCreds)) {
289
+ this.eUser = newCreds;
290
+ this.saveCredentials(newCreds);
291
+ onChange(newCreds);
292
+ }
293
+ }
294
+ }
295
+ );
296
+ };
297
+ const initialUser = this.currentUser();
298
+ if (initialUser) {
299
+ this.eUser = initialUser;
300
+ onChange(initialUser);
301
+ }
302
+ handleCredsChange(initialUser);
303
+ const credsUnsub = this.onValue("creds", handleCredsChange);
304
+ return () => {
305
+ credsUnsub();
306
+ userDocUnsubscribe?.();
307
+ };
293
308
  }
294
309
  };
295
- Authentication.instance = null;
310
+ _Authentication.instance = null;
311
+ var Authentication = _Authentication;
296
312
 
297
313
  // src/database/Timestamp.ts
298
314
  var Timestamp = class _Timestamp {
@@ -1572,11 +1588,11 @@ var socketURI = "https://api.edmaxlabs.com";
1572
1588
 
1573
1589
  // src/core/EdmaxLabs.ts
1574
1590
  var _EdmaxLabs = class _EdmaxLabs {
1575
- constructor({ token, authToken, project }) {
1591
+ constructor({ token, project }) {
1576
1592
  this.persistence = false;
1577
1593
  this.baseUrl = serverURI;
1578
1594
  this.socketUrl = socketURI;
1579
- this.auth = { token, authToken, project };
1595
+ this.auth = { token, project };
1580
1596
  this._db = new Database(this);
1581
1597
  this._hosting = new Hosting(this);
1582
1598
  this.realtime = new Realtime(this);
@@ -1640,6 +1656,7 @@ var EdmaxLabs = _EdmaxLabs;
1640
1656
  var src_default = EdmaxLabs;
1641
1657
  export {
1642
1658
  ArraySnapshot,
1659
+ Authentication,
1643
1660
  Credentials,
1644
1661
  DocumentSnapshot,
1645
1662
  StorageSnapshot,