edmaxlabs-core 2.7.1 → 2.7.3

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
@@ -32,29 +32,27 @@ module.exports = __toCommonJS(src_exports);
32
32
 
33
33
  // src/authentication/Credentials.ts
34
34
  var Credentials = class _Credentials {
35
- constructor(uid, displayInfo, createdAt, updatedAt, lastLogged, logged) {
35
+ constructor(uid, displayInfo, data, verified, createdAt, updatedAt) {
36
36
  this.uid = uid;
37
37
  this.displayInfo = displayInfo;
38
+ this.data = data;
39
+ this.verified = verified;
38
40
  this.createdAt = createdAt;
39
41
  this.updatedAt = updatedAt;
40
- this.lastLogged = lastLogged;
41
- this.logged = logged === true;
42
42
  }
43
43
  static fromMap(map) {
44
- const uid = map.id ?? map.uid ?? "";
45
- const data = map.data ?? map;
46
- const d_info = data.displayInfo;
44
+ const uid = map.id ?? map.uid ?? map._id ?? null;
45
+ const data = map.data ?? map.user ?? map;
46
+ const displayInfo = data.displayInfo;
47
47
  const createdAt = data.createdAt;
48
48
  const updatedAt = data.updatedAt;
49
- const lastLogged = data.lastLogged;
50
- const logged = data.logged;
51
49
  const result = new _Credentials(
52
50
  uid,
53
- d_info,
51
+ displayInfo,
52
+ data.data,
53
+ data.verified,
54
54
  createdAt,
55
- updatedAt,
56
- lastLogged,
57
- logged
55
+ updatedAt
58
56
  );
59
57
  return result;
60
58
  }
@@ -62,10 +60,10 @@ var Credentials = class _Credentials {
62
60
  return {
63
61
  uid: this.uid,
64
62
  displayInfo: this.displayInfo,
63
+ data: this.data,
64
+ verified: this.verified,
65
65
  createdAt: this.createdAt,
66
- updatedAt: this.updatedAt,
67
- lastLogged: this.lastLogged,
68
- logged: this.logged
66
+ updatedAt: this.updatedAt
69
67
  };
70
68
  }
71
69
  };
@@ -150,32 +148,23 @@ var _Authentication = class _Authentication {
150
148
  }) => {
151
149
  this.eUser = void 0;
152
150
  this.saveCredentials(null);
153
- const app = this.app?.getDatabase;
154
- const data = await app?.collection("users").query.where({
155
- key: "email",
156
- op: "===",
157
- value: email
158
- }).get();
159
- if (data.length > 0) {
160
- throw new Error("Email Already In Use.");
161
- }
162
- const userRef = await app?.collection("users").add({
163
- email,
164
- password,
165
- logged: true,
166
- lastLogged: Date.now(),
167
- displayInfo: {
168
- firstname: email.split("@")[0],
169
- lastname: "",
170
- surname: "",
171
- profile: "./logo.png",
151
+ const res = await new HttpsRequest({
152
+ method: "POST" /* POST */,
153
+ endpoint: `${this.app?.getBaseUrl()}/auth/create`,
154
+ headers: { "authorization": this.app?.getConfig().token ?? "", "x-project": this.app?.getConfig().project ?? "" },
155
+ body: {
172
156
  email,
173
- phone: ""
157
+ password,
158
+ project: this.client?.getConfig().project
174
159
  }
175
- });
176
- if (!userRef?.id || userRef?.id === "") {
177
- throw new Error("Something went wrong try Again.");
160
+ }).sendRequest();
161
+ if (!res.success) {
162
+ throw new Error("Something went wrong. Try Again.");
178
163
  }
164
+ if (res.success === false) {
165
+ throw new Error(res.error ?? "Authentication Failed");
166
+ }
167
+ const userRef = res.result;
179
168
  this.eUser = Credentials.fromMap(userRef);
180
169
  this.saveCredentials(this.eUser);
181
170
  return Credentials.fromMap(userRef.toMap());
@@ -184,28 +173,23 @@ var _Authentication = class _Authentication {
184
173
  email,
185
174
  password
186
175
  }) => {
187
- const app = this.app?.getDatabase;
188
- const data = await app?.collection("users").query.where({
189
- key: "email",
190
- op: "===",
191
- value: email
192
- }).where({
193
- key: "password",
194
- op: "===",
195
- value: password
196
- }).get();
197
- console.log("Auth Data:", data);
198
- if (data === void 0) {
199
- throw new Error("Auth Failed.");
176
+ const res = await new HttpsRequest({
177
+ method: "POST" /* POST */,
178
+ endpoint: `${this.app?.getBaseUrl()}/auth/login`,
179
+ headers: { "authorization": this.app?.getConfig().token ?? "", "x-project": this.app?.getConfig().project ?? "" },
180
+ body: {
181
+ email,
182
+ password,
183
+ project: this.client?.getConfig().project
184
+ }
185
+ }).sendRequest();
186
+ if (!res.success) {
187
+ throw new Error("Something went wrong. Try Again.");
200
188
  }
201
- if (data?.length === 0) {
202
- throw new Error("User Not Found.");
189
+ if (res.success === false) {
190
+ throw new Error(res.error ?? "Authentication Failed");
203
191
  }
204
- const _data = data[0];
205
- await app?.collection("users").doc(_data.id).update({
206
- logged: true,
207
- lastLogged: Date.now()
208
- });
192
+ const _data = res.result;
209
193
  this.eUser = Credentials.fromMap(_data);
210
194
  this.saveCredentials(this.eUser);
211
195
  return Credentials.fromMap(_data);
@@ -214,62 +198,54 @@ var _Authentication = class _Authentication {
214
198
  if (!this.eUser) {
215
199
  throw new Error("No User Signed in");
216
200
  }
217
- const app = this.app?.getDatabase;
218
- const userRef = await app?.collection("users").doc(this.eUser.uid).delete();
219
- if (userRef) {
220
- throw new Error("Something went wrong try Again.");
201
+ const res = await new HttpsRequest({
202
+ method: "POST" /* POST */,
203
+ endpoint: `${this.app?.getBaseUrl()}/auth/delete`,
204
+ headers: { "authorization": this.app?.getConfig().token ?? "", "x-project": this.app?.getConfig().project ?? "" },
205
+ body: {
206
+ uid: this.eUser.uid,
207
+ project: this.client?.getConfig().project
208
+ }
209
+ }).sendRequest();
210
+ if (!res.status) {
211
+ throw new Error("Something went wrong. Try Again.");
212
+ }
213
+ if (res.status === false) {
214
+ throw new Error(res.error ?? "Authentication Failed");
221
215
  }
222
216
  this.eUser = void 0;
223
217
  this.saveCredentials(null);
224
218
  };
225
- this.signOut = async () => {
226
- const app = this.app?.getDatabase;
227
- const luid = this.currentUser();
228
- this.eUser = void 0;
229
- this.saveCredentials(null);
230
- if (luid)
231
- await app?.collection("users").doc(luid?.uid).update({
232
- logged: false,
233
- lastLogged: Date.now()
234
- });
235
- return;
236
- };
237
219
  this.resetPassword = async () => {
238
220
  const app = this.app?.getDatabase;
239
221
  const luid = this.currentUser();
240
- const payload = {
241
- uid: luid?.uid,
242
- ttl: 3
243
- //minutes
244
- };
245
- const id = await app?.collection("_auth_tokens").add(payload);
246
- if (id) {
247
- return `https://auth.edmaxlabs.com/reset/${id}`;
248
- }
249
- throw new Error("Failed to request reset tokens. Try Again");
250
- };
251
- this.rules = async (path, context) => {
252
222
  const res = await new HttpsRequest({
253
223
  method: "POST" /* POST */,
254
- endpoint: this.client.getBaseUrl() + "/auth/rules/verify",
255
- headers: {
256
- authorization: this.client.getConfig().token,
257
- project: this.client.getConfig().project
258
- },
224
+ endpoint: `${this.app?.getBaseUrl()}/auth/reset`,
225
+ headers: { "authorization": this.app?.getConfig().token ?? "", "x-project": this.app?.getConfig().project ?? "" },
259
226
  body: {
260
- path,
261
- context
227
+ uid: luid,
228
+ project: this.client?.getConfig().project
262
229
  }
263
230
  }).sendRequest();
264
- return res;
231
+ if (!res.status) {
232
+ throw new Error("Something went wrong. Try Again.");
233
+ }
234
+ if (res.status === false) {
235
+ throw new Error(res.error ?? "Authentication Failed");
236
+ }
237
+ const url = res.url;
238
+ return url;
239
+ };
240
+ this.signOut = async () => {
241
+ this.eUser = void 0;
242
+ this.saveCredentials(null);
243
+ return;
265
244
  };
266
245
  if (_Authentication.instance)
267
246
  return _Authentication.instance;
268
247
  this.client = EdmaxLabs.instance;
269
- this.app = new EdmaxLabs({
270
- token: "auth",
271
- project: "698457c2f7448550b9e166c4"
272
- });
248
+ this.app = EdmaxLabs.instance;
273
249
  _Authentication.instance = this;
274
250
  this.restoreSession();
275
251
  }
@@ -342,7 +318,7 @@ var _Authentication = class _Authentication {
342
318
  onSignOut?.();
343
319
  return;
344
320
  }
345
- const userRef = this.app?.getDatabase.collection("users").doc(creds.uid);
321
+ const userRef = this.app?.getDatabase.collection("__users").doc(creds.uid);
346
322
  if (!userRef)
347
323
  return;
348
324
  userDocUnsubscribe = userRef.onSnapshot(