From f9f85c8c5996982cdff2c666cd51188690f27d9b Mon Sep 17 00:00:00 2001 From: JKaypa Date: Fri, 16 Aug 2024 11:47:53 -0500 Subject: [PATCH 01/19] OV-2: + add sign in string to AuthApiPath const --- shared/src/bundles/auth/enums/auth-api-path.enum.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/shared/src/bundles/auth/enums/auth-api-path.enum.ts b/shared/src/bundles/auth/enums/auth-api-path.enum.ts index 5833c6684..f64e5d59e 100644 --- a/shared/src/bundles/auth/enums/auth-api-path.enum.ts +++ b/shared/src/bundles/auth/enums/auth-api-path.enum.ts @@ -1,6 +1,7 @@ const AuthApiPath = { ROOT: '/', SIGN_UP: '/sign-up', + SIGN_IN: '/sign-in', } as const; export { AuthApiPath }; From 532a1197e11bf7cbe140bc7a55972680117014cd Mon Sep 17 00:00:00 2001 From: JKaypa Date: Fri, 16 Aug 2024 12:14:23 -0500 Subject: [PATCH 02/19] OV-2: + create user signin request dto type --- shared/src/bundles/users/types/types.ts | 1 + .../src/bundles/users/types/user-sign-in-request-dto.type.ts | 5 +++++ shared/src/bundles/users/users.ts | 1 + shared/src/index.ts | 1 + 4 files changed, 8 insertions(+) create mode 100644 shared/src/bundles/users/types/user-sign-in-request-dto.type.ts diff --git a/shared/src/bundles/users/types/types.ts b/shared/src/bundles/users/types/types.ts index 7564f70b2..f6fd8caa2 100644 --- a/shared/src/bundles/users/types/types.ts +++ b/shared/src/bundles/users/types/types.ts @@ -1,4 +1,5 @@ export { type UserGetAllItemResponseDto } from './user-get-all-item-response-dto.type.js'; export { type UserGetAllResponseDto } from './user-get-all-response-dto.type.js'; +export { type UserSignInRequestDto } from './user-sign-in-request-dto.type.js'; export { type UserSignUpRequestDto } from './user-sign-up-request-dto.type.js'; export { type UserSignUpResponseDto } from './user-sign-up-response-dto.type.js'; diff --git a/shared/src/bundles/users/types/user-sign-in-request-dto.type.ts b/shared/src/bundles/users/types/user-sign-in-request-dto.type.ts new file mode 100644 index 000000000..6d445fe1e --- /dev/null +++ b/shared/src/bundles/users/types/user-sign-in-request-dto.type.ts @@ -0,0 +1,5 @@ +import { type UserSignUpRequestDto } from './types.js'; + +type UserSignInRequestDto = Pick; + +export { type UserSignInRequestDto }; diff --git a/shared/src/bundles/users/users.ts b/shared/src/bundles/users/users.ts index e65858985..f51c03f83 100644 --- a/shared/src/bundles/users/users.ts +++ b/shared/src/bundles/users/users.ts @@ -2,6 +2,7 @@ export { UsersApiPath, UserValidationMessage } from './enums/enums.js'; export { type UserGetAllItemResponseDto, type UserGetAllResponseDto, + type UserSignInRequestDto, type UserSignUpRequestDto, type UserSignUpResponseDto, } from './types/types.js'; diff --git a/shared/src/index.ts b/shared/src/index.ts index df887038b..62e27af88 100644 --- a/shared/src/index.ts +++ b/shared/src/index.ts @@ -2,6 +2,7 @@ export { AuthApiPath } from './bundles/auth/auth.js'; export { type UserGetAllItemResponseDto, type UserGetAllResponseDto, + type UserSignInRequestDto, type UserSignUpRequestDto, type UserSignUpResponseDto, UsersApiPath, From 8e9d770ef08fb351ec73878fdf5d4c52d381cf12 Mon Sep 17 00:00:00 2001 From: JKaypa Date: Sat, 17 Aug 2024 18:20:00 -0500 Subject: [PATCH 03/19] OV-2: + create user signin response dto type --- shared/src/bundles/users/types/types.ts | 1 + .../bundles/users/types/user-sign-in-response-dto.type.ts | 5 +++++ shared/src/bundles/users/users.ts | 1 + shared/src/index.ts | 1 + 4 files changed, 8 insertions(+) create mode 100644 shared/src/bundles/users/types/user-sign-in-response-dto.type.ts diff --git a/shared/src/bundles/users/types/types.ts b/shared/src/bundles/users/types/types.ts index f6fd8caa2..36cfa4fdb 100644 --- a/shared/src/bundles/users/types/types.ts +++ b/shared/src/bundles/users/types/types.ts @@ -1,5 +1,6 @@ export { type UserGetAllItemResponseDto } from './user-get-all-item-response-dto.type.js'; export { type UserGetAllResponseDto } from './user-get-all-response-dto.type.js'; export { type UserSignInRequestDto } from './user-sign-in-request-dto.type.js'; +export { type UserSignInResponseDto } from './user-sign-in-response-dto.type.js'; export { type UserSignUpRequestDto } from './user-sign-up-request-dto.type.js'; export { type UserSignUpResponseDto } from './user-sign-up-response-dto.type.js'; diff --git a/shared/src/bundles/users/types/user-sign-in-response-dto.type.ts b/shared/src/bundles/users/types/user-sign-in-response-dto.type.ts new file mode 100644 index 000000000..61be88ee0 --- /dev/null +++ b/shared/src/bundles/users/types/user-sign-in-response-dto.type.ts @@ -0,0 +1,5 @@ +import { type UserSignUpResponseDto } from './user-sign-up-response-dto.type.js'; + +type UserSignInResponseDto = UserSignUpResponseDto; + +export { type UserSignInResponseDto }; diff --git a/shared/src/bundles/users/users.ts b/shared/src/bundles/users/users.ts index f51c03f83..502faa91f 100644 --- a/shared/src/bundles/users/users.ts +++ b/shared/src/bundles/users/users.ts @@ -3,6 +3,7 @@ export { type UserGetAllItemResponseDto, type UserGetAllResponseDto, type UserSignInRequestDto, + type UserSignInResponseDto, type UserSignUpRequestDto, type UserSignUpResponseDto, } from './types/types.js'; diff --git a/shared/src/index.ts b/shared/src/index.ts index 62e27af88..3a0064087 100644 --- a/shared/src/index.ts +++ b/shared/src/index.ts @@ -3,6 +3,7 @@ export { type UserGetAllItemResponseDto, type UserGetAllResponseDto, type UserSignInRequestDto, + type UserSignInResponseDto, type UserSignUpRequestDto, type UserSignUpResponseDto, UsersApiPath, From 55df57e698a9bb8004f73bcfd0b4d04917b43a03 Mon Sep 17 00:00:00 2001 From: JKaypa Date: Sun, 18 Aug 2024 15:53:44 -0500 Subject: [PATCH 04/19] OV-2: + add 400 error code --- shared/src/framework/http/enums/http-code.enum.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/shared/src/framework/http/enums/http-code.enum.ts b/shared/src/framework/http/enums/http-code.enum.ts index 59a238f46..6d2ad89ba 100644 --- a/shared/src/framework/http/enums/http-code.enum.ts +++ b/shared/src/framework/http/enums/http-code.enum.ts @@ -1,6 +1,7 @@ const HttpCode = { OK: 200, CREATED: 201, + BAD_REQUEST: 400, UNPROCESSED_ENTITY: 422, INTERNAL_SERVER_ERROR: 500, } as const; From 6df4a10e6f3f7a51b168c4a00d10b0c6bb82e499 Mon Sep 17 00:00:00 2001 From: JKaypa Date: Sun, 18 Aug 2024 15:55:39 -0500 Subject: [PATCH 05/19] OV-2: * edit props order --- shared/src/bundles/auth/enums/auth-api-path.enum.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shared/src/bundles/auth/enums/auth-api-path.enum.ts b/shared/src/bundles/auth/enums/auth-api-path.enum.ts index f64e5d59e..79d9340e1 100644 --- a/shared/src/bundles/auth/enums/auth-api-path.enum.ts +++ b/shared/src/bundles/auth/enums/auth-api-path.enum.ts @@ -1,7 +1,7 @@ const AuthApiPath = { ROOT: '/', - SIGN_UP: '/sign-up', SIGN_IN: '/sign-in', + SIGN_UP: '/sign-up', } as const; export { AuthApiPath }; From 782952a70ec31f109651c10ca62496b3d87b444b Mon Sep 17 00:00:00 2001 From: JKaypa Date: Sun, 18 Aug 2024 15:57:15 -0500 Subject: [PATCH 06/19] OV-2: + add validation message --- shared/src/bundles/users/enums/user-validation-message.enum.ts | 1 + shared/src/index.ts | 1 + 2 files changed, 2 insertions(+) diff --git a/shared/src/bundles/users/enums/user-validation-message.enum.ts b/shared/src/bundles/users/enums/user-validation-message.enum.ts index b28063d96..bba0dc6b7 100644 --- a/shared/src/bundles/users/enums/user-validation-message.enum.ts +++ b/shared/src/bundles/users/enums/user-validation-message.enum.ts @@ -1,6 +1,7 @@ const UserValidationMessage = { EMAIL_REQUIRE: 'Email is required', EMAIL_WRONG: 'Email is wrong', + WRONG_CREDENTIALS: 'Email or password are incorrect', } as const; export { UserValidationMessage }; diff --git a/shared/src/index.ts b/shared/src/index.ts index 3a0064087..33089cc13 100644 --- a/shared/src/index.ts +++ b/shared/src/index.ts @@ -8,6 +8,7 @@ export { type UserSignUpResponseDto, UsersApiPath, userSignUpValidationSchema, + UserValidationMessage, } from './bundles/users/users.js'; export { ApiPath, From 830ec5a097f973a5c5a5625bf365e89289a933c8 Mon Sep 17 00:00:00 2001 From: JKaypa Date: Sun, 18 Aug 2024 16:00:09 -0500 Subject: [PATCH 07/19] OV-2: * edit service, repository find method type --- backend/src/common/types/repository.type.ts | 2 +- backend/src/common/types/service.type.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/src/common/types/repository.type.ts b/backend/src/common/types/repository.type.ts index d69be849b..21b21ed88 100644 --- a/backend/src/common/types/repository.type.ts +++ b/backend/src/common/types/repository.type.ts @@ -1,5 +1,5 @@ type Repository = { - find(): Promise; + find(payload: string | number): Promise; findAll(): Promise; create(payload: unknown): Promise; update(): Promise; diff --git a/backend/src/common/types/service.type.ts b/backend/src/common/types/service.type.ts index 7b1a64ebb..31d498842 100644 --- a/backend/src/common/types/service.type.ts +++ b/backend/src/common/types/service.type.ts @@ -1,5 +1,5 @@ type Service = { - find(): Promise; + find(payload: string | number): Promise; findAll(): Promise<{ items: T[]; }>; From f07f147547fbc815826e33aa086994ec13f88e21 Mon Sep 17 00:00:00 2001 From: JKaypa Date: Sun, 18 Aug 2024 16:02:10 -0500 Subject: [PATCH 08/19] OV-2: * update repository find method --- backend/src/bundles/users/user.repository.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/backend/src/bundles/users/user.repository.ts b/backend/src/bundles/users/user.repository.ts index be08919a1..9bbd2d3a9 100644 --- a/backend/src/bundles/users/user.repository.ts +++ b/backend/src/bundles/users/user.repository.ts @@ -9,8 +9,13 @@ class UserRepository implements Repository { this.userModel = userModel; } - public find(): ReturnType { - return Promise.resolve(null); + public async find(payload: string | number): Promise { + const user = await this.userModel + .query() + .findOne({ payload }) + .execute(); + + return user ? UserEntity.initialize(user) : null; } public async findAll(): Promise { From 1f7ec608907ee1f6e86503322ad25268f9d2261c Mon Sep 17 00:00:00 2001 From: JKaypa Date: Sun, 18 Aug 2024 16:03:15 -0500 Subject: [PATCH 09/19] OV-2: * update service find method --- backend/src/bundles/users/user.service.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/src/bundles/users/user.service.ts b/backend/src/bundles/users/user.service.ts index 1616231f0..00c796c2e 100644 --- a/backend/src/bundles/users/user.service.ts +++ b/backend/src/bundles/users/user.service.ts @@ -16,8 +16,8 @@ class UserService implements Service { this.userRepository = userRepository; } - public find(): ReturnType { - return Promise.resolve(null); + public async find(payload: string | number): Promise { + return await this.userRepository.find(payload); } public async findAll(): Promise { From 483f1c87033dc47188da67ea1d429a9bca4e9a7f Mon Sep 17 00:00:00 2001 From: JKaypa Date: Sun, 18 Aug 2024 16:04:29 -0500 Subject: [PATCH 10/19] OV-2: + implement sign in service method --- backend/src/bundles/auth/auth.service.ts | 36 ++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/backend/src/bundles/auth/auth.service.ts b/backend/src/bundles/auth/auth.service.ts index 5ae168e63..3ae63790d 100644 --- a/backend/src/bundles/auth/auth.service.ts +++ b/backend/src/bundles/auth/auth.service.ts @@ -1,8 +1,14 @@ import { + type UserSignInRequestDto, + type UserSignInResponseDto, type UserSignUpRequestDto, type UserSignUpResponseDto, } from '~/bundles/users/types/types.js'; import { type UserService } from '~/bundles/users/user.service.js'; +import { HttpCode, HttpError } from '~/common/http/http.js'; +import { cryptService } from '~/common/services/services.js'; + +import { UserValidationMessage } from './enums/enums.js'; class AuthService { private userService: UserService; @@ -11,6 +17,36 @@ class AuthService { this.userService = userService; } + public async signIn( + userRequestDto: UserSignInRequestDto, + ): Promise { + const { email, password } = userRequestDto; + const user = await this.userService.find(email); + + if (!user) { + throw new HttpError({ + message: UserValidationMessage.WRONG_CREDENTIALS, + status: HttpCode.BAD_REQUEST, + }); + } + + const { passwordHash } = user.toNewObject(); + + const isPwdCorrect = cryptService.compareSyncPassword( + password, + passwordHash, + ); + + if (!isPwdCorrect) { + throw new HttpError({ + message: UserValidationMessage.WRONG_CREDENTIALS, + status: HttpCode.BAD_REQUEST, + }); + } + + return user.toObject(); + } + public signUp( userRequestDto: UserSignUpRequestDto, ): Promise { From 4c449b62f23ec7d18ab2ff5f40928a4a4b21ec67 Mon Sep 17 00:00:00 2001 From: JKaypa Date: Sun, 18 Aug 2024 16:06:14 -0500 Subject: [PATCH 11/19] OV-2: + add sign in endpoint to auth controller --- backend/src/bundles/auth/auth.controller.ts | 30 ++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/backend/src/bundles/auth/auth.controller.ts b/backend/src/bundles/auth/auth.controller.ts index 7a64ed90f..bc650db0c 100644 --- a/backend/src/bundles/auth/auth.controller.ts +++ b/backend/src/bundles/auth/auth.controller.ts @@ -1,4 +1,7 @@ -import { type UserSignUpRequestDto } from '~/bundles/users/users.js'; +import { + type UserSignInRequestDto, + type UserSignUpRequestDto, +} from '~/bundles/users/users.js'; import { userSignUpValidationSchema } from '~/bundles/users/users.js'; import { type ApiHandlerOptions, @@ -20,6 +23,20 @@ class AuthController extends BaseController { this.authService = authService; + this.addRoute({ + path: AuthApiPath.SIGN_IN, + method: 'POST', + validation: { + body: userSignUpValidationSchema, + }, + handler: (options) => + this.signIn( + options as ApiHandlerOptions<{ + body: UserSignInRequestDto; + }>, + ), + }); + this.addRoute({ path: AuthApiPath.SIGN_UP, method: 'POST', @@ -65,6 +82,17 @@ class AuthController extends BaseController { * type: object * $ref: '#/components/schemas/User' */ + private async signIn( + options: ApiHandlerOptions<{ + body: UserSignInRequestDto; + }>, + ): Promise { + return { + payload: await this.authService.signIn(options.body), + status: HttpCode.OK, + }; + } + private async signUp( options: ApiHandlerOptions<{ body: UserSignUpRequestDto; From c32e1fa25d1676e01fc542f4284fb9749f49820d Mon Sep 17 00:00:00 2001 From: JKaypa Date: Sun, 18 Aug 2024 16:07:39 -0500 Subject: [PATCH 12/19] OV-2: + import user validation messages --- backend/src/bundles/auth/enums/enums.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/src/bundles/auth/enums/enums.ts b/backend/src/bundles/auth/enums/enums.ts index 7cbd1669d..e208cd7f4 100644 --- a/backend/src/bundles/auth/enums/enums.ts +++ b/backend/src/bundles/auth/enums/enums.ts @@ -1 +1 @@ -export { AuthApiPath } from 'shared'; +export { AuthApiPath, UserValidationMessage } from 'shared'; From 1beb23aa7b49aae41b8e826104fa13087027af27 Mon Sep 17 00:00:00 2001 From: JKaypa Date: Sun, 18 Aug 2024 16:09:59 -0500 Subject: [PATCH 13/19] OV-2: + import sign in dto types --- backend/src/bundles/users/types/types.ts | 2 ++ backend/src/bundles/users/users.ts | 2 ++ 2 files changed, 4 insertions(+) diff --git a/backend/src/bundles/users/types/types.ts b/backend/src/bundles/users/types/types.ts index f44313789..6a3620c41 100644 --- a/backend/src/bundles/users/types/types.ts +++ b/backend/src/bundles/users/types/types.ts @@ -1,5 +1,7 @@ export { type UserGetAllResponseDto, + type UserSignInRequestDto, + type UserSignInResponseDto, type UserSignUpRequestDto, type UserSignUpResponseDto, } from 'shared'; diff --git a/backend/src/bundles/users/users.ts b/backend/src/bundles/users/users.ts index 1ad8ad701..93afaa708 100644 --- a/backend/src/bundles/users/users.ts +++ b/backend/src/bundles/users/users.ts @@ -11,6 +11,8 @@ const userController = new UserController(logger, userService); export { userController, userService }; export { + type UserSignInRequestDto, + type UserSignInResponseDto, type UserSignUpRequestDto, type UserSignUpResponseDto, } from './types/types.js'; From 913919756bf3190645388333338d4dacff258c7c Mon Sep 17 00:00:00 2001 From: JKaypa Date: Mon, 19 Aug 2024 10:01:33 -0500 Subject: [PATCH 14/19] OV-2: + implement validation --- backend/src/bundles/auth/auth.controller.ts | 7 ++-- backend/src/bundles/users/users.ts | 5 ++- .../validation-schemas/validation-schemas.ts | 2 +- .../enums/user-validation-message.enum.ts | 4 +++ .../users/enums/user-validation-rule.enum.ts | 5 ++- shared/src/bundles/users/users.ts | 5 ++- .../user-sig-in.validation-schema.ts | 36 +++++++++++++++++++ .../validation-schemas/validation-schemas.ts | 1 + shared/src/index.ts | 1 + 9 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 shared/src/bundles/users/validation-schemas/user-sig-in.validation-schema.ts diff --git a/backend/src/bundles/auth/auth.controller.ts b/backend/src/bundles/auth/auth.controller.ts index bc650db0c..462bb967d 100644 --- a/backend/src/bundles/auth/auth.controller.ts +++ b/backend/src/bundles/auth/auth.controller.ts @@ -2,7 +2,10 @@ import { type UserSignInRequestDto, type UserSignUpRequestDto, } from '~/bundles/users/users.js'; -import { userSignUpValidationSchema } from '~/bundles/users/users.js'; +import { + userSignInValidationSchema, + userSignUpValidationSchema, +} from '~/bundles/users/users.js'; import { type ApiHandlerOptions, type ApiHandlerResponse, @@ -27,7 +30,7 @@ class AuthController extends BaseController { path: AuthApiPath.SIGN_IN, method: 'POST', validation: { - body: userSignUpValidationSchema, + body: userSignInValidationSchema, }, handler: (options) => this.signIn( diff --git a/backend/src/bundles/users/users.ts b/backend/src/bundles/users/users.ts index 93afaa708..73f640e85 100644 --- a/backend/src/bundles/users/users.ts +++ b/backend/src/bundles/users/users.ts @@ -17,4 +17,7 @@ export { type UserSignUpResponseDto, } from './types/types.js'; export { UserModel } from './user.model.js'; -export { userSignUpValidationSchema } from './validation-schemas/validation-schemas.js'; +export { + userSignInValidationSchema, + userSignUpValidationSchema, +} from './validation-schemas/validation-schemas.js'; diff --git a/backend/src/bundles/users/validation-schemas/validation-schemas.ts b/backend/src/bundles/users/validation-schemas/validation-schemas.ts index 7bc9a09c5..5952fe0cf 100644 --- a/backend/src/bundles/users/validation-schemas/validation-schemas.ts +++ b/backend/src/bundles/users/validation-schemas/validation-schemas.ts @@ -1 +1 @@ -export { userSignUpValidationSchema } from 'shared'; +export { userSignInValidationSchema, userSignUpValidationSchema } from 'shared'; diff --git a/shared/src/bundles/users/enums/user-validation-message.enum.ts b/shared/src/bundles/users/enums/user-validation-message.enum.ts index bba0dc6b7..1a168422a 100644 --- a/shared/src/bundles/users/enums/user-validation-message.enum.ts +++ b/shared/src/bundles/users/enums/user-validation-message.enum.ts @@ -1,6 +1,10 @@ const UserValidationMessage = { EMAIL_REQUIRE: 'Email is required', EMAIL_WRONG: 'Email is wrong', + EMAIL_INVALID: 'Please enter a valid email', + FIELD_REQUIRE: 'Please fill out this field', + PASSWORD_LENGTH: 'Password must have from 6 to 12 characters', + INVALID_DATA: 'Incorrect email or password. Please try again.', WRONG_CREDENTIALS: 'Email or password are incorrect', } as const; diff --git a/shared/src/bundles/users/enums/user-validation-rule.enum.ts b/shared/src/bundles/users/enums/user-validation-rule.enum.ts index 36ca9c2e2..eb7d606aa 100644 --- a/shared/src/bundles/users/enums/user-validation-rule.enum.ts +++ b/shared/src/bundles/users/enums/user-validation-rule.enum.ts @@ -1,5 +1,8 @@ const UserValidationRule = { - EMAIL_MINIMUM_LENGTH: 1, + EMAIL_MINIMUM_LENGTH: 6, + EMAIL_MAXIMUM_LENGTH: 320, + PASSWORD_MINIMUM_LENGTH: 6, + PASSWORD_MAXIMUM_LENGTH: 12, } as const; export { UserValidationRule }; diff --git a/shared/src/bundles/users/users.ts b/shared/src/bundles/users/users.ts index 502faa91f..5a2147adc 100644 --- a/shared/src/bundles/users/users.ts +++ b/shared/src/bundles/users/users.ts @@ -7,4 +7,7 @@ export { type UserSignUpRequestDto, type UserSignUpResponseDto, } from './types/types.js'; -export { userSignUp as userSignUpValidationSchema } from './validation-schemas/validation-schemas.js'; +export { + userSignIn as userSignInValidationSchema, + userSignUp as userSignUpValidationSchema, +} from './validation-schemas/validation-schemas.js'; diff --git a/shared/src/bundles/users/validation-schemas/user-sig-in.validation-schema.ts b/shared/src/bundles/users/validation-schemas/user-sig-in.validation-schema.ts new file mode 100644 index 000000000..100cb3dad --- /dev/null +++ b/shared/src/bundles/users/validation-schemas/user-sig-in.validation-schema.ts @@ -0,0 +1,36 @@ +import { z } from 'zod'; + +import { UserValidationMessage, UserValidationRule } from '../enums/enums.js'; + +type UserSignInRequestValidationDto = { + email: z.ZodString; + password: z.ZodString; +}; + +const userSignIn = z + .object({ + email: z + .string({ required_error: UserValidationMessage.FIELD_REQUIRE }) + .trim() + .min(UserValidationRule.EMAIL_MINIMUM_LENGTH, { + message: UserValidationMessage.EMAIL_INVALID, + }) + .max(UserValidationRule.EMAIL_MAXIMUM_LENGTH, { + message: UserValidationMessage.EMAIL_INVALID, + }) + .email({ + message: UserValidationMessage.EMAIL_INVALID, + }), + password: z + .string({ required_error: UserValidationMessage.FIELD_REQUIRE }) + .trim() + .min(UserValidationRule.PASSWORD_MINIMUM_LENGTH, { + message: UserValidationMessage.PASSWORD_LENGTH, + }) + .max(UserValidationRule.PASSWORD_MAXIMUM_LENGTH, { + message: UserValidationMessage.PASSWORD_LENGTH, + }), + }) + .required(); + +export { userSignIn }; diff --git a/shared/src/bundles/users/validation-schemas/validation-schemas.ts b/shared/src/bundles/users/validation-schemas/validation-schemas.ts index cb1c2ad60..58cd817cf 100644 --- a/shared/src/bundles/users/validation-schemas/validation-schemas.ts +++ b/shared/src/bundles/users/validation-schemas/validation-schemas.ts @@ -1 +1,2 @@ +export { userSignIn } from './user-sig-in.validation-schema.js'; export { userSignUp } from './user-sign-up.validation-schema.js'; diff --git a/shared/src/index.ts b/shared/src/index.ts index 33089cc13..d4b77ee0e 100644 --- a/shared/src/index.ts +++ b/shared/src/index.ts @@ -7,6 +7,7 @@ export { type UserSignUpRequestDto, type UserSignUpResponseDto, UsersApiPath, + userSignInValidationSchema, userSignUpValidationSchema, UserValidationMessage, } from './bundles/users/users.js'; From 375fde5c72372e002b69822022bde34b3089945d Mon Sep 17 00:00:00 2001 From: JKaypa Date: Mon, 19 Aug 2024 10:53:24 -0500 Subject: [PATCH 15/19] OV-2: * update imports from users/users --- backend/src/bundles/auth/auth.controller.ts | 4 +--- backend/src/bundles/auth/auth.service.ts | 6 ++++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/backend/src/bundles/auth/auth.controller.ts b/backend/src/bundles/auth/auth.controller.ts index 462bb967d..148002532 100644 --- a/backend/src/bundles/auth/auth.controller.ts +++ b/backend/src/bundles/auth/auth.controller.ts @@ -1,11 +1,9 @@ import { type UserSignInRequestDto, type UserSignUpRequestDto, -} from '~/bundles/users/users.js'; -import { userSignInValidationSchema, - userSignUpValidationSchema, } from '~/bundles/users/users.js'; +import { userSignUpValidationSchema } from '~/bundles/users/users.js'; import { type ApiHandlerOptions, type ApiHandlerResponse, diff --git a/backend/src/bundles/auth/auth.service.ts b/backend/src/bundles/auth/auth.service.ts index 3ae63790d..c7a34fc24 100644 --- a/backend/src/bundles/auth/auth.service.ts +++ b/backend/src/bundles/auth/auth.service.ts @@ -1,10 +1,12 @@ import { - type UserSignInRequestDto, - type UserSignInResponseDto, type UserSignUpRequestDto, type UserSignUpResponseDto, } from '~/bundles/users/types/types.js'; import { type UserService } from '~/bundles/users/user.service.js'; +import { + type UserSignInRequestDto, + type UserSignInResponseDto, +} from '~/bundles/users/users.js'; import { HttpCode, HttpError } from '~/common/http/http.js'; import { cryptService } from '~/common/services/services.js'; From d5f4f15a90cb2cf08619ec19a69f4032787c72f3 Mon Sep 17 00:00:00 2001 From: JKaypa Date: Mon, 19 Aug 2024 15:30:14 -0500 Subject: [PATCH 16/19] OV-2: * edit findByEmail methods --- backend/src/bundles/auth/auth.service.ts | 2 +- backend/src/bundles/users/user.repository.ts | 11 ++++++----- backend/src/bundles/users/user.service.ts | 8 ++++++-- backend/src/common/types/repository.type.ts | 3 ++- backend/src/common/types/service.type.ts | 3 ++- 5 files changed, 17 insertions(+), 10 deletions(-) diff --git a/backend/src/bundles/auth/auth.service.ts b/backend/src/bundles/auth/auth.service.ts index c7a34fc24..fabf0d934 100644 --- a/backend/src/bundles/auth/auth.service.ts +++ b/backend/src/bundles/auth/auth.service.ts @@ -23,7 +23,7 @@ class AuthService { userRequestDto: UserSignInRequestDto, ): Promise { const { email, password } = userRequestDto; - const user = await this.userService.find(email); + const user = await this.userService.findByEmail(email); if (!user) { throw new HttpError({ diff --git a/backend/src/bundles/users/user.repository.ts b/backend/src/bundles/users/user.repository.ts index 9bbd2d3a9..b4d6110d8 100644 --- a/backend/src/bundles/users/user.repository.ts +++ b/backend/src/bundles/users/user.repository.ts @@ -9,11 +9,12 @@ class UserRepository implements Repository { this.userModel = userModel; } - public async find(payload: string | number): Promise { - const user = await this.userModel - .query() - .findOne({ payload }) - .execute(); + public find(): ReturnType { + return Promise.resolve(null); + } + + public async findByEmail(email: string): Promise { + const user = await this.userModel.query().findOne({ email }).execute(); return user ? UserEntity.initialize(user) : null; } diff --git a/backend/src/bundles/users/user.service.ts b/backend/src/bundles/users/user.service.ts index 00c796c2e..778c8f95f 100644 --- a/backend/src/bundles/users/user.service.ts +++ b/backend/src/bundles/users/user.service.ts @@ -16,8 +16,12 @@ class UserService implements Service { this.userRepository = userRepository; } - public async find(payload: string | number): Promise { - return await this.userRepository.find(payload); + public find(): ReturnType { + return Promise.resolve(null); + } + + public async findByEmail(email: string): Promise { + return await this.userRepository.findByEmail(email); } public async findAll(): Promise { diff --git a/backend/src/common/types/repository.type.ts b/backend/src/common/types/repository.type.ts index 21b21ed88..4008a9251 100644 --- a/backend/src/common/types/repository.type.ts +++ b/backend/src/common/types/repository.type.ts @@ -1,6 +1,7 @@ type Repository = { - find(payload: string | number): Promise; + find(): Promise; findAll(): Promise; + findByEmail(email: string): Promise; create(payload: unknown): Promise; update(): Promise; delete(): Promise; diff --git a/backend/src/common/types/service.type.ts b/backend/src/common/types/service.type.ts index 31d498842..245943234 100644 --- a/backend/src/common/types/service.type.ts +++ b/backend/src/common/types/service.type.ts @@ -1,8 +1,9 @@ type Service = { - find(payload: string | number): Promise; + find(): Promise; findAll(): Promise<{ items: T[]; }>; + findByEmail(email: string): Promise; create(payload: unknown): Promise; update(): Promise; delete(): Promise; From 7f48764819d40fd1da6a939ccd2cc331e114a1f1 Mon Sep 17 00:00:00 2001 From: JKaypa Date: Tue, 20 Aug 2024 07:06:18 -0500 Subject: [PATCH 17/19] OV-2: - remove unnecessary types --- backend/src/common/types/repository.type.ts | 1 - backend/src/common/types/service.type.ts | 3 --- 2 files changed, 4 deletions(-) diff --git a/backend/src/common/types/repository.type.ts b/backend/src/common/types/repository.type.ts index 4008a9251..d69be849b 100644 --- a/backend/src/common/types/repository.type.ts +++ b/backend/src/common/types/repository.type.ts @@ -1,7 +1,6 @@ type Repository = { find(): Promise; findAll(): Promise; - findByEmail(email: string): Promise; create(payload: unknown): Promise; update(): Promise; delete(): Promise; diff --git a/backend/src/common/types/service.type.ts b/backend/src/common/types/service.type.ts index 245943234..47673e6d3 100644 --- a/backend/src/common/types/service.type.ts +++ b/backend/src/common/types/service.type.ts @@ -1,8 +1,5 @@ type Service = { find(): Promise; - findAll(): Promise<{ - items: T[]; - }>; findByEmail(email: string): Promise; create(payload: unknown): Promise; update(): Promise; From 06fa5d15110343aeeec9684d51529a8bc657fdd1 Mon Sep 17 00:00:00 2001 From: JKaypa Date: Tue, 20 Aug 2024 07:21:08 -0500 Subject: [PATCH 18/19] OV-2: - * fix service types --- backend/src/common/types/service.type.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/backend/src/common/types/service.type.ts b/backend/src/common/types/service.type.ts index 47673e6d3..7b1a64ebb 100644 --- a/backend/src/common/types/service.type.ts +++ b/backend/src/common/types/service.type.ts @@ -1,6 +1,8 @@ type Service = { find(): Promise; - findByEmail(email: string): Promise; + findAll(): Promise<{ + items: T[]; + }>; create(payload: unknown): Promise; update(): Promise; delete(): Promise; From e411fe23b3ddc52f3df1cd21e826d188578b75fc Mon Sep 17 00:00:00 2001 From: JKaypa Date: Tue, 20 Aug 2024 10:56:32 -0500 Subject: [PATCH 19/19] OV-2: + implemented swagger and added sign-in docs --- backend/src/bundles/auth/auth.controller.ts | 45 +++++++++++++++++-- .../server-application/base-server-app-api.ts | 20 ++++++++- 2 files changed, 60 insertions(+), 5 deletions(-) diff --git a/backend/src/bundles/auth/auth.controller.ts b/backend/src/bundles/auth/auth.controller.ts index 148002532..eaf4bb7a3 100644 --- a/backend/src/bundles/auth/auth.controller.ts +++ b/backend/src/bundles/auth/auth.controller.ts @@ -55,9 +55,9 @@ class AuthController extends BaseController { /** * @swagger - * /auth/sign-up: + * /auth/sign-in: * post: - * description: Sign up user into the application + * description: Sign in user into the application * requestBody: * description: User auth data * required: true @@ -72,7 +72,7 @@ class AuthController extends BaseController { * password: * type: string * responses: - * 201: + * 200: * description: Successful operation * content: * application/json: @@ -82,7 +82,15 @@ class AuthController extends BaseController { * message: * type: object * $ref: '#/components/schemas/User' + * 400: + * description: Failed operation + * content: + * application/json: + * schema: + * type: object + * $ref: '#/components/schemas/Error' */ + private async signIn( options: ApiHandlerOptions<{ body: UserSignInRequestDto; @@ -94,6 +102,37 @@ class AuthController extends BaseController { }; } + /** + * @swagger + * /auth/sign-up: + * post: + * description: Sign up user into the application + * requestBody: + * description: User auth data + * required: true + * content: + * application/json: + * schema: + * type: object + * properties: + * email: + * type: string + * format: email + * password: + * type: string + * responses: + * 201: + * description: Successful operation + * content: + * application/json: + * schema: + * type: object + * properties: + * message: + * type: object + * $ref: '#/components/schemas/User' + */ + private async signUp( options: ApiHandlerOptions<{ body: UserSignUpRequestDto; diff --git a/backend/src/common/server-application/base-server-app-api.ts b/backend/src/common/server-application/base-server-app-api.ts index 6630eaebc..e86461e11 100644 --- a/backend/src/common/server-application/base-server-app-api.ts +++ b/backend/src/common/server-application/base-server-app-api.ts @@ -38,11 +38,27 @@ class BaseServerAppApi implements ServerAppApi { definition: { openapi: '3.0.0', info: { - title: 'Hello World', + title: 'OutreachVids API documentation', version: `${this.version}.0.0`, }, + components: { + schemas: { + Error: { + type: 'object', + properties: { + errorType: { + type: 'string', + enum: ['COMMON', 'VALIDATION'], + }, + message: { + type: 'string', + }, + }, + }, + }, + }, }, - apis: [`src/packages/**/*.controller.${controllerExtension}`], + apis: [`src/bundles/**/*.controller.${controllerExtension}`], }); } }