-
Notifications
You must be signed in to change notification settings - Fork 0
[LOCKLITE-132] Refactor register and login API routes #72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 26 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
c1f1704
docs: place tags in docs
vbetsch 8a3bcd8
refactor: move register in legacy folder
vbetsch b7a5483
feat: create register DTOs
vbetsch 5e7b8ea
feat: create register route
vbetsch 3518f4f
feat: create UserModelDto
vbetsch f3c39f2
feat: create UserAdapter
vbetsch 2ae5a86
feat: create UsersRepository
vbetsch 9738ef5
fix: RegisterPayloadDto
vbetsch ed5b371
feat: create RegisterUseCase
vbetsch b57cd3c
feat: create HashService
vbetsch 9b318cd
feat: test if user already exists
vbetsch da6e811
refactor: clean comments
vbetsch a3e01ab
docs: update register responses
vbetsch 7371978
fix: doc
vbetsch 5845735
fix: import reflect-metadata
vbetsch d8f3adc
fix: update doc properties
vbetsch e3076d9
refactor: remove legacy register
vbetsch ceef3b6
style: format indents
vbetsch ee72729
feat: create SignInDataDto
vbetsch 7559c34
feat: implement compare hash
vbetsch 7efbe0e
feat: implement SignInUseCase
vbetsch e1a2ce9
fix: UserModelDto string format
vbetsch 575d64c
refactor: Create BusinessErrorDto
vbetsch e918da4
fix: Implement BusinessErrorDto in HttpResponseDto
vbetsch 59a459e
fix: linter
vbetsch 97fcf22
fix: refactor tags api docs
vbetsch f7dc128
fix: typo
vbetsch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,109 +1,54 @@ | ||
| import type { NextRequest } from 'next/server'; | ||
| import { NextResponse } from 'next/server'; | ||
| import prisma from '@lib/prisma'; | ||
| import { hash } from 'bcrypt'; | ||
| import 'reflect-metadata'; | ||
| import type { NextRequest, NextResponse } from 'next/server'; | ||
| import type { HttpResponseDto } from '@shared/dto/output/responses/abstract/http.response.dto'; | ||
| import type { RegisterDataDto } from '@shared/dto/output/data/register.data.dto'; | ||
| import type { RegisterPayloadDto } from '@shared/dto/input/payloads/register.payload.dto'; | ||
| import { container } from 'tsyringe'; | ||
| import { handleApiRequest } from '@api/helpers/api/handle-api-request'; | ||
| import { StatusCodes } from 'http-status-codes'; | ||
| import type { User } from '@prisma/generated'; | ||
| /** | ||
| * @swagger | ||
| * tags: | ||
| * - name: Auth | ||
| * description: Authentication and user management | ||
| */ | ||
| import type { UserModelDto } from '@shared/dto/models/user.model.dto'; | ||
| import { RegisterUseCase } from '@api/usecases/auth/register.usecase'; | ||
|
|
||
| /** | ||
| * @swagger | ||
| * /api/auth/register: | ||
| * post: | ||
| * summary: Register a new user | ||
| * tags: [Auth] | ||
| * summary: Register a new user | ||
| * requestBody: | ||
| * description: JSON object containing email and password | ||
| * required: true | ||
| * content: | ||
| * application/json: | ||
| * schema: | ||
| * type: object | ||
| * required: | ||
| * - password | ||
| * properties: | ||
| * email: | ||
| * type: string | ||
| * format: email | ||
| * example: user@example.com | ||
| * password: | ||
| * type: string | ||
| * format: password | ||
| * example: securePassword123 | ||
| * $ref: '#/components/schemas/RegisterPayloadDto' | ||
| * responses: | ||
| * 201: | ||
| * description: User created successfully | ||
| * content: | ||
| * application/json: | ||
| * schema: | ||
| * $ref: '#/components/schemas/User' | ||
| * 400: | ||
| * description: Bad request (missing email or password) | ||
| * $ref: '#/components/schemas/RegisterDataDto' | ||
| * 409: | ||
| * description: Conflict user already exists | ||
| * description: User already exists | ||
| * content: | ||
| * application/json: | ||
| * schema: | ||
| * $ref: '#/components/schemas/BusinessErrorDto' | ||
| * 500: | ||
| * description: Internal server error | ||
| */ | ||
|
|
||
| /** | ||
| * @swagger | ||
| * components: | ||
| * schemas: | ||
| * User: | ||
| * type: object | ||
| * properties: | ||
| * id: | ||
| * type: string | ||
| * description: Unique identifier generated by the database | ||
| * example: 'cl0x1a2b3c4d5e6f7g8h9' | ||
| * email: | ||
| * type: string | ||
| * format: email | ||
| * description: User's email address | ||
| * example: user@example.com | ||
| * content: | ||
| * application/json: | ||
| * schema: | ||
| * $ref: '#/components/schemas/HttpErrorDto' | ||
| */ | ||
| export async function POST( | ||
| req: NextRequest | ||
| ): Promise< | ||
| | NextResponse<{ message: string }> | ||
| | NextResponse<{ id: string; email: string }> | ||
| > { | ||
| try { | ||
| const { email, password } = await req.json(); | ||
| if (!email || !password) { | ||
| return NextResponse.json( | ||
| { message: 'Email and password are required' }, | ||
| { status: StatusCodes.BAD_REQUEST } | ||
| ); | ||
| } | ||
| const exists: User | null = await prisma.user.findUnique({ | ||
| where: { email }, | ||
| }); | ||
| if (exists) { | ||
| return NextResponse.json( | ||
| { message: 'User already exists' }, | ||
| { status: StatusCodes.CONFLICT } | ||
| ); | ||
| } | ||
| const salt: number = parseInt(process.env.BCRYPT_SALT_ROUNDS || '10', 10); | ||
| const hashed: string = await hash(password, salt); | ||
| const user: User = await prisma.user.create({ | ||
| data: { email, password: hashed }, | ||
| }); | ||
| return NextResponse.json( | ||
| { id: user.id, email: user.email }, | ||
| { status: StatusCodes.CREATED } | ||
| ); | ||
| } catch { | ||
| return NextResponse.json( | ||
| { message: 'Internal server error' }, | ||
| { status: StatusCodes.INTERNAL_SERVER_ERROR } | ||
| ); | ||
| } | ||
| request: NextRequest | ||
| ): Promise<NextResponse<HttpResponseDto<RegisterDataDto>>> { | ||
| const payload: RegisterPayloadDto = await request.json(); | ||
| const registerUseCase: RegisterUseCase = container.resolve(RegisterUseCase); | ||
| return await handleApiRequest<RegisterDataDto>(async () => { | ||
| const userCreated: UserModelDto = await registerUseCase.handle(payload); | ||
| const response: RegisterDataDto = { userCreated }; | ||
| return response; | ||
| }, StatusCodes.CREATED); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import { injectable } from 'tsyringe'; | ||
| import { IAdapter } from '@api/adapters/abstract/adapter.interface'; | ||
| import { User } from '@prisma/generated'; | ||
| import { UserModelDto } from '@shared/dto/models/user.model.dto'; | ||
|
|
||
| @injectable() | ||
| export class UserAdapter implements IAdapter<User, UserModelDto> { | ||
| public getDtoFromEntity(entity: User): UserModelDto { | ||
| return { | ||
| id: entity.id, | ||
| email: entity.email, | ||
| // eslint-disable-next-line no-undefined | ||
| name: entity.name || undefined, | ||
| }; | ||
| } | ||
|
|
||
| public getDtoListFromEntities(entities: User[]): UserModelDto[] { | ||
| return entities.map((entity: User) => this.getDtoFromEntity(entity)); | ||
| } | ||
| } | ||
13 changes: 13 additions & 0 deletions
13
src/modules/api/errors/business/auth/user-already-exists.error.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import { StatusCodes } from 'http-status-codes'; | ||
| import { BusinessError } from '@shared/errors/business-error'; | ||
| import { BusinessErrorCodeEnum } from '@shared/errors/business-error-code.enum'; | ||
|
|
||
| export class UserAlreadyExistsError extends BusinessError { | ||
| public constructor(email: string) { | ||
| super( | ||
| `User with email '${email}' already exists`, | ||
| StatusCodes.CONFLICT, | ||
| BusinessErrorCodeEnum.USER_ALREADY_EXISTS | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import { injectable } from 'tsyringe'; | ||
| import { handlePrismaRequest } from '@api/helpers/prisma/handle-prisma-request'; | ||
| import { User } from '@prisma/generated'; | ||
| import prisma from '@lib/prisma'; | ||
| import type { RegisterPayloadDto } from '@shared/dto/input/payloads/register.payload.dto'; | ||
|
|
||
| @injectable() | ||
| export class UsersRepository { | ||
| public async findByEmail(email: string): Promise<User | null> { | ||
| return await handlePrismaRequest<User | null>(() => | ||
| prisma.user.findUnique({ | ||
| where: { email }, | ||
| }) | ||
| ); | ||
| } | ||
|
|
||
| public async create(payload: RegisterPayloadDto): Promise<User> { | ||
| return await handlePrismaRequest<User>(() => | ||
| prisma.user.create({ data: payload }) | ||
| ); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.