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.cjs CHANGED
@@ -21,6 +21,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var src_exports = {};
22
22
  __export(src_exports, {
23
23
  ArraySnapshot: () => ArraySnapshot,
24
+ Authentication: () => Authentication,
24
25
  Credentials: () => Credentials,
25
26
  DocumentSnapshot: () => DocumentSnapshot,
26
27
  StorageSnapshot: () => StorageSnapshot,
@@ -110,112 +111,18 @@ var HttpsRequest = class {
110
111
  }
111
112
  };
112
113
 
113
- // src/utils/waiter.ts
114
- var listeners = /* @__PURE__ */ new Map();
115
- var waiters = /* @__PURE__ */ new Map();
116
- function emitValue(key, value) {
117
- const keyListeners = listeners.get(key) || [];
118
- keyListeners.forEach((listener) => listener(value));
119
- const keyWaiters = waiters.get(key) || [];
120
- keyWaiters.forEach((resolve) => resolve(value));
121
- waiters.delete(key);
122
- }
123
- function onValue(key, callback) {
124
- const current = listeners.get(key) || [];
125
- current.push(callback);
126
- listeners.set(key, current);
127
- return () => {
128
- const next = (listeners.get(key) || []).filter((cb) => cb !== callback);
129
- listeners.set(key, next);
130
- };
131
- }
132
-
133
114
  // src/authentication/Authentication.ts
134
- var Authentication = class {
115
+ var _Authentication = class _Authentication {
135
116
  constructor() {
136
- this.init = () => {
137
- const _currentUser = this.currentUser();
138
- if (!_currentUser)
139
- return void 0;
140
- this.eUser = _currentUser;
141
- return this.eUser;
142
- };
143
- this.saveCredentials = (credentials) => {
144
- const ldb = localStorage;
145
- if (!credentials) {
146
- ldb.removeItem("eauth");
147
- return;
148
- }
149
- ldb.setItem("eauth", JSON.stringify(credentials));
150
- emitValue("credentials", credentials);
151
- };
152
- this.currentUser = () => {
153
- const ldb = localStorage;
154
- const data = ldb.getItem("eauth");
155
- if (data === void 0 || data === null) {
156
- return void 0;
157
- }
158
- const result = JSON.parse(data ?? "{}");
159
- return Credentials.fromMap(result);
160
- };
161
- this.authState = ({
162
- onChange,
163
- onSignOut,
164
- onDeleted
165
- }) => {
166
- let userDocUnsubscribe;
167
- const attachUserListener = (creds) => {
168
- if (userDocUnsubscribe) {
169
- userDocUnsubscribe();
170
- userDocUnsubscribe = void 0;
171
- }
172
- if (!creds) {
173
- this.eUser = void 0;
174
- onSignOut?.();
175
- return;
176
- }
177
- const userRef = this.app?.database.collection("users").doc(creds.uid);
178
- if (!userRef)
179
- return;
180
- userDocUnsubscribe = userRef.onSnapshot(
181
- (snapshot, change) => {
182
- if (change === "insert" || change === "update") {
183
- if (snapshot.data.logged === "false" || snapshot.data.logged === false) {
184
- this.eUser = void 0;
185
- this.saveCredentials(null);
186
- onSignOut?.();
187
- return;
188
- }
189
- const result = Credentials.fromMap(snapshot.toMap());
190
- this.eUser = result;
191
- this.saveCredentials(result);
192
- onChange(result);
193
- }
194
- if (change === "delete") {
195
- this.eUser = void 0;
196
- this.saveCredentials(null);
197
- onSignOut?.();
198
- onDeleted?.();
199
- }
200
- }
201
- );
202
- };
203
- const current = this.currentUser();
204
- if (current) {
205
- this.eUser = current;
206
- onChange(current);
207
- attachUserListener(current);
208
- }
209
- const unsubscribeCredentials = onValue(
210
- "credentials",
211
- (creds) => {
212
- attachUserListener(creds);
213
- }
214
- );
215
- return () => {
216
- unsubscribeCredentials?.();
217
- userDocUnsubscribe?.();
218
- };
117
+ this.eventListeners = /* @__PURE__ */ new Map();
118
+ this.eventWaiters = /* @__PURE__ */ new Map();
119
+ this.unsubscribers = /* @__PURE__ */ new Set();
120
+ this.isSameCredentials = (a, b) => {
121
+ if (!a && !b)
122
+ return true;
123
+ if (!a || !b)
124
+ return false;
125
+ return JSON.stringify(a.toMap()) === JSON.stringify(b.toMap());
219
126
  };
220
127
  this.createUserWithEmailAndPassword = async ({
221
128
  email,
@@ -267,13 +174,12 @@ var Authentication = class {
267
174
  throw new Error("User Not Found.");
268
175
  }
269
176
  const _data = data[0];
270
- console.log("Signing ...");
271
- this.eUser = Credentials.fromMap(_data);
272
- this.saveCredentials(this.eUser);
273
177
  await db?.collection("users").doc(data[0].id).update({
274
178
  logged: true,
275
179
  lastLogged: Date.now()
276
180
  });
181
+ this.eUser = Credentials.fromMap(_data);
182
+ this.saveCredentials(this.eUser);
277
183
  return Credentials.fromMap(data[0].toMap());
278
184
  };
279
185
  this.deleteUser = async () => {
@@ -314,16 +220,127 @@ var Authentication = class {
314
220
  }).sendRequest();
315
221
  return res;
316
222
  };
223
+ if (_Authentication.instance)
224
+ return _Authentication.instance;
317
225
  this.client = EdmaxLabs.instance;
318
226
  this.app = new EdmaxLabs({
319
- token: this.client.getAuth.authToken,
320
- authToken: this.client.getAuth.authToken,
227
+ token: "auth",
321
228
  project: this.client.getAuth.project
322
229
  });
323
- this.init();
230
+ _Authentication.instance = this;
231
+ this.restoreSession();
232
+ }
233
+ restoreSession() {
234
+ const saved = this.currentUser();
235
+ if (saved) {
236
+ this.eUser = saved;
237
+ this.emitValue("creds", saved);
238
+ }
239
+ }
240
+ // Better singleton
241
+ static getInstance() {
242
+ if (!_Authentication.instance) {
243
+ _Authentication.instance = new _Authentication();
244
+ }
245
+ return _Authentication.instance;
246
+ }
247
+ emitValue(key, value) {
248
+ const listeners = this.eventListeners.get(key) || [];
249
+ listeners.forEach((l) => l(value));
250
+ const waiters = this.eventWaiters.get(key) || [];
251
+ waiters.forEach((resolve) => resolve(value));
252
+ this.eventWaiters.delete(key);
253
+ }
254
+ onValue(key, callback) {
255
+ const listeners = this.eventListeners.get(key) || [];
256
+ listeners.push(callback);
257
+ this.eventListeners.set(key, listeners);
258
+ return () => {
259
+ const filtered = listeners.filter((cb) => cb !== callback);
260
+ this.eventListeners.set(key, filtered);
261
+ };
262
+ }
263
+ currentUser() {
264
+ const data = localStorage.getItem("eauth");
265
+ if (!data)
266
+ return null;
267
+ try {
268
+ return Credentials.fromMap(JSON.parse(data));
269
+ } catch {
270
+ localStorage.removeItem("eauth");
271
+ return null;
272
+ }
273
+ }
274
+ saveCredentials(credentials) {
275
+ if (!credentials) {
276
+ localStorage.removeItem("eauth");
277
+ this.eUser = null;
278
+ this.emitValue("creds", null);
279
+ return;
280
+ }
281
+ localStorage.setItem("eauth", JSON.stringify(credentials.toMap()));
282
+ this.eUser = credentials;
283
+ this.emitValue("creds", credentials);
284
+ }
285
+ // Main auth state listener - should be called ONCE at app root
286
+ authState({
287
+ onChange,
288
+ onSignOut,
289
+ onDeleted
290
+ }) {
291
+ let userDocUnsubscribe;
292
+ const handleCredsChange = (creds) => {
293
+ if (userDocUnsubscribe) {
294
+ userDocUnsubscribe();
295
+ userDocUnsubscribe = void 0;
296
+ }
297
+ if (!creds) {
298
+ this.eUser = null;
299
+ onSignOut?.();
300
+ return;
301
+ }
302
+ const userRef = this.app?.database.collection("users").doc(creds.uid);
303
+ if (!userRef)
304
+ return;
305
+ userDocUnsubscribe = userRef.onSnapshot(
306
+ (snapshot, change) => {
307
+ if (change === "delete") {
308
+ this.saveCredentials(null);
309
+ onSignOut?.();
310
+ onDeleted?.();
311
+ return;
312
+ }
313
+ if (change === "insert" || change === "update") {
314
+ if (snapshot.data.logged === false || snapshot.data.logged === "false") {
315
+ this.saveCredentials(null);
316
+ onSignOut?.();
317
+ return;
318
+ }
319
+ const newCreds = Credentials.fromMap(snapshot.toMap());
320
+ if (!this.isSameCredentials(this.eUser, newCreds)) {
321
+ this.eUser = newCreds;
322
+ this.saveCredentials(newCreds);
323
+ onChange(newCreds);
324
+ }
325
+ }
326
+ }
327
+ );
328
+ };
329
+ const initialUser = this.currentUser();
330
+ if (initialUser) {
331
+ this.eUser = initialUser;
332
+ onChange(initialUser);
333
+ }
334
+ handleCredsChange(initialUser);
335
+ const credsUnsub = this.onValue("creds", handleCredsChange);
336
+ return () => {
337
+ credsUnsub();
338
+ userDocUnsubscribe?.();
339
+ };
324
340
  }
325
341
  };
326
- Authentication.instance = null;
342
+ _Authentication.instance = null;
343
+ var Authentication = _Authentication;
327
344
 
328
345
  // src/database/Timestamp.ts
329
346
  var Timestamp = class _Timestamp {
@@ -1603,11 +1620,11 @@ var socketURI = "https://api.edmaxlabs.com";
1603
1620
 
1604
1621
  // src/core/EdmaxLabs.ts
1605
1622
  var _EdmaxLabs = class _EdmaxLabs {
1606
- constructor({ token, authToken, project }) {
1623
+ constructor({ token, project }) {
1607
1624
  this.persistence = false;
1608
1625
  this.baseUrl = serverURI;
1609
1626
  this.socketUrl = socketURI;
1610
- this.auth = { token, authToken, project };
1627
+ this.auth = { token, project };
1611
1628
  this._db = new Database(this);
1612
1629
  this._hosting = new Hosting(this);
1613
1630
  this.realtime = new Realtime(this);
@@ -1672,6 +1689,7 @@ var src_default = EdmaxLabs;
1672
1689
  // Annotate the CommonJS export names for ESM import in node:
1673
1690
  0 && (module.exports = {
1674
1691
  ArraySnapshot,
1692
+ Authentication,
1675
1693
  Credentials,
1676
1694
  DocumentSnapshot,
1677
1695
  StorageSnapshot,