@cocreate/users 1.39.3 → 1.39.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/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## [1.39.4](https://github.com/CoCreate-app/CoCreate-users/compare/v1.39.3...v1.39.4) (2026-04-04)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * improved ([6f0003b](https://github.com/CoCreate-app/CoCreate-users/commit/6f0003b6b151886e88d53de3271f63dde61d7e28))
7
+
1
8
  ## [1.39.3](https://github.com/CoCreate-app/CoCreate-users/compare/v1.39.2...v1.39.3) (2026-04-04)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/users",
3
- "version": "1.39.3",
3
+ "version": "1.39.4",
4
4
  "description": "A simple users component in vanilla javascript. Easily configured using HTML5 attributes and/or JavaScript API.",
5
5
  "keywords": [
6
6
  "users",
package/src/client.js CHANGED
@@ -35,7 +35,6 @@ const CoCreateUser = {
35
35
  method: "object.create",
36
36
  array: "keys",
37
37
  object: {
38
- _id: user.object._id || user.object[0]?._id,
39
38
  type: "user",
40
39
  key: user.object._id || user.object[0]?._id,
41
40
  roles: user.object.roles || [user.object["roles[]"]] || user.object[0]?.roles,
package/src/server.js CHANGED
@@ -26,6 +26,23 @@ class CoCreateUser {
26
26
  }
27
27
  }
28
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
+
29
46
  async signUp(data) {
30
47
  try {
31
48
  let response = {
@@ -38,32 +55,85 @@ class CoCreateUser {
38
55
  uid: data.uid
39
56
  };
40
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.
41
96
  if (data.user) {
42
- data.user.method = "object.create";
97
+ data.user.method = "object.update";
43
98
  data.user.host = data.host;
99
+ data.user.upsert = true;
100
+
44
101
  if (!data.user.organization_id) {
45
102
  data.user.organization_id = data.organization_id;
46
- data.userKey.organization_id = data.organization_id;
47
103
  }
48
- let createdUser = await this.crud.send(data.user);
49
- if (data.userKey && createdUser.object[0] && createdUser.object[0]._id) {
50
- data.userKey.object.key = createdUser.object[0]._id;
51
- }
52
- }
53
104
 
54
- if (data.userKey) {
55
- data.userKey.method = "object.create";
56
- data.userKey.host = data.host;
57
- if (!data.userKey.organization_id) {
58
- data.userKey.organization_id = data.organization_id;
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";
59
132
  }
60
- await this.crud.send(data.userKey);
61
133
  }
62
134
 
63
- response.success = true;
64
- response.message = "signUp successful";
65
-
66
135
  this.wsManager.send(response);
136
+
67
137
  } catch (error) {
68
138
  console.log("signup error", error);
69
139
  }