edmaxlabs-core 1.2.4 → 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 +128 -110
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +17 -9
- package/dist/index.d.ts +17 -9
- package/dist/index.mjs +127 -110
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
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
|
|
83
|
+
var _Authentication = class _Authentication {
|
|
104
84
|
constructor() {
|
|
105
|
-
this.
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
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,14 +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
|
});
|
|
246
|
-
|
|
149
|
+
this.eUser = Credentials.fromMap(_data);
|
|
150
|
+
this.saveCredentials(this.eUser);
|
|
247
151
|
return Credentials.fromMap(data[0].toMap());
|
|
248
152
|
};
|
|
249
153
|
this.deleteUser = async () => {
|
|
@@ -284,15 +188,127 @@ var Authentication = class {
|
|
|
284
188
|
}).sendRequest();
|
|
285
189
|
return res;
|
|
286
190
|
};
|
|
191
|
+
if (_Authentication.instance)
|
|
192
|
+
return _Authentication.instance;
|
|
287
193
|
this.client = EdmaxLabs.instance;
|
|
288
194
|
this.app = new EdmaxLabs({
|
|
289
195
|
token: "auth",
|
|
290
196
|
project: this.client.getAuth.project
|
|
291
197
|
});
|
|
292
|
-
this
|
|
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
|
-
|
|
310
|
+
_Authentication.instance = null;
|
|
311
|
+
var Authentication = _Authentication;
|
|
296
312
|
|
|
297
313
|
// src/database/Timestamp.ts
|
|
298
314
|
var Timestamp = class _Timestamp {
|
|
@@ -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,
|