Skip to content

Commit

Permalink
[Implement][EF-70] Register API for customer
Browse files Browse the repository at this point in the history
  • Loading branch information
nghiavohuynhdai authored and hideonbush106 committed Jan 25, 2024
1 parent 41dde80 commit 0e144c1
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 5 deletions.
11 changes: 10 additions & 1 deletion src/auth/controllers/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Body, Controller, Post, Req, UseGuards } from '@nestjs/common'
import { ApiBadRequestResponse, ApiBearerAuth, ApiBody, ApiOkResponse, ApiTags } from '@nestjs/swagger'
import { ErrorResponse } from '@common/contracts/dto'
import { ErrorResponse, ResponseSuccessDto } from '@common/contracts/dto'
import { LoginReqDto } from '@auth/dto/login.dto'
import { AuthService } from '@auth/services/auth.service'
import { ResponseTokenDto, TokenResDto } from '@auth/dto/token.dto'
import { UserSide } from '@common/contracts/constant'
import { JwtAuthGuard } from '@auth/guards/jwt-auth.guard'
import { RegisterReqDto } from '@auth/dto/register.dto'

@ApiTags('Auth')
@Controller()
Expand All @@ -22,6 +23,14 @@ export class AuthController {
return res
}

@Post('customer/register')
@ApiBody({ type: RegisterReqDto })
@ApiOkResponse({ type: ResponseSuccessDto })
@ApiBadRequestResponse({ type: ErrorResponse })
async register(@Body() registerReqDto: RegisterReqDto) {
return await this.authService.register(registerReqDto)
}

@UseGuards(JwtAuthGuard.REFRESH_TOKEN)
@Post('customer/refresh')
@ApiBearerAuth()
Expand Down
34 changes: 34 additions & 0 deletions src/auth/dto/register.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { ApiProperty } from '@nestjs/swagger'
import { PHONE_REGEX } from '@src/config'
import { IsEmail, IsNotEmpty, IsStrongPassword, Matches, MaxLength } from 'class-validator'

export class RegisterReqDto {
@ApiProperty()
@IsNotEmpty()
@MaxLength(30)
firstName: string

@ApiProperty()
@IsNotEmpty()
@MaxLength(30)
lastName: string

@ApiProperty()
@IsNotEmpty()
@IsEmail()
email: string

@ApiProperty()
@IsNotEmpty()
@IsStrongPassword()
password: string

@ApiProperty()
@IsNotEmpty()
@Matches(PHONE_REGEX)
phone: string

@ApiProperty()
@IsNotEmpty()
address: string
}
24 changes: 24 additions & 0 deletions src/auth/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { AccessTokenPayload } from '@auth/strategies/jwt-access.strategy'
import { RefreshTokenPayload } from '@auth/strategies/jwt-refresh.strategy'
import { TokenResDto } from '@auth/dto/token.dto'
import { ConfigService } from '@nestjs/config'
import { RegisterReqDto } from '@auth/dto/register.dto'

@Injectable()
export class AuthService {
Expand Down Expand Up @@ -51,6 +52,29 @@ export class AuthService {
}
}

public async register(registerReqDto: RegisterReqDto) {
const customer = await this.customerRepository.findOne({
conditions: {
email: registerReqDto.email
}
})

if (customer) throw new BadRequestException(Errors.EMAIL_ALREADY_EXIST.message)

const password = await this.hashPassword(registerReqDto.password)

await this.customerRepository.create({
firstName: registerReqDto.firstName,
lastName: registerReqDto.lastName,
email: registerReqDto.email,
password,
phone: registerReqDto.phone,
address: [registerReqDto.address]
})

return { success: true }
}

public async refreshAccessToken(id: string, side: UserSide): Promise<TokenResDto> {
let tokens: TokenResDto

Expand Down
13 changes: 9 additions & 4 deletions src/common/contracts/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ import { HttpStatus } from '@nestjs/common'
export const Errors = {
OBJECT_NOT_FOUND: {
error: 'OBJECT_NOT_FOUND',
message: 'không tìm thấy đối tượng',
httpStatus: HttpStatus.NOT_FOUND,
message: 'Không tìm thấy đối tượng',
httpStatus: HttpStatus.NOT_FOUND
},
WRONG_EMAIL_OR_PASSWORD: {
error: 'WRONG_EMAIL_OR_PASSWORD',
message: 'Email hoặc mật khẩu không đúng',
httpStatus: HttpStatus.BAD_REQUEST,
httpStatus: HttpStatus.BAD_REQUEST
},
};
EMAIL_ALREADY_EXIST: {
error: 'EMAIL_ALREADY_EXIST',
message: 'Email đã được sử dụng',
httpStatus: HttpStatus.BAD_REQUEST
}
}

0 comments on commit 0e144c1

Please sign in to comment.