@cocreate/users 1.37.6 → 1.38.1

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/src/server.js CHANGED
@@ -1,48 +1,54 @@
1
1
  class CoCreateUser {
2
- constructor(crud) {
3
- this.wsManager = crud.wsManager
4
- this.crud = crud
5
- this.init()
6
- }
7
-
8
- init() {
9
- if (this.wsManager) {
10
- this.wsManager.on('signUp', (data) => this.signUp(data));
11
- this.wsManager.on('signIn', (data) => this.signIn(data))
12
- this.wsManager.on('userStatus', (data) => this.userStatus(data))
13
- this.wsManager.on('checkSession', (data) => this.checkSession(data))
14
- this.wsManager.on('inviteUser', (data) => this.inviteUser(data))
15
- this.wsManager.on('acceptInvite', (data) => this.acceptInvite(data))
16
- this.wsManager.on('forgotPassword', (data) => this.forgotPassword(data))
17
- this.wsManager.on('resetPassword', (data) => this.resetPassword(data))
18
- }
19
- }
20
-
21
- async signUp(data) {
22
- try {
23
- if (data.user) {
24
- data.user.method = 'object.create'
25
- data.user.host = data.host
26
- const response = await this.crud.send(data.user)
27
- this.wsManager.send(response);
28
- }
29
-
30
- if (data.userKey) {
31
- data.userKey.method = 'object.create'
32
- data.userKey.host = data.host
33
- const response = await this.crud.send(data.userKey)
34
- this.wsManager.send(response);
35
- }
36
-
37
- this.wsManager.send(data);
38
-
39
- } catch (error) {
40
- console.log('signup error', error);
41
- }
42
- }
43
-
44
-
45
- /**
2
+ constructor(crud) {
3
+ this.wsManager = crud.wsManager;
4
+ this.crud = crud;
5
+ this.init();
6
+ }
7
+
8
+ init() {
9
+ if (this.wsManager) {
10
+ this.wsManager.on("signUp", (data) => this.signUp(data));
11
+ this.wsManager.on("signIn", (data) => this.signIn(data));
12
+ this.wsManager.on("userStatus", (data) => this.userStatus(data));
13
+ this.wsManager.on("checkSession", (data) =>
14
+ this.checkSession(data)
15
+ );
16
+ this.wsManager.on("inviteUser", (data) => this.inviteUser(data));
17
+ this.wsManager.on("acceptInvite", (data) =>
18
+ this.acceptInvite(data)
19
+ );
20
+ this.wsManager.on("forgotPassword", (data) =>
21
+ this.forgotPassword(data)
22
+ );
23
+ this.wsManager.on("resetPassword", (data) =>
24
+ this.resetPassword(data)
25
+ );
26
+ }
27
+ }
28
+
29
+ async signUp(data) {
30
+ try {
31
+ if (data.user) {
32
+ data.user.method = "object.create";
33
+ data.user.host = data.host;
34
+ const response = await this.crud.send(data.user);
35
+ this.wsManager.send(response);
36
+ }
37
+
38
+ if (data.userKey) {
39
+ data.userKey.method = "object.create";
40
+ data.userKey.host = data.host;
41
+ const response = await this.crud.send(data.userKey);
42
+ this.wsManager.send(response);
43
+ }
44
+
45
+ this.wsManager.send(data);
46
+ } catch (error) {
47
+ console.log("signup error", error);
48
+ }
49
+ }
50
+
51
+ /**
46
52
  data = {
47
53
  namespace: string,
48
54
  array: string,
@@ -52,148 +58,166 @@ class CoCreateUser {
52
58
  organization_id: string
53
59
  }
54
60
  **/
55
- async signIn(data) {
56
- const self = this;
57
- try {
58
- data.method = 'object.read'
59
- let socket = data.socket
60
- delete data.socket
61
-
62
- this.crud.send(data).then(async (data) => {
63
- let response = {
64
- socket,
65
- host: data.host,
66
- method: 'signIn',
67
- success: false,
68
- message: "signIn failed",
69
- userStatus: 'off',
70
- organization_id: data.organization_id,
71
- uid: data.uid
72
- }
73
-
74
- if (data.object[0] && data.object[0]._id && self.wsManager.authenticate) {
75
- const user_id = data.object[0].key
76
- const token = self.wsManager.authenticate.encodeToken(data.organization_id, user_id, data.clientId);
77
-
78
- if (token && token != 'null') {
79
- socket.user_id = user_id
80
- response.success = true
81
- response.message = "signIn successful"
82
- response.userStatus = 'on'
83
- response.user_id = user_id
84
- response.token = token
85
- }
86
- }
87
- self.wsManager.send(response)
88
- self.wsManager.send({
89
- socket,
90
- host: data.host,
91
- method: 'updateUserStatus',
92
- user_id: response.user_id,
93
- userStatus: response.userStatus,
94
- organization_id: response.organization_id
95
- })
96
- })
97
-
98
- } catch (error) {
99
- console.log('signIn failed', error);
100
- }
101
- }
102
-
103
-
104
- /**
105
- * status: 'on/off/idle'
106
- */
107
- async userStatus(data) {
108
- const self = this;
109
- try {
110
- if (data.user_id && data.userStatus) {
111
- data.array = 'users'
112
- data['object'] = {
113
- _id: data.user_id,
114
- userStatus: data.userStatus
115
- }
116
-
117
- data.method = 'object.update'
118
- data = await this.crud.send(data)
119
-
120
- if (data.socket)
121
- self.wsManager.send({
122
- socket: data.socket,
123
- method: 'updateUserStatus',
124
- host: data.host,
125
- user_id: data.user_id,
126
- clientId: data.clientId,
127
- userStatus: data.userStatus,
128
- token: data.token,
129
- organization_id: data.organization_id || socket.organization_id
130
- })
131
- } else if (data.socket)
132
- data.socket.send(JSON.stringify({
133
- method: 'updateUserStatus',
134
- host: data.host,
135
- user_id: data.user_id,
136
- userStatus: data.userStatus,
137
- clientId: data.clientId,
138
- token: data.token,
139
- organization_id: data.organization_id || socket.organization_id
140
- }))
141
-
142
-
143
- } catch (error) {
144
- console.log('userStatus error')
145
- }
146
- }
147
-
148
- async checkSession(data) {
149
- try {
150
- data.method = 'updateUserStatus'
151
-
152
- if (!data.socket.user_id)
153
- data.userStatus = 'off'
154
- else
155
- data.userStatus = 'on'
156
-
157
- this.wsManager.send(data)
158
- } catch (error) {
159
- console.log('checkSession error')
160
- }
161
- }
162
-
163
- // TODO: email or phone param refrencing the object to execute via api
164
- async inviteUser(data) {
165
- try {
166
- const inviteId = this.crud.ObjectId().toString()
167
- let uid = data.uid
168
- delete data.uid
169
-
170
- data.method = 'object.update'
171
- data.array = "users"
172
- data.object = { _id: data.user_id, '$push.invitations': inviteId, '$addToSet.members': data.email }
173
-
174
- // TODO: upodateDB is a temp solution to by pass crud.sync clientId as all crud methods are ignored fro same clientid except for read
175
- data.updateDB = true
176
-
177
- data = await this.crud.send(data)
178
-
179
-
180
- let invitee = await this.crud.send({
181
- method: 'object.read',
182
- host: data.host,
183
- array: 'users',
184
- $filter: {
185
- query: { email: data.email },
186
- limit: 1
187
- },
188
- organization_id: data.organization_id
189
- })
190
-
191
- if (invitee.object[0]) {
192
- invitee = invitee.object[0]._id
193
- } else
194
- invitee = ''
195
-
196
- let htmlBody = `
61
+ async signIn(data) {
62
+ const self = this;
63
+ try {
64
+ data.method = "object.read";
65
+ data.array = "keys";
66
+ let socket = data.socket;
67
+ delete data.socket;
68
+
69
+ // TODO: Enforce query from array keys $filter.query = {email, phone, username, password, key }
70
+ // Get crud properties from returned key and enforce query $filter.query = {database, array, email, phone, username, password, key }
71
+ // or use webhook to update keys credintials when user updates ther user
72
+ // Also enforce roles by webhook else role could be spoofed,
73
+ // Also enforce admin privlige
74
+
75
+ this.crud.send(data).then(async (data) => {
76
+ let response = {
77
+ socket,
78
+ host: data.host,
79
+ method: "signIn",
80
+ success: false,
81
+ message: "signIn failed",
82
+ userStatus: "off",
83
+ organization_id: data.organization_id,
84
+ uid: data.uid
85
+ };
86
+
87
+ if (
88
+ data.object[0] &&
89
+ data.object[0]._id &&
90
+ self.wsManager.authenticate
91
+ ) {
92
+ const user_id = data.object[0].key;
93
+ // TODO: get key to then get crud properties from key to intiate signin
94
+ // const { storage, database, array, key } = data.object[0];
95
+ // data = await this.crud.send(data);
96
+ const token = self.wsManager.authenticate.encodeToken(
97
+ data.organization_id,
98
+ user_id,
99
+ data.clientId
100
+ );
101
+
102
+ if (token && token != "null") {
103
+ socket.user_id = user_id;
104
+ response.success = true;
105
+ response.message = "signIn successful";
106
+ response.userStatus = "on";
107
+ response.user_id = user_id;
108
+ response.token = token;
109
+ }
110
+ }
111
+ self.wsManager.send(response);
112
+ self.wsManager.send({
113
+ socket,
114
+ host: data.host,
115
+ method: "updateUserStatus",
116
+ user_id: response.user_id,
117
+ userStatus: response.userStatus,
118
+ organization_id: response.organization_id
119
+ });
120
+ });
121
+ } catch (error) {
122
+ console.log("signIn failed", error);
123
+ }
124
+ }
125
+
126
+ /**
127
+ * status: 'on/off/idle'
128
+ */
129
+ async userStatus(data) {
130
+ const self = this;
131
+ try {
132
+ if (data.user_id && data.userStatus) {
133
+ data.array = "users";
134
+ data["object"] = {
135
+ _id: data.user_id,
136
+ userStatus: data.userStatus
137
+ };
138
+
139
+ data.method = "object.update";
140
+ data = await this.crud.send(data);
141
+
142
+ if (data.socket)
143
+ self.wsManager.send({
144
+ socket: data.socket,
145
+ method: "updateUserStatus",
146
+ host: data.host,
147
+ user_id: data.user_id,
148
+ clientId: data.clientId,
149
+ userStatus: data.userStatus,
150
+ token: data.token,
151
+ organization_id:
152
+ data.organization_id || socket.organization_id
153
+ });
154
+ } else if (data.socket)
155
+ data.socket.send(
156
+ JSON.stringify({
157
+ method: "updateUserStatus",
158
+ host: data.host,
159
+ user_id: data.user_id,
160
+ userStatus: data.userStatus,
161
+ clientId: data.clientId,
162
+ token: data.token,
163
+ organization_id:
164
+ data.organization_id || socket.organization_id
165
+ })
166
+ );
167
+ } catch (error) {
168
+ console.log("userStatus error");
169
+ }
170
+ }
171
+
172
+ async checkSession(data) {
173
+ try {
174
+ data.method = "updateUserStatus";
175
+
176
+ if (!data.socket.user_id) data.userStatus = "off";
177
+ else data.userStatus = "on";
178
+
179
+ this.wsManager.send(data);
180
+ } catch (error) {
181
+ console.log("checkSession error");
182
+ }
183
+ }
184
+
185
+ // TODO: email or phone param refrencing the object to execute via api
186
+ async inviteUser(data) {
187
+ try {
188
+ const inviteId = this.crud.ObjectId().toString();
189
+ let uid = data.uid;
190
+ delete data.uid;
191
+
192
+ data.method = "object.update";
193
+ data.array = "users";
194
+ data.object = {
195
+ _id: data.user_id,
196
+ "$push.invitations": inviteId,
197
+ "$addToSet.members": data.email
198
+ };
199
+
200
+ // TODO: upodateDB is a temp solution to by pass crud.sync clientId as all crud methods are ignored fro same clientid except for read
201
+ data.updateDB = true;
202
+
203
+ data = await this.crud.send(data);
204
+
205
+ let invitee = await this.crud.send({
206
+ method: "object.read",
207
+ host: data.host,
208
+ array: "users",
209
+ $filter: {
210
+ query: { email: data.email },
211
+ limit: 1
212
+ },
213
+ organization_id: data.organization_id
214
+ });
215
+
216
+ if (invitee.object[0]) {
217
+ invitee = invitee.object[0]._id;
218
+ } else invitee = "";
219
+
220
+ let htmlBody = `
197
221
  <html>
198
222
  <head>
199
223
  <title>Welcome to Yellow Oracle!</title>
@@ -218,132 +242,147 @@ class CoCreateUser {
218
242
  </html>
219
243
  `;
220
244
 
221
- let email = {
222
- method: 'postmark.sendEmail',
223
- host: data.host,
224
- postmark: {
225
- "From": data.from,
226
- "To": data.email,
227
- "Subject": `${data.name} has invited you to join them on Yellow Oracle`,
228
- "HtmlBody": htmlBody,
229
- "TextBody": "Hello, \n\nWe received a request to reset the password for your account.If you did not make this request, please ignore this email.Otherwise, you can reset your password by copying and pasting the following link into your browser: https://example.com/reset-password\n\nThis link will expire in 24 hours for your security.\n\nNeed more help? Our support team is here for you at support@example.com.\n\nThank you for using our services!\n\nBest regards,\nThe [Your Company] Team",
230
- "MessageStream": "outbound"
231
- },
232
- organization_id: data.organization_id
233
- }
234
-
235
- // TODO: wsManager.emit('postmark', email) needs to await response
236
- this.wsManager.emit('postmark', email);
237
-
238
- let response = {
239
- socket: data.socket,
240
- host: data.host,
241
- method: 'inviteUser',
242
- success: true,
243
- message: "Succesfully sent invite",
244
- organization_id: data.organization_id,
245
- uid
246
- }
247
-
248
- this.wsManager.send(response)
249
-
250
- } catch (error) {
251
- data.error = error
252
- this.wsManager.send(data)
253
-
254
- console.log('Invite User failed', error);
255
- }
256
- }
257
-
258
- async acceptInvite(data) {
259
- const self = this;
260
- try {
261
- if (!data.token || !data.user_id) {
262
- // TODO: handle error
263
- return
264
- }
265
-
266
- let uid = data.uid
267
- delete data.uid
268
-
269
- data.method = 'object.update'
270
- data.array = "users"
271
- data.object = { '$pull.invitations': data.token, '$pull.members': data.email }
272
- data.$filter = {
273
- query: {
274
- invitations: { $in: [data.token] },
275
- members: { $in: [data.email] }
276
- },
277
- limit: 2
278
- }
279
-
280
- // TODO: upodateDB is a temp solution to by pass crud.sync clientId as all crud methods are ignored fro same clientid except for read
281
- data.updateDB = true
282
-
283
- data = await this.crud.send(data)
284
-
285
- let response = {
286
- socket: data.socket,
287
- host: data.host,
288
- method: 'acceptInvite',
289
- success: false,
290
- message: "The token is invalid or has expired. However, you can still access your account. Please request a new invite to proceed, and feel free to explore the available features while you wait.",
291
- organization_id: data.organization_id,
292
- uid
293
- }
294
-
295
- for (let object of data.object) {
296
- if (object._id) {
297
- delete data.$filter
298
- data.object = { _id: object._id, '$addToSet.members': data.user_id }
299
- data = await this.crud.send(data)
300
- this.crud.send({ method: 'object.update', host: data.host, array: 'users', object: { _id: data.user_id, memberAccount: object._id, subscription: '6571fe530c48ef6970900a82' } })
301
- response.success = true
302
- response.message = "Invite Accepted"
303
- break
304
- }
305
- }
306
-
307
- self.wsManager.send(response)
308
-
309
- } catch (error) {
310
- console.log("Accept invite failed", error);
311
- }
312
- }
313
-
314
- // TODO: email or phone param refrencing the object to execute via api
315
- async forgotPassword(data) {
316
- const self = this;
317
- try {
318
- const recoveryId = this.crud.ObjectId().toString()
319
- let socket = data.socket
320
- delete data.socket
321
-
322
- data.method = 'object.update'
323
- data.array = "keys"
324
- data.object = { recoveryId }
325
- data.$filter = {
326
- query: { email: data.email },
327
- limit: 1
328
- }
329
-
330
- this.crud.send(data).then(async (data) => {
331
- let response = {
332
- socket,
333
- host: data.host,
334
- method: 'forgotPassword',
335
- success: false,
336
- message: "Email does not exist",
337
- organization_id: data.organization_id,
338
- uid: data.uid
339
- }
340
-
341
- for (let object of data.object) {
342
- if (object._id) {
343
- // TODO: sendEmail
344
- response.success = true
345
- response.message = "Email Sent"
346
- let htmlBody = `
245
+ let email = {
246
+ method: "postmark.sendEmail",
247
+ host: data.host,
248
+ postmark: {
249
+ From: data.from,
250
+ To: data.email,
251
+ Subject: `${data.name} has invited you to join them on Yellow Oracle`,
252
+ HtmlBody: htmlBody,
253
+ TextBody:
254
+ "Hello, \n\nWe received a request to reset the password for your account.If you did not make this request, please ignore this email.Otherwise, you can reset your password by copying and pasting the following link into your browser: https://example.com/reset-password\n\nThis link will expire in 24 hours for your security.\n\nNeed more help? Our support team is here for you at support@example.com.\n\nThank you for using our services!\n\nBest regards,\nThe [Your Company] Team",
255
+ MessageStream: "outbound"
256
+ },
257
+ organization_id: data.organization_id
258
+ };
259
+
260
+ // TODO: wsManager.emit('postmark', email) needs to await response
261
+ this.wsManager.emit("postmark", email);
262
+
263
+ let response = {
264
+ socket: data.socket,
265
+ host: data.host,
266
+ method: "inviteUser",
267
+ success: true,
268
+ message: "Succesfully sent invite",
269
+ organization_id: data.organization_id,
270
+ uid
271
+ };
272
+
273
+ this.wsManager.send(response);
274
+ } catch (error) {
275
+ data.error = error;
276
+ this.wsManager.send(data);
277
+
278
+ console.log("Invite User failed", error);
279
+ }
280
+ }
281
+
282
+ async acceptInvite(data) {
283
+ const self = this;
284
+ try {
285
+ if (!data.token || !data.user_id) {
286
+ // TODO: handle error
287
+ return;
288
+ }
289
+
290
+ let uid = data.uid;
291
+ delete data.uid;
292
+
293
+ data.method = "object.update";
294
+ data.array = "users";
295
+ data.object = {
296
+ "$pull.invitations": data.token,
297
+ "$pull.members": data.email
298
+ };
299
+ data.$filter = {
300
+ query: {
301
+ invitations: { $in: [data.token] },
302
+ members: { $in: [data.email] }
303
+ },
304
+ limit: 2
305
+ };
306
+
307
+ // TODO: upodateDB is a temp solution to by pass crud.sync clientId as all crud methods are ignored fro same clientid except for read
308
+ data.updateDB = true;
309
+
310
+ data = await this.crud.send(data);
311
+
312
+ let response = {
313
+ socket: data.socket,
314
+ host: data.host,
315
+ method: "acceptInvite",
316
+ success: false,
317
+ message:
318
+ "The token is invalid or has expired. However, you can still access your account. Please request a new invite to proceed, and feel free to explore the available features while you wait.",
319
+ organization_id: data.organization_id,
320
+ uid
321
+ };
322
+
323
+ for (let object of data.object) {
324
+ if (object._id) {
325
+ delete data.$filter;
326
+ data.object = {
327
+ _id: object._id,
328
+ "$addToSet.members": data.user_id
329
+ };
330
+ data = await this.crud.send(data);
331
+ this.crud.send({
332
+ method: "object.update",
333
+ host: data.host,
334
+ array: "users",
335
+ object: {
336
+ _id: data.user_id,
337
+ memberAccount: object._id,
338
+ subscription: "6571fe530c48ef6970900a82"
339
+ }
340
+ });
341
+ response.success = true;
342
+ response.message = "Invite Accepted";
343
+ break;
344
+ }
345
+ }
346
+
347
+ self.wsManager.send(response);
348
+ } catch (error) {
349
+ console.log("Accept invite failed", error);
350
+ }
351
+ }
352
+
353
+ // TODO: email or phone param refrencing the object to execute via api
354
+ async forgotPassword(data) {
355
+ const self = this;
356
+ try {
357
+ const recoveryId = this.crud.ObjectId().toString();
358
+ let socket = data.socket;
359
+ delete data.socket;
360
+
361
+ data.method = "object.update";
362
+ data.array = "keys";
363
+ data.object = { recoveryId };
364
+ data.$filter = {
365
+ query: { email: data.email },
366
+ limit: 1
367
+ };
368
+
369
+ this.crud.send(data).then(async (data) => {
370
+ let response = {
371
+ socket,
372
+ host: data.host,
373
+ method: "forgotPassword",
374
+ success: false,
375
+ message: "Email does not exist",
376
+ organization_id: data.organization_id,
377
+ uid: data.uid
378
+ };
379
+
380
+ for (let object of data.object) {
381
+ if (object._id) {
382
+ // TODO: sendEmail
383
+ response.success = true;
384
+ response.message = "Email Sent";
385
+ let htmlBody = `
347
386
  <html>
348
387
  <head>
349
388
  <title>Reset Your Password</title>
@@ -366,75 +405,74 @@ class CoCreateUser {
366
405
 
367
406
  </body>
368
407
  </html>
369
- `
370
- let email = {
371
- method: 'postmark.sendEmail',
372
- host: data.host,
373
- postmark: {
374
- "From": data.from,
375
- "To": data.email,
376
- "Subject": "Reset Your Password Easily",
377
- "HtmlBody": htmlBody,
378
- "TextBody": "Hello, \n\nWe received a request to reset the password for your account. If you did not make this request, please ignore this email. Otherwise, you can reset your password by copying and pasting the following link into your browser: https://example.com/reset-password\n\nThis link will expire in 24 hours for your security.\n\nNeed more help? Our support team is here for you at support@example.com.\n\nThank you for using our services!\n\nBest regards,\nThe [Your Company] Team",
379
- "MessageStream": "outbound"
380
- },
381
- organization_id: data.organization_id
382
- }
383
-
384
- // TODO: wsManager.emit('postmark', email) needs to await response
385
- self.wsManager.emit('postmark', email);
386
-
387
- break
388
- }
389
- }
390
-
391
- self.wsManager.send(response)
392
- })
393
-
394
- } catch (error) {
395
- console.log('Forgot Password failed', error);
396
- }
397
- }
398
-
399
- async resetPassword(data) {
400
- const self = this;
401
- try {
402
- if (!data.email || !data.password || !data.token)
403
- return
404
-
405
- data.method = 'object.update'
406
- data.array = "keys"
407
- data.object = { password: data.password, recoveryId: "" }
408
- data.$filter = {
409
- query: { email: data.email, recoveryId: data.token },
410
- limit: 1
411
- }
412
-
413
- this.crud.send(data).then(async (data) => {
414
- let response = {
415
- socket: data.socket,
416
- host: data.host,
417
- method: 'resetPassword',
418
- success: false,
419
- message: "Token is invalid or has expired",
420
- organization_id: data.organization_id,
421
- uid: data.uid
422
- }
423
-
424
- for (let object of data.object) {
425
- if (object._id) {
426
- response.success = true
427
- response.message = "Password reset succesfull"
428
- break
429
- }
430
- }
431
-
432
- self.wsManager.send(response)
433
- })
434
- } catch (error) {
435
- console.log("Password reset failed", error);
436
- }
437
- }
408
+ `;
409
+ let email = {
410
+ method: "postmark.sendEmail",
411
+ host: data.host,
412
+ postmark: {
413
+ From: data.from,
414
+ To: data.email,
415
+ Subject: "Reset Your Password Easily",
416
+ HtmlBody: htmlBody,
417
+ TextBody:
418
+ "Hello, \n\nWe received a request to reset the password for your account. If you did not make this request, please ignore this email. Otherwise, you can reset your password by copying and pasting the following link into your browser: https://example.com/reset-password\n\nThis link will expire in 24 hours for your security.\n\nNeed more help? Our support team is here for you at support@example.com.\n\nThank you for using our services!\n\nBest regards,\nThe [Your Company] Team",
419
+ MessageStream: "outbound"
420
+ },
421
+ organization_id: data.organization_id
422
+ };
423
+
424
+ // TODO: wsManager.emit('postmark', email) needs to await response
425
+ self.wsManager.emit("postmark", email);
426
+
427
+ break;
428
+ }
429
+ }
430
+
431
+ self.wsManager.send(response);
432
+ });
433
+ } catch (error) {
434
+ console.log("Forgot Password failed", error);
435
+ }
436
+ }
437
+
438
+ async resetPassword(data) {
439
+ const self = this;
440
+ try {
441
+ if (!data.email || !data.password || !data.token) return;
442
+
443
+ data.method = "object.update";
444
+ data.array = "keys";
445
+ data.object = { password: data.password, recoveryId: "" };
446
+ data.$filter = {
447
+ query: { email: data.email, recoveryId: data.token },
448
+ limit: 1
449
+ };
450
+
451
+ this.crud.send(data).then(async (data) => {
452
+ let response = {
453
+ socket: data.socket,
454
+ host: data.host,
455
+ method: "resetPassword",
456
+ success: false,
457
+ message: "Token is invalid or has expired",
458
+ organization_id: data.organization_id,
459
+ uid: data.uid
460
+ };
461
+
462
+ for (let object of data.object) {
463
+ if (object._id) {
464
+ response.success = true;
465
+ response.message = "Password reset succesfull";
466
+ break;
467
+ }
468
+ }
469
+
470
+ self.wsManager.send(response);
471
+ });
472
+ } catch (error) {
473
+ console.log("Password reset failed", error);
474
+ }
475
+ }
438
476
  }
439
477
 
440
- module.exports = CoCreateUser;
478
+ module.exports = CoCreateUser;