From 133cce0cf3ff08ea3b374c0dbf211dbddc5cac03 Mon Sep 17 00:00:00 2001 From: Alberto Quiles Boix Date: Mon, 29 Jul 2024 15:20:42 +0200 Subject: [PATCH] feat: add username and password checker (#36) * feat: add username and password checker --- src/modules/api/v1/account/register.request.ts | 17 ++++++++++++++++- src/shared/consts/account.consts.ts | 4 ++++ src/shared/consts/main.ts | 1 + 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 src/shared/consts/account.consts.ts diff --git a/src/modules/api/v1/account/register.request.ts b/src/modules/api/v1/account/register.request.ts index fbf9500..f9e6728 100644 --- a/src/modules/api/v1/account/register.request.ts +++ b/src/modules/api/v1/account/register.request.ts @@ -2,6 +2,7 @@ import { RequestType } from "shared/types/main.ts"; import { RequestMethod } from "shared/enums/main.ts"; import { System } from "system/main.ts"; import * as bcrypt from "bcrypt"; +import {PASSWORD_REGEX, USERNAME_MAX_LENGTH, USERNAME_MIN_LENGTH} from "shared/consts/main.ts"; export const registerRequest: RequestType = { method: RequestMethod.POST, @@ -17,7 +18,21 @@ export const registerRequest: RequestType = { }, ); - //verify captcha + if (username.length > USERNAME_MAX_LENGTH || username.length < USERNAME_MIN_LENGTH) + return Response.json( + { status: 400 }, + { + status: 400, + }, + ); + + if (!password.test(PASSWORD_REGEX)) + return Response.json( + { status: 400 }, + { + status: 400, + }, + ); const accountId = crypto.randomUUID(); diff --git a/src/shared/consts/account.consts.ts b/src/shared/consts/account.consts.ts new file mode 100644 index 0000000..bcf4daa --- /dev/null +++ b/src/shared/consts/account.consts.ts @@ -0,0 +1,4 @@ +export const USERNAME_MAX_LENGTH = 16 +export const USERNAME_MIN_LENGTH = 3 + +export const PASSWORD_REGEX = /^(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*])[A-Za-z0-9!@#$%^&*]{8,}$/; diff --git a/src/shared/consts/main.ts b/src/shared/consts/main.ts index 32bac2e..6c1c1d3 100644 --- a/src/shared/consts/main.ts +++ b/src/shared/consts/main.ts @@ -1,2 +1,3 @@ export * from "./config.consts.ts"; export * from "./session.consts.ts"; +export * from "./account.consts.ts";