Create DTO for admin login

master
nicekid1 2 months ago
parent 7240744076
commit 4506469b9b
  1. 11
      src/admin/admin.controller.ts
  2. 15
      src/admin/admin.service.ts
  3. 15
      src/admin/dto/login-Admin.dto.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);
}
}

@ -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<Admin> {
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<string | number>("JWT_EXPIRES") || "1h",
},
);
return { token };
} catch (error) {
console.log(error);
throw new HttpException("An error occurred during login.", HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}

@ -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;
}
Loading…
Cancel
Save