Skip to content

Commit 6f0003b

Browse files
committed
fix: improved
1 parent be0180a commit 6f0003b

File tree

2 files changed

+86
-17
lines changed

2 files changed

+86
-17
lines changed

src/client.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ const CoCreateUser = {
3535
method: "object.create",
3636
array: "keys",
3737
object: {
38-
_id: user.object._id || user.object[0]?._id,
3938
type: "user",
4039
key: user.object._id || user.object[0]?._id,
4140
roles: user.object.roles || [user.object["roles[]"]] || user.object[0]?.roles,

src/server.js

Lines changed: 86 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,23 @@ class CoCreateUser {
2626
}
2727
}
2828

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+
2946
async signUp(data) {
3047
try {
3148
let response = {
@@ -38,32 +55,85 @@ class CoCreateUser {
3855
uid: data.uid
3956
};
4057

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.
4196
if (data.user) {
42-
data.user.method = "object.create";
97+
data.user.method = "object.update";
4398
data.user.host = data.host;
99+
data.user.upsert = true;
100+
44101
if (!data.user.organization_id) {
45102
data.user.organization_id = data.organization_id;
46-
data.userKey.organization_id = data.organization_id;
47103
}
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-
}
53104

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";
59132
}
60-
await this.crud.send(data.userKey);
61133
}
62134

63-
response.success = true;
64-
response.message = "signUp successful";
65-
66135
this.wsManager.send(response);
136+
67137
} catch (error) {
68138
console.log("signup error", error);
69139
}

0 commit comments

Comments
 (0)