Skip to content
This repository was archived by the owner on Apr 19, 2023. It is now read-only.

Commit d594dea

Browse files
✨ Check existing email for register
1 parent 6f45ccc commit d594dea

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

src/crud/email.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,20 @@ export const getEmailObject = async (email: string) => {
188188
);
189189
};
190190

191+
/**
192+
* Get the detailed email object from a verified email
193+
*/
194+
export const getVerifiedEmailObject = async (email: string) => {
195+
return (<Email[]>(
196+
await cachedQuery(
197+
CacheCategories.EMAIL,
198+
email,
199+
"SELECT * FROM emails WHERE email = ? AND isVerified = 1 LIMIT 1",
200+
[email]
201+
)
202+
))[0];
203+
};
204+
191205
/**
192206
* Get a list of all verified emails of a user
193207
*/
@@ -208,3 +222,15 @@ export const getUserVerifiedEmails = async (user: User | number) => {
208222
)
209223
));
210224
};
225+
226+
export const checkIfNewEmail = async (email: string) => {
227+
email = normalizeEmail(email) || email;
228+
let hasEmail = true;
229+
try {
230+
(await getVerifiedEmailObject(email)).id;
231+
} catch (error) {
232+
hasEmail = false;
233+
}
234+
if (hasEmail) throw new Error(ErrorCode.EMAIL_EXISTS);
235+
return;
236+
};

src/interfaces/enum.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export enum ErrorCode {
4646
MISSING_PRIMARY_EMAIL = "422/missing-primary-email",
4747
MISSING_PASSWORD = "422/missing-password",
4848
MISSING_FIELD = "422/missing-field",
49+
EMAIL_EXISTS = "422/email-exists",
4950
USER_NOT_FOUND = "404/user-not-found",
5051
INVALID_LOGIN = "401/invalid-login",
5152
INSUFFICIENT_PERMISSION = "401/insufficient-permission",

src/rest/auth.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ import {
77
addApprovedLocation
88
} from "../crud/user";
99
import { InsertResult } from "../interfaces/mysql";
10-
import { createEmail, updateEmail, getEmail } from "../crud/email";
10+
import {
11+
createEmail,
12+
updateEmail,
13+
getEmail,
14+
checkIfNewEmail
15+
} from "../crud/email";
1116
import { mail } from "../helpers/mail";
1217
import {
1318
verifyToken,
@@ -69,6 +74,7 @@ export const register = async (
6974
organizationId?: number,
7075
role?: MembershipRole
7176
) => {
77+
if (email) await checkIfNewEmail(email);
7278
// Create user
7379
const result = <InsertResult>await createUser(user);
7480
const userId = result.insertId;

src/rest/email.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,16 @@ import {
55
getUserVerifiedEmails,
66
getUserPrimaryEmailObject,
77
deleteEmail,
8-
getUserEmails
8+
getUserEmails,
9+
checkIfNewEmail
910
} from "../crud/email";
1011
import { createEvent } from "../crud/event";
11-
import { ErrorCode, EventType, Authorizations, ValidationTypes } from "../interfaces/enum";
12+
import {
13+
ErrorCode,
14+
EventType,
15+
Authorizations,
16+
ValidationTypes
17+
} from "../interfaces/enum";
1218
import { updateUser } from "../crud/user";
1319
import { can } from "../helpers/authorization";
1420
import { validate } from "../helpers/utils";
@@ -28,6 +34,7 @@ export const addEmailToUser = async (
2834
locals: Locals
2935
) => {
3036
validate(email, ValidationTypes.EMAIL);
37+
await checkIfNewEmail(email);
3138
await createEmail({ email, userId });
3239
await createEvent(
3340
{ userId, type: EventType.EMAIL_CREATED, data: { email } },

0 commit comments

Comments
 (0)