@cocreate/users 1.39.4 → 1.40.0

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,314 +1,340 @@
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) =>
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
- /**
30
- * Handles the user sign-up process by performing identity validation,
31
- * contact record synchronization, and credential attachment.
32
- * * @async
33
- * @function signUp
34
- * @param {Object} data - The sign-up data payload.
35
- * @param {Object} data.socket - The client socket identifier.
36
- * @param {string} data.host - The host address.
37
- * @param {string} data.organization_id - The ID of the organization.
38
- * @param {string} data.uid - Unique identifier for the request.
39
- * @param {Object} [data.user] - The User/Contact object to be created or updated.
40
- * @param {Object} data.user.object - The actual contact data (name, email, _id, etc.).
41
- * @param {Object} [data.userKey] - The credential/login object to be created.
42
- * @param {Object} data.userKey.object - The credential data (password, etc.).
43
- * @returns {Promise<void>} - Sends the result status via this.wsManager.
44
- */
45
-
46
- async signUp(data) {
47
- try {
48
- let response = {
49
- socket: data.socket,
50
- host: data.host,
51
- method: "signUp",
52
- success: false,
53
- message: "signUp failed",
54
- organization_id: data.organization_id,
55
- uid: data.uid
56
- };
57
-
58
- const targetEmail = data.user?.object?.email;
59
- const targetId = data.user?.object?._id;
60
-
61
- // --- STEP 1: The Ultimate Gatekeeper Check ---
62
- // We look in the 'keys' array to see if this email OR this ID is already registered.
63
- // If they pass this test, we are clear to proceed with creation.
64
- const keyCheck = await this.crud.send({
65
- method: "object.read",
66
- array: "keys",
67
- $filter: {
68
- limit: 1,
69
- query: {
70
- $or: [
71
- { "email": targetEmail },
72
- { "key": targetId }
73
- ]
74
- }
75
- }
76
- });
77
-
78
- // Detailed error messaging for the user
79
- if (keyCheck?.object?.length > 0) {
80
- const match = keyCheck.object[0];
81
-
82
- if (match.email === targetEmail && match.key !== targetId) {
83
- response.message = "Email is already in use with another account";
84
- } else if (match.key === targetId) {
85
- response.message = "You already have an account, try signing in instead";
86
- } else {
87
- response.message = "An account with these details already exists";
88
- }
89
-
90
- this.wsManager.send(response);
91
- return; // Exit early: Gatekeeper blocked the sign-up.
92
- }
93
-
94
- // --- STEP 2: Resolve or Create the Contact ---
95
- // Since Step 1 passed, we can safely upsert the contact record.
96
- if (data.user) {
97
- data.user.method = "object.update";
98
- data.user.host = data.host;
99
- data.user.upsert = true;
100
-
101
- if (!data.user.organization_id) {
102
- data.user.organization_id = data.organization_id;
103
- }
104
-
105
- let userResult = await this.crud.send(data.user);
106
-
107
- // --- STEP 3: Create the Key ---
108
- // Uniqueness is already guaranteed by Step 1, so we just perform the creation.
109
- if (data.userKey && userResult.object?.[0]?._id) {
110
- const resolvedUserId = userResult.object[0]._id;
111
-
112
- data.userKey.method = "object.create";
113
- data.userKey.host = data.host;
114
- data.userKey.object.key = resolvedUserId;
115
- data.userKey.object.email = targetEmail;
116
-
117
- if (!data.userKey.organization_id) {
118
- data.userKey.organization_id = data.organization_id;
119
- }
120
-
121
- // Get the result and check for an ID to confirm success
122
- let createdKey = await this.crud.send(data.userKey);
123
-
124
- if (createdKey.object?.[0]?._id) {
125
- response.success = true;
126
- response.message = "signUp successful";
127
- } else {
128
- response.message = "Account creation failed at credential stage";
129
- }
130
- } else {
131
- response.message = "Account creation failed at user stage";
132
- }
133
- }
134
-
135
- this.wsManager.send(response);
136
-
137
- } catch (error) {
138
- console.log("signup error", error);
139
- }
140
- }
141
-
142
- /**
143
- data = {
144
- namespace: string,
145
- array: string,
146
- data: object,
147
- eId: string,
148
- key: string,
149
- organization_id: string
1
+ /********************************************************************************
2
+ * Copyright (C) 2023 CoCreate and Contributors.
3
+ *
4
+ * This program is free software: you can redistribute it and/or modify
5
+ * it under the terms of the GNU Affero General Public License as published
6
+ * by the Free Software Foundation, either version 3 of the License, or
7
+ * (at your option) any later version.
8
+ *
9
+ * This program is distributed in the hope that it will be useful,
10
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ * GNU Affero General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU Affero General Public License
15
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
+ *
17
+ ********************************************************************************/
18
+
19
+ // Commercial Licensing Information:
20
+ // For commercial use of this software without the copyleft provisions of the AGPLv3,
21
+ // you must obtain a commercial license from CoCreate LLC.
22
+ // For details, visit <https://cocreate.app/licenses/> or contact us at sales@cocreate.app.
23
+
24
+ import server from '@cocreate/server';
25
+
26
+ /**
27
+ * Handles the user sign-up process by performing identity validation,
28
+ * contact record synchronization, and credential attachment.
29
+ */
30
+ async function signUp(data) {
31
+ try {
32
+ let response = {
33
+ socket: data.socket,
34
+ host: data.host,
35
+ method: "signUp",
36
+ success: false,
37
+ message: "signUp failed",
38
+ organization_id: data.organization_id,
39
+ uid: data.uid
40
+ };
41
+
42
+ const targetEmail = data.user?.object?.email;
43
+ const targetId = data.user?.object?._id;
44
+
45
+ // --- STEP 1: The Ultimate Gatekeeper Check ---
46
+ // We look in the 'keys' array to see if this email OR this ID is already registered.
47
+ if (!server || !server.crud) return;
48
+
49
+ const keyCheck = await server.crud.send({
50
+ method: "object.read",
51
+ array: "keys",
52
+ $filter: {
53
+ limit: 1,
54
+ query: {
55
+ $or: [
56
+ { "email": targetEmail },
57
+ { "key": targetId }
58
+ ]
59
+ }
60
+ }
61
+ });
62
+
63
+ // Detailed error messaging for the user
64
+ if (keyCheck?.object?.length > 0) {
65
+ const match = keyCheck.object[0];
66
+
67
+ if (match.email === targetEmail && match.key !== targetId) {
68
+ response.message = "Email is already in use with another account";
69
+ } else if (match.key === targetId) {
70
+ response.message = "You already have an account, try signing in instead";
71
+ } else {
72
+ response.message = "An account with these details already exists";
73
+ }
74
+
75
+ if (server.wsManager) {
76
+ server.wsManager.send(response);
77
+ }
78
+ return; // Exit early: Gatekeeper blocked the sign-up.
150
79
  }
151
- **/
152
- async signIn(data) {
153
- const self = this;
154
- try {
155
- data.method = "object.read";
156
- data.array = "keys";
157
- let socket = data.socket;
158
- delete data.socket;
159
-
160
- // TODO: Enforce query from array keys $filter.query = {email, phone, username, password, key }
161
- // Get crud properties from returned key and enforce query $filter.query = {database, array, email, phone, username, password, key }
162
- // or use webhook to update keys credintials when user updates ther user
163
- // Also enforce roles by webhook else role could be spoofed,
164
- // Also enforce admin privlige
165
-
166
- this.crud.send(data).then(async (data) => {
167
- let response = {
168
- socket,
169
- host: data.host,
170
- method: "signIn",
171
- success: false,
172
- message: "signIn failed",
173
- userStatus: "offline",
174
- organization_id: data.organization_id,
175
- uid: data.uid
176
- };
177
-
178
- if (
179
- data.object[0] &&
180
- data.object[0]._id &&
181
- self.wsManager.authenticate
182
- ) {
183
- const user_id = data.object[0].key;
184
- // TODO: get key to then get crud properties from key to intiate signin
185
- // const { storage, database, array, key } = data.object[0];
186
- // data = await this.crud.send(data);
187
- const token = self.wsManager.authenticate.encodeToken(
188
- data.organization_id,
189
- user_id,
190
- data.clientId
191
- );
192
-
193
- if (token && token != "null") {
194
- socket.user_id = user_id;
195
- response.success = true;
196
- response.message = "signIn successful";
197
- response.userStatus = "online";
198
- response.user_id = user_id;
199
- response.token = token;
200
- }
201
- }
202
- self.wsManager.send(response);
203
- self.wsManager.send({
204
- socket,
205
- host: data.host,
206
- method: "updateUserStatus",
207
- user_id: response.user_id,
208
- userStatus: response.userStatus,
209
- organization_id: response.organization_id
210
- });
211
- });
212
- } catch (error) {
213
- console.log("signIn failed", error);
214
- }
215
- }
216
-
217
- /**
218
- * status: 'online/offline/idle'
219
- */
220
- async userStatus(data) {
221
- const self = this;
222
- try {
223
- if (data.user_id && data.userStatus) {
224
- data.array = "users";
225
- data["object"] = {
226
- _id: data.user_id,
227
- userStatus: data.userStatus
228
- };
229
-
230
- data.method = "object.update";
231
- data = await this.crud.send(data);
232
-
233
- if (data.socket)
234
- self.wsManager.send({
235
- socket: data.socket,
236
- method: "updateUserStatus",
237
- host: data.host,
238
- user_id: data.user_id,
239
- clientId: data.clientId,
240
- userStatus: data.userStatus,
241
- token: data.token,
242
- organization_id:
243
- data.organization_id || socket.organization_id
244
- });
245
- } else if (data.socket)
246
- data.socket.send(
247
- JSON.stringify({
248
- method: "updateUserStatus",
249
- host: data.host,
250
- user_id: data.user_id,
251
- userStatus: data.userStatus,
252
- clientId: data.clientId,
253
- token: data.token,
254
- organization_id:
255
- data.organization_id || socket.organization_id
256
- })
257
- );
258
- } catch (error) {
259
- console.log("userStatus error");
260
- }
261
- }
262
-
263
- async checkSession(data) {
264
- try {
265
- data.method = "updateUserStatus";
266
-
267
- if (!data.socket.user_id) data.userStatus = "offline";
268
- else data.userStatus = "online";
269
-
270
- this.wsManager.send(data);
271
- } catch (error) {
272
- console.log("checkSession error");
273
- }
274
- }
275
-
276
- // TODO: email or phone param refrencing the object to execute via api
277
- async inviteUser(data) {
278
- try {
279
- const inviteId = this.crud.ObjectId().toString();
280
- let uid = data.uid;
281
- delete data.uid;
282
-
283
- data.method = "object.update";
284
- data.array = "users";
285
- data.object = {
286
- _id: data.user_id,
287
- "$push.invitations": inviteId,
288
- "$addToSet.members": data.email
289
- };
290
-
291
- // TODO: upodateDB is a temp solution to by pass crud.sync clientId as all crud methods are ignored fro same clientid except for read
292
- data.updateDB = true;
293
-
294
- data = await this.crud.send(data);
295
-
296
- let invitee = await this.crud.send({
297
- method: "object.read",
298
- host: data.host,
299
- array: "users",
300
- $filter: {
301
- query: { email: data.email },
302
- limit: 1
303
- },
304
- organization_id: data.organization_id
305
- });
306
-
307
- if (invitee.object[0]) {
308
- invitee = invitee.object[0]._id;
309
- } else invitee = "";
310
-
311
- let htmlBody = `
80
+
81
+ // --- STEP 2: Resolve or Create the Contact ---
82
+ if (data.user) {
83
+ data.user.method = "object.create";
84
+ data.user.host = data.host;
85
+
86
+ if (!data.user.organization_id) {
87
+ data.user.organization_id = data.organization_id;
88
+ }
89
+
90
+ if (data.user.object) {
91
+ delete data.user.object.password; // Remove password attribute to avoid plain text database leakage
92
+ }
93
+
94
+ let userResult = await server.crud.send(data.user);
95
+
96
+ // --- STEP 3: Create the Key ---
97
+ if (data.userKey && userResult.object?.[0]?._id) {
98
+ const resolvedUserId = userResult.object[0]._id;
99
+
100
+ data.userKey.method = "object.create";
101
+ data.userKey.host = data.host;
102
+ data.userKey.object.key = resolvedUserId;
103
+ data.userKey.object.email = targetEmail;
104
+
105
+ if (!data.userKey.organization_id) {
106
+ data.userKey.organization_id = data.organization_id;
107
+ }
108
+
109
+ // Get the result and check for an ID to confirm success
110
+ let createdKey = await server.crud.send(data.userKey);
111
+
112
+ if (createdKey.object?.[0]?._id) {
113
+ response.success = true;
114
+ response.message = "signUp successful";
115
+ } else {
116
+ response.message = "Account creation failed at credential stage";
117
+ }
118
+ } else {
119
+ response.message = "Account creation failed at user stage";
120
+ }
121
+ }
122
+
123
+ if (server.wsManager) {
124
+ server.wsManager.send(response);
125
+ }
126
+
127
+ } catch (error) {
128
+ console.error("signup error", error);
129
+ }
130
+ }
131
+
132
+ /**
133
+ * Executes credential matching queries, assigns socket-level identifiers,
134
+ * and encodes secure session web tokens.
135
+ */
136
+ async function signIn(data) {
137
+ try {
138
+ if (!server || !server.crud) return;
139
+
140
+ data.method = "object.read";
141
+ data.array = "keys";
142
+ let socket = data.socket;
143
+ delete data.socket;
144
+
145
+ const result = await server.crud.send(data);
146
+
147
+ let response = {
148
+ socket,
149
+ host: result.host,
150
+ method: "signIn",
151
+ success: false,
152
+ message: "signIn failed",
153
+ userStatus: "offline",
154
+ organization_id: result.organization_id,
155
+ uid: result.uid
156
+ };
157
+
158
+ if (
159
+ result.object[0] &&
160
+ result.object[0]._id &&
161
+ server.wsManager &&
162
+ server.authenticate
163
+ ) {
164
+ const user_id = result.object[0].key;
165
+ const token = server.authenticate.encodeToken(
166
+ result.organization_id,
167
+ user_id,
168
+ result.clientId
169
+ );
170
+
171
+ if (token && token !== "null") {
172
+ socket.user_id = user_id;
173
+ response.success = true;
174
+ response.message = "signIn successful";
175
+ response.userStatus = "online";
176
+ response.user_id = user_id;
177
+ response.token = token;
178
+ }
179
+ }
180
+
181
+ if (server.wsManager) {
182
+ server.wsManager.send(response);
183
+ server.wsManager.send({
184
+ socket,
185
+ host: result.host,
186
+ method: "updateUserStatus",
187
+ user_id: response.user_id,
188
+ userStatus: response.userStatus,
189
+ organization_id: response.organization_id
190
+ });
191
+ }
192
+ } catch (error) {
193
+ console.error("signIn failed", error);
194
+ }
195
+ }
196
+
197
+ /**
198
+ * Updates dynamic user activity indicators ('online', 'offline', 'idle').
199
+ */
200
+ async function userStatus(data) {
201
+ try {
202
+ if (!server || !server.crud) return;
203
+
204
+ if (data.user_id && data.userStatus) {
205
+ let targetArray = "users"; // default fallback
206
+ let keyData = await server.crud.send({
207
+ method: "object.read",
208
+ host: data.host,
209
+ array: "keys",
210
+ $filter: {
211
+ query: { key: data.user_id },
212
+ limit: 1
213
+ },
214
+ organization_id: data.organization_id || (data.socket && data.socket.organization_id)
215
+ });
216
+
217
+ if (keyData && keyData.object && keyData.object.length > 0 && keyData.object[0].array) {
218
+ targetArray = keyData.object[0].array;
219
+ }
220
+
221
+ data.array = targetArray;
222
+ data["object"] = {
223
+ _id: data.user_id,
224
+ userStatus: data.userStatus
225
+ };
226
+
227
+ data.method = "object.update";
228
+ const updatedData = await server.crud.send(data);
229
+
230
+ if (updatedData.socket && server.wsManager) {
231
+ server.wsManager.send({
232
+ socket: updatedData.socket,
233
+ method: "updateUserStatus",
234
+ host: updatedData.host,
235
+ user_id: updatedData.user_id,
236
+ clientId: updatedData.clientId,
237
+ userStatus: updatedData.userStatus,
238
+ token: updatedData.token,
239
+ organization_id:
240
+ updatedData.organization_id || (updatedData.socket && updatedData.socket.organization_id)
241
+ });
242
+ }
243
+ } else if (data.socket) {
244
+ data.socket.send(
245
+ JSON.stringify({
246
+ method: "updateUserStatus",
247
+ host: data.host,
248
+ user_id: data.user_id,
249
+ userStatus: data.userStatus,
250
+ clientId: data.clientId,
251
+ token: data.token,
252
+ organization_id:
253
+ data.organization_id || (data.socket && data.socket.organization_id)
254
+ })
255
+ );
256
+ }
257
+ } catch (error) {
258
+ console.error("userStatus error", error);
259
+ }
260
+ }
261
+
262
+ /**
263
+ * Quickly checks for valid runtime credentials assigned to active client connections.
264
+ */
265
+ async function checkSession(data) {
266
+ try {
267
+ data.method = "updateUserStatus";
268
+
269
+ if (!data.socket.user_id) data.userStatus = "offline";
270
+ else data.userStatus = "online";
271
+
272
+ if (server && server.wsManager) {
273
+ server.wsManager.send(data);
274
+ }
275
+ } catch (error) {
276
+ console.error("checkSession error", error);
277
+ }
278
+ }
279
+
280
+ /**
281
+ * Creates, formats, and dispatches email invitations to new team collaborators.
282
+ */
283
+ async function inviteUser(data) {
284
+ try {
285
+ if (!server || !server.crud) return;
286
+
287
+ const inviteId = server.crud.ObjectId().toString();
288
+ let uid = data.uid;
289
+ delete data.uid;
290
+
291
+ let targetArray = "users"; // default fallback
292
+ let keyData = await server.crud.send({
293
+ method: "object.read",
294
+ host: data.host,
295
+ array: "keys",
296
+ $filter: {
297
+ query: { key: data.user_id },
298
+ limit: 1
299
+ },
300
+ organization_id: data.organization_id || (data.socket && data.socket.organization_id)
301
+ });
302
+
303
+ if (keyData && keyData.object && keyData.object.length > 0 && keyData.object[0].array) {
304
+ targetArray = keyData.object[0].array;
305
+ }
306
+
307
+ data.method = "object.update";
308
+ data.array = targetArray;
309
+ data.object = {
310
+ _id: data.user_id,
311
+ "$push.invitations": inviteId,
312
+ "$addToSet.members": data.email
313
+ };
314
+
315
+ // Bypass crud.sync check to update across local clients simultaneously
316
+ data.updateDB = true;
317
+
318
+ const updatedData = await server.crud.send(data);
319
+
320
+ let invitee = await server.crud.send({
321
+ method: "object.read",
322
+ host: updatedData.host,
323
+ array: "users",
324
+ $filter: {
325
+ query: { email: updatedData.email },
326
+ limit: 1
327
+ },
328
+ organization_id: updatedData.organization_id
329
+ });
330
+
331
+ if (invitee.object && invitee.object[0]) {
332
+ invitee = invitee.object[0]._id;
333
+ } else {
334
+ invitee = "";
335
+ }
336
+
337
+ let htmlBody = `
312
338
  <html>
313
339
  <head>
314
340
  <title>Welcome to Yellow Oracle!</title>
@@ -316,164 +342,174 @@ class CoCreateUser {
316
342
  <body>
317
343
  <p>Hello,</p>
318
344
 
319
- <p>You have been invited to join ${data.name} on Yellow Oracle.</p>
345
+ <p>You have been invited to join ${updatedData.name} on Yellow Oracle.</p>
320
346
 
321
- <p><a href="${data.origin}${data.path}?email=${data.email}&token=${inviteId}&user_id=${invitee}&name=${data.name}" style="color: #ffffff; background-color: #FFD700; padding: 10px 20px; text-decoration: none; border-radius: 5px;">Accept Your Invitation</a></p>
347
+ <p><a href="${updatedData.origin}${updatedData.path}?email=${updatedData.email}&token=${inviteId}&user_id=${invitee}&name=${updatedData.name}" style="color: #ffffff; background-color: #FFD700; padding: 10px 20px; text-decoration: none; border-radius: 5px;">Accept Your Invitation</a></p>
322
348
 
323
- <p>Please note, this invitation link will expire in 48 hours. We encourage you to accept it soon to begin your journey with ${data.name} on Yellow Oracle.</p>
349
+ <p>Please note, this invitation link will expire in 48 hours. We encourage you to accept it soon to begin your journey with ${updatedData.name} on Yellow Oracle.</p>
324
350
 
325
351
  <p>If the button above doesn't work, you can copy and paste the following URL into your web browser:</p>
326
- <p><a href="${data.origin}${data.path}?email=${data.email}&token=${inviteId}&user_id=${invitee}&name=${data.name}">${data.origin}${data.path}?email=${data.email}&token=${inviteId}&user_id=${invitee}&name=${data.name}</a></p>
352
+ <p><a href="${updatedData.origin}${updatedData.path}?email=${updatedData.email}&token=${inviteId}&user_id=${invitee}&name=${updatedData.name}">${updatedData.origin}${updatedData.path}?email=${updatedData.email}&token=${inviteId}&user_id=${invitee}&name=${updatedData.name}</a></p>
327
353
 
328
- <p>If you received this invitation by mistake or have any questions, please don't hesitate to get in touch with our support team at <a href="mailto:support@${data.hostname}">support@${data.hostname}</a>.</p>
354
+ <p>If you received this invitation by mistake or have any questions, please don't hesitate to get in touch with our support team at <a href="mailto:support@${updatedData.hostname}">support@${updatedData.hostname}</a>.</p>
329
355
 
330
- <p>We look forward to welcoming you to ${data.name}'s Yellow Oracle account!</p>
356
+ <p>We look forward to welcoming you to ${updatedData.name}'s Yellow Oracle account!</p>
331
357
 
332
358
  </body>
333
- </html>
359
+ </html>
334
360
  `;
335
361
 
336
- let email = {
337
- method: "postmark.sendEmail",
338
- host: data.host,
339
- postmark: {
340
- From: data.from,
341
- To: data.email,
342
- Subject: `${data.name} has invited you to join them on Yellow Oracle`,
343
- HtmlBody: htmlBody,
344
- TextBody:
345
- "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",
346
- MessageStream: "outbound"
347
- },
348
- organization_id: data.organization_id
349
- };
350
-
351
- // TODO: wsManager.emit('postmark', email) needs to await response
352
- this.wsManager.emit("postmark", email);
353
-
354
- let response = {
355
- socket: data.socket,
356
- host: data.host,
357
- method: "inviteUser",
358
- success: true,
359
- message: "Succesfully sent invite",
360
- organization_id: data.organization_id,
361
- uid
362
- };
363
-
364
- this.wsManager.send(response);
365
- } catch (error) {
366
- data.error = error;
367
- this.wsManager.send(data);
368
-
369
- console.log("Invite User failed", error);
370
- }
371
- }
372
-
373
- async acceptInvite(data) {
374
- const self = this;
375
- try {
376
- if (!data.token || !data.user_id) {
377
- // TODO: handle error
378
- return;
379
- }
380
-
381
- let uid = data.uid;
382
- delete data.uid;
383
-
384
- data.method = "object.update";
385
- data.array = "users";
386
- data.object = {
387
- "$pull.invitations": data.token,
388
- "$pull.members": data.email
389
- };
390
- data.$filter = {
391
- query: {
392
- invitations: { $in: [data.token] },
393
- members: { $in: [data.email] }
394
- },
395
- limit: 2
396
- };
397
-
398
- // TODO: upodateDB is a temp solution to by pass crud.sync clientId as all crud methods are ignored fro same clientid except for read
399
- data.updateDB = true;
400
-
401
- data = await this.crud.send(data);
402
-
403
- let response = {
404
- socket: data.socket,
405
- host: data.host,
406
- method: "acceptInvite",
407
- success: false,
408
- message:
409
- "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.",
410
- organization_id: data.organization_id,
411
- uid
412
- };
413
-
414
- for (let object of data.object) {
415
- if (object._id) {
416
- delete data.$filter;
417
- data.object = {
418
- _id: object._id,
419
- "$addToSet.members": data.user_id
420
- };
421
- data = await this.crud.send(data);
422
- this.crud.send({
423
- method: "object.update",
424
- host: data.host,
425
- array: "users",
426
- object: {
427
- _id: data.user_id,
428
- memberAccount: object._id,
429
- subscription: "6571fe530c48ef6970900a82"
430
- }
431
- });
432
- response.success = true;
433
- response.message = "Invite Accepted";
434
- break;
435
- }
436
- }
437
-
438
- self.wsManager.send(response);
439
- } catch (error) {
440
- console.log("Accept invite failed", error);
441
- }
442
- }
443
-
444
- // TODO: email or phone param refrencing the object to execute via api
445
- async forgotPassword(data) {
446
- const self = this;
447
- try {
448
- const recoveryId = this.crud.ObjectId().toString();
449
- let socket = data.socket;
450
- delete data.socket;
451
-
452
- data.method = "object.update";
453
- data.array = "keys";
454
- data.object = { recoveryId };
455
- data.$filter = {
456
- query: { email: data.email },
457
- limit: 1
458
- };
459
-
460
- this.crud.send(data).then(async (data) => {
461
- let response = {
462
- socket,
463
- host: data.host,
464
- method: "forgotPassword",
465
- success: false,
466
- message: "Email does not exist",
467
- organization_id: data.organization_id,
468
- uid: data.uid
469
- };
470
-
471
- for (let object of data.object) {
472
- if (object._id) {
473
- // TODO: sendEmail
474
- response.success = true;
475
- response.message = "Email Sent";
476
- let htmlBody = `
362
+ let email = {
363
+ method: "postmark.sendEmail",
364
+ host: updatedData.host,
365
+ postmark: {
366
+ From: updatedData.from,
367
+ To: updatedData.email,
368
+ Subject: `${updatedData.name} has invited you to join them on Yellow Oracle`,
369
+ HtmlBody: htmlBody,
370
+ TextBody:
371
+ "Hello, \n\nWe received a request to join Yellow Oracle. Please copy and paste the invitation link directly to proceed.",
372
+ MessageStream: "outbound"
373
+ },
374
+ organization_id: updatedData.organization_id
375
+ };
376
+
377
+ if (server.wsManager) {
378
+ server.wsManager.emit("postmark", email);
379
+ }
380
+
381
+ let response = {
382
+ socket: updatedData.socket,
383
+ host: updatedData.host,
384
+ method: "inviteUser",
385
+ success: true,
386
+ message: "Successfully sent invite",
387
+ organization_id: updatedData.organization_id,
388
+ uid
389
+ };
390
+
391
+ if (server.wsManager) {
392
+ server.wsManager.send(response);
393
+ }
394
+ } catch (error) {
395
+ data.error = error;
396
+ if (server && server.wsManager) {
397
+ server.wsManager.send(data);
398
+ }
399
+ console.error("Invite User failed", error);
400
+ }
401
+ }
402
+
403
+ /**
404
+ * Validates outstanding team tokens and connects user accounts to parent environments.
405
+ */
406
+ async function acceptInvite(data) {
407
+ try {
408
+ if (!server || !server.crud) return;
409
+ if (!data.token || !data.user_id) {
410
+ return;
411
+ }
412
+
413
+ let uid = data.uid;
414
+ delete data.uid;
415
+
416
+ data.method = "object.update";
417
+ data.array = "users";
418
+ data.object = {
419
+ "$pull.invitations": data.token,
420
+ "$pull.members": data.email
421
+ };
422
+ data.$filter = {
423
+ query: {
424
+ invitations: { $in: [data.token] },
425
+ members: { $in: [data.email] }
426
+ },
427
+ limit: 2
428
+ };
429
+
430
+ data.updateDB = true;
431
+
432
+ const updatedData = await server.crud.send(data);
433
+
434
+ let response = {
435
+ socket: updatedData.socket,
436
+ host: updatedData.host,
437
+ method: "acceptInvite",
438
+ success: false,
439
+ message:
440
+ "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.",
441
+ organization_id: updatedData.organization_id,
442
+ uid
443
+ };
444
+
445
+ for (let object of updatedData.object) {
446
+ if (object._id) {
447
+ delete updatedData.$filter;
448
+ updatedData.object = {
449
+ _id: object._id,
450
+ "$addToSet.members": updatedData.user_id
451
+ };
452
+ const finalData = await server.crud.send(updatedData);
453
+ await server.crud.send({
454
+ method: "object.update",
455
+ host: finalData.host,
456
+ array: "users",
457
+ object: {
458
+ _id: finalData.user_id,
459
+ memberAccount: object._id,
460
+ subscription: "6571fe530c48ef6970900a82"
461
+ }
462
+ });
463
+ response.success = true;
464
+ response.message = "Invite Accepted";
465
+ break;
466
+ }
467
+ }
468
+
469
+ if (server.wsManager) {
470
+ server.wsManager.send(response);
471
+ }
472
+ } catch (error) {
473
+ console.error("Accept invite failed", error);
474
+ }
475
+ }
476
+
477
+ /**
478
+ * Constructs recovery parameters and sends secure password-reset tokens to verified accounts.
479
+ */
480
+ async function forgotPassword(data) {
481
+ try {
482
+ if (!server || !server.crud) return;
483
+
484
+ const recoveryId = server.crud.ObjectId().toString();
485
+ let socket = data.socket;
486
+ delete data.socket;
487
+
488
+ data.method = "object.update";
489
+ data.array = "keys";
490
+ data.object = { recoveryId };
491
+ data.$filter = {
492
+ query: { email: data.email },
493
+ limit: 1
494
+ };
495
+
496
+ const result = await server.crud.send(data);
497
+
498
+ let response = {
499
+ socket,
500
+ host: result.host,
501
+ method: "forgotPassword",
502
+ success: false,
503
+ message: "Email does not exist",
504
+ organization_id: result.organization_id,
505
+ uid: result.uid
506
+ };
507
+
508
+ for (let object of result.object) {
509
+ if (object._id) {
510
+ response.success = true;
511
+ response.message = "Email Sent";
512
+ let htmlBody = `
477
513
  <html>
478
514
  <head>
479
515
  <title>Reset Your Password</title>
@@ -483,87 +519,117 @@ class CoCreateUser {
483
519
 
484
520
  <p>We 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 using the link below.</p>
485
521
 
486
- <p><a href="${data.origin}${data.path}?email=${data.email}&token=${recoveryId}&recoveyId=${recoveryId}" style="color: #ffffff; background-color: #007bff; padding: 10px 20px; text-decoration: none; border-radius: 5px;">Reset My Password</a></p>
522
+ <p><a href="${result.origin}${result.path}?email=${result.email}&token=${recoveryId}&recoveyId=${recoveryId}" style="color: #ffffff; background-color: #007bff; padding: 10px 20px; text-decoration: none; border-radius: 5px;">Reset My Password</a></p>
487
523
 
488
524
  <p>This link will expire in 24 hours for your security.</p>
489
525
 
490
526
  <p>If you're having trouble with the button above, copy and paste the URL below into your web browser:</p>
491
- <p><a href="${data.origin}${data.path}?email=${data.email}&token=${recoveryId}&recoveyId=${recoveryId}"">${data.origin}${data.path}?email=${data.email}&token=${recoveryId}&recoveyId=${recoveryId}"</a></p>
527
+ <p><a href="${result.origin}${result.path}?email=${result.email}&token=${recoveryId}&recoveyId=${recoveryId}">${result.origin}${result.path}?email=${result.email}&token=${recoveryId}&recoveyId=${recoveryId}</a></p>
492
528
 
493
- <p>Need more help? Our support team is here for you. Contact us at <a href="mailto:support@${data.hostname}">support@${data.hostname}</a>.</p>
529
+ <p>Need more help? Our support team is here for you. Contact us at <a href="mailto:support@${result.hostname}">support@${result.hostname}</a>.</p>
494
530
 
495
531
  <p>Thank you for using our services!</p>
496
532
 
497
533
  </body>
498
534
  </html>
499
535
  `;
500
- let email = {
501
- method: "postmark.sendEmail",
502
- host: data.host,
503
- postmark: {
504
- From: data.from,
505
- To: data.email,
506
- Subject: "Reset Your Password Easily",
507
- HtmlBody: htmlBody,
508
- TextBody:
509
- "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",
510
- MessageStream: "outbound"
511
- },
512
- organization_id: data.organization_id
513
- };
514
-
515
- // TODO: wsManager.emit('postmark', email) needs to await response
516
- self.wsManager.emit("postmark", email);
517
-
518
- break;
519
- }
520
- }
521
-
522
- self.wsManager.send(response);
523
- });
524
- } catch (error) {
525
- console.log("Forgot Password failed", error);
526
- }
527
- }
528
-
529
- async resetPassword(data) {
530
- const self = this;
531
- try {
532
- if (!data.email || !data.password || !data.token) return;
533
-
534
- data.method = "object.update";
535
- data.array = "keys";
536
- data.object = { password: data.password, recoveryId: "" };
537
- data.$filter = {
538
- query: { email: data.email, recoveryId: data.token },
539
- limit: 1
540
- };
541
-
542
- this.crud.send(data).then(async (data) => {
543
- let response = {
544
- socket: data.socket,
545
- host: data.host,
546
- method: "resetPassword",
547
- success: false,
548
- message: "Token is invalid or has expired",
549
- organization_id: data.organization_id,
550
- uid: data.uid
551
- };
552
-
553
- for (let object of data.object) {
554
- if (object._id) {
555
- response.success = true;
556
- response.message = "Password reset succesfull";
557
- break;
558
- }
559
- }
560
-
561
- self.wsManager.send(response);
562
- });
563
- } catch (error) {
564
- console.log("Password reset failed", error);
565
- }
566
- }
536
+ let email = {
537
+ method: "postmark.sendEmail",
538
+ host: result.host,
539
+ postmark: {
540
+ From: result.from,
541
+ To: result.email,
542
+ Subject: "Reset Your Password Easily",
543
+ HtmlBody: htmlBody,
544
+ TextBody:
545
+ "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 accessing your personal recovery link directly.",
546
+ MessageStream: "outbound"
547
+ },
548
+ organization_id: result.organization_id
549
+ };
550
+
551
+ if (server.wsManager) {
552
+ server.wsManager.emit("postmark", email);
553
+ }
554
+ break;
555
+ }
556
+ }
557
+
558
+ if (server.wsManager) {
559
+ server.wsManager.send(response);
560
+ }
561
+ } catch (error) {
562
+ console.error("Forgot Password failed", error);
563
+ }
567
564
  }
568
565
 
569
- module.exports = CoCreateUser;
566
+ /**
567
+ * Validates individual recovery tokens and writes new user credentials safely.
568
+ */
569
+ async function resetPassword(data) {
570
+ try {
571
+ if (!server || !server.crud) return;
572
+ if (!data.email || !data.password || !data.token) return;
573
+
574
+ data.method = "object.update";
575
+ data.array = "keys";
576
+ data.object = { password: data.password, recoveryId: "" };
577
+ data.$filter = {
578
+ query: { email: data.email, recoveryId: data.token },
579
+ limit: 1
580
+ };
581
+
582
+ const result = await server.crud.send(data);
583
+
584
+ let response = {
585
+ socket: result.socket,
586
+ host: result.host,
587
+ method: "resetPassword",
588
+ success: false,
589
+ message: "Token is invalid or has expired",
590
+ organization_id: result.organization_id,
591
+ uid: result.uid
592
+ };
593
+
594
+ for (let object of result.object) {
595
+ if (object._id) {
596
+ response.success = true;
597
+ response.message = "Password reset successful";
598
+ break;
599
+ }
600
+ }
601
+
602
+ if (server.wsManager) {
603
+ server.wsManager.send(response);
604
+ }
605
+ } catch (error) {
606
+ console.error("Password reset failed", error);
607
+ }
608
+ }
609
+
610
+ /**
611
+ * Auto-initialization loop.
612
+ * Silently polls to verify that the unified server orchestrator is fully loaded,
613
+ * and that both the database (crud) and socket manager (wsManager) have completed
614
+ * their initial handshakes, then attaches active user event triggers.
615
+ */
616
+ function init() {
617
+ if (server && server.isInitialized && server.crud && server.wsManager) {
618
+ server.wsManager.on("signUp", (data) => signUp(data));
619
+ server.wsManager.on("signIn", (data) => signIn(data));
620
+ server.wsManager.on("userStatus", (data) => userStatus(data));
621
+ server.wsManager.on("checkSession", (data) => checkSession(data));
622
+ server.wsManager.on("inviteUser", (data) => inviteUser(data));
623
+ server.wsManager.on("acceptInvite", (data) => acceptInvite(data));
624
+ server.wsManager.on("forgotPassword", (data) => forgotPassword(data));
625
+ server.wsManager.on("resetPassword", (data) => resetPassword(data));
626
+ } else {
627
+ // If the shared server orchestrator is not yet ready, check again in 500ms
628
+ setTimeout(init, 500);
629
+ }
630
+ }
631
+
632
+ // Auto-execute initialization instantly upon module load
633
+ init();
634
+
635
+ export default init;