From e364e31e25bd1d4b8d801c845a96d6be708f0a18 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 19 Jan 2023 09:27:16 +0100 Subject: Implement signup approval in server --- .../validators/shared/user-registrations.ts | 60 ++++++++++++++++++++++ server/middlewares/validators/shared/users.ts | 4 +- 2 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 server/middlewares/validators/shared/user-registrations.ts (limited to 'server/middlewares/validators/shared') diff --git a/server/middlewares/validators/shared/user-registrations.ts b/server/middlewares/validators/shared/user-registrations.ts new file mode 100644 index 000000000..dbc7dda06 --- /dev/null +++ b/server/middlewares/validators/shared/user-registrations.ts @@ -0,0 +1,60 @@ +import express from 'express' +import { UserRegistrationModel } from '@server/models/user/user-registration' +import { MRegistration } from '@server/types/models' +import { forceNumber, pick } from '@shared/core-utils' +import { HttpStatusCode } from '@shared/models' + +function checkRegistrationIdExist (idArg: number | string, res: express.Response) { + const id = forceNumber(idArg) + return checkRegistrationExist(() => UserRegistrationModel.load(id), res) +} + +function checkRegistrationEmailExist (email: string, res: express.Response, abortResponse = true) { + return checkRegistrationExist(() => UserRegistrationModel.loadByEmail(email), res, abortResponse) +} + +async function checkRegistrationHandlesDoNotAlreadyExist (options: { + username: string + channelHandle: string + email: string + res: express.Response +}) { + const { res } = options + + const registration = await UserRegistrationModel.loadByEmailOrHandle(pick(options, [ 'username', 'email', 'channelHandle' ])) + + if (registration) { + res.fail({ + status: HttpStatusCode.CONFLICT_409, + message: 'Registration with this username, channel name or email already exists.' + }) + return false + } + + return true +} + +async function checkRegistrationExist (finder: () => Promise, res: express.Response, abortResponse = true) { + const registration = await finder() + + if (!registration) { + if (abortResponse === true) { + res.fail({ + status: HttpStatusCode.NOT_FOUND_404, + message: 'User not found' + }) + } + + return false + } + + res.locals.userRegistration = registration + return true +} + +export { + checkRegistrationIdExist, + checkRegistrationEmailExist, + checkRegistrationHandlesDoNotAlreadyExist, + checkRegistrationExist +} diff --git a/server/middlewares/validators/shared/users.ts b/server/middlewares/validators/shared/users.ts index b8f1436d3..030adc9f7 100644 --- a/server/middlewares/validators/shared/users.ts +++ b/server/middlewares/validators/shared/users.ts @@ -14,7 +14,7 @@ function checkUserEmailExist (email: string, res: express.Response, abortRespons return checkUserExist(() => UserModel.loadByEmail(email), res, abortResponse) } -async function checkUserNameOrEmailDoesNotAlreadyExist (username: string, email: string, res: express.Response) { +async function checkUserNameOrEmailDoNotAlreadyExist (username: string, email: string, res: express.Response) { const user = await UserModel.loadByUsernameOrEmail(username, email) if (user) { @@ -58,6 +58,6 @@ async function checkUserExist (finder: () => Promise, res: express export { checkUserIdExist, checkUserEmailExist, - checkUserNameOrEmailDoesNotAlreadyExist, + checkUserNameOrEmailDoNotAlreadyExist, checkUserExist } -- cgit v1.2.3