From 86ce21487ad4f92ecf699f657c9891822d620ae5 Mon Sep 17 00:00:00 2001 From: nicekid1 <86746988+nicekid1@users.noreply.github.com> Date: Sat, 4 Jan 2025 11:37:57 +0330 Subject: [PATCH] Create DTO for login route and refactor related routes --- src/users/dto/login-user.dto.ts | 15 +++++++++++++++ src/users/users.controller.ts | 11 ++++++----- src/users/users.service.ts | 9 ++++----- 3 files changed, 25 insertions(+), 10 deletions(-) create mode 100644 src/users/dto/login-user.dto.ts diff --git a/src/users/dto/login-user.dto.ts b/src/users/dto/login-user.dto.ts new file mode 100644 index 0000000..9c1bbc8 --- /dev/null +++ b/src/users/dto/login-user.dto.ts @@ -0,0 +1,15 @@ +import { IsString, IsEmail, IsEnum, IsNotEmpty, IsOptional, Matches } from "class-validator"; + +export class LoginUserDto { + @IsEmail({}, { message: "Invalid email format" }) + email: string; + + @IsString() + @IsNotEmpty({ message: "Password is required" }) + password: string; + + @IsString() + @IsNotEmpty({ message: "Username is required" }) + username: string; + +} diff --git a/src/users/users.controller.ts b/src/users/users.controller.ts index 6de2520..8230871 100644 --- a/src/users/users.controller.ts +++ b/src/users/users.controller.ts @@ -2,19 +2,20 @@ import { Controller, Post, Body} from "@nestjs/common"; import { UsersService } from "./users.service"; import { User } from "./entities/user.entity"; import { CreateUserDto } from "./dto/create-user.dto"; +import { LoginUserDto } from "./dto/login-user.dto"; @Controller("user") export class UsersController { constructor(private readonly usersService: UsersService) {} - + //register as user @Post("register") async register(@Body() createUserDto: CreateUserDto):Promise { return this.usersService.register(createUserDto); } - + //login as user @Post("login") - async login(@Body() body: { email: string; password: string }):Promise<{token}> { - const { email, password } = body; - return this.usersService.login(email, password); + async login(@Body() loginUserDto:LoginUserDto):Promise<{token}> { + return this.usersService.login(loginUserDto); } + } diff --git a/src/users/users.service.ts b/src/users/users.service.ts index 4e4f17d..98b2d34 100644 --- a/src/users/users.service.ts +++ b/src/users/users.service.ts @@ -5,6 +5,7 @@ import * as bcrypt from "bcrypt"; import { JwtService } from "@nestjs/jwt"; import { ConfigService } from "@nestjs/config"; import { CreateUserDto } from "./dto/create-user.dto"; +import { LoginUserDto } from "./dto/login-user.dto"; @Injectable() export class UsersService { @@ -41,18 +42,16 @@ export class UsersService { // Login method - async login(email: string, password: string): Promise<{ token: string }> { - if (!email) throw new BadRequestException("Email should be entered"); - if (!password) throw new BadRequestException("Password should be entered"); + async login(loginUserDto:LoginUserDto): Promise<{ token: string }> { const user = await this.userModel.findOne({ - where: { email }, + where: { email:loginUserDto.email }, }); if (!user) { throw new UnauthorizedException("Invalid email or password"); } - const passwordMatch = await bcrypt.compare(password, user.password); + const passwordMatch = await bcrypt.compare(loginUserDto.password, user.password); if (!passwordMatch) { throw new UnauthorizedException("Invalid email or password"); }