From 4506469b9bd7114bad7fb83ca4a021264f6ba2b0 Mon Sep 17 00:00:00 2001 From: nicekid1 <86746988+nicekid1@users.noreply.github.com> Date: Sat, 4 Jan 2025 14:12:10 +0330 Subject: [PATCH] Create DTO for admin login --- src/admin/admin.controller.ts | 11 +++++++---- src/admin/admin.service.ts | 15 +++++++++++---- src/admin/dto/login-Admin.dto.ts | 15 +++++++++++++++ 3 files changed, 33 insertions(+), 8 deletions(-) create mode 100644 src/admin/dto/login-Admin.dto.ts diff --git a/src/admin/admin.controller.ts b/src/admin/admin.controller.ts index 26d8da1..6df3a71 100644 --- a/src/admin/admin.controller.ts +++ b/src/admin/admin.controller.ts @@ -1,7 +1,10 @@ -import { Controller, Get, Post, Body, Patch, Param, Delete } from "@nestjs/common"; +import { Controller, Get, Post, Body,Request, Put, UseGuards } from "@nestjs/common"; import { AdminService } from "./admin.service"; import { Admin } from "./entities/admin.entity"; import { CreateAdminDto } from "./dto/create-Admin.dto"; +import { LoginAdminDto } from "./dto/login-Admin.dto"; +import { JwtAuthGuard } from "src/guard/auth.guard"; +import { UpdateUserDto } from "./dto/update-user.dto"; @Controller("admin") export class AdminController { @@ -11,8 +14,8 @@ export class AdminController { return this.adminService.register(createAdminDto); } @Post("login") - async login(@Body() body: { email: string; password: string }): Promise<{ token: string }> { - const { email, password } = body; - return this.adminService.login(email, password); + async login(@Body() loginAdminDto:LoginAdminDto ): Promise<{ token: string }> { + return this.adminService.login(loginAdminDto); } + } diff --git a/src/admin/admin.service.ts b/src/admin/admin.service.ts index 464127b..7441bfe 100644 --- a/src/admin/admin.service.ts +++ b/src/admin/admin.service.ts @@ -5,6 +5,7 @@ import * as bcrypt from "bcrypt"; import { JwtService } from "@nestjs/jwt"; import { ConfigService } from "@nestjs/config"; import { CreateAdminDto } from "./dto/create-Admin.dto"; +import { LoginAdminDto } from "./dto/login-Admin.dto"; @Injectable() export class AdminService { constructor( @@ -12,6 +13,7 @@ export class AdminService { private readonly jwtService: JwtService, private readonly configService: ConfigService, ) {} + //register method async register(createAdminDto:CreateAdminDto): Promise { try { const existingAdmin = await this.adminModel.findOne({ where: { email:createAdminDto.email } }); @@ -32,17 +34,20 @@ export class AdminService { throw new HttpException("An error occurred during registration.", HttpStatus.INTERNAL_SERVER_ERROR); } } - - async login(email: string, password: string): Promise<{ token: string }> { + //login method + async login(loginAdminDto: LoginAdminDto): Promise<{ token: string }> { try { - const admin = await this.adminModel.findOne({ where: { email } }); + const admin = await this.adminModel.findOne({ where: { email: loginAdminDto.email } }); + if (!admin) { throw new HttpException("Invalid email or password", HttpStatus.UNAUTHORIZED); } - const isValidPassword = await bcrypt.compare(password, admin.password); + + const isValidPassword = await bcrypt.compare(loginAdminDto.password, admin.password); if (!isValidPassword) { throw new HttpException("Invalid email or password", HttpStatus.UNAUTHORIZED); } + const token = this.jwtService.sign( { id: admin.id, role: admin.role }, { @@ -50,10 +55,12 @@ export class AdminService { expiresIn: this.configService.get("JWT_EXPIRES") || "1h", }, ); + return { token }; } catch (error) { console.log(error); throw new HttpException("An error occurred during login.", HttpStatus.INTERNAL_SERVER_ERROR); } } + } diff --git a/src/admin/dto/login-Admin.dto.ts b/src/admin/dto/login-Admin.dto.ts new file mode 100644 index 0000000..275e57d --- /dev/null +++ b/src/admin/dto/login-Admin.dto.ts @@ -0,0 +1,15 @@ +import { IsString, IsEmail, IsEnum, IsNotEmpty, IsOptional, Matches } from "class-validator"; + +export class LoginAdminDto { + @IsEmail({}, { message: "Invalid email format" }) + email: string; + + @IsString() + @IsNotEmpty({ message: "Password is required" }) + password: string; + + @IsString() + @IsNotEmpty({ message: "Username is required" }) + username: string; + +}