parent
ad738f2c61
commit
4a2c479879
4 changed files with 72 additions and 27 deletions
@ -1,17 +1,18 @@ |
|||||||
import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common'; |
import { Controller, Get, Post, Body, Patch, Param, Delete } from "@nestjs/common"; |
||||||
import { AdminService } from './admin.service'; |
import { AdminService } from "./admin.service"; |
||||||
import { Injectable, UnauthorizedException } from '@nestjs/common'; |
import { Admin } from "./entities/admin.entity"; |
||||||
import { InjectModel } from '@nestjs/sequelize'; |
|
||||||
import { Admin } from './entities/admin.entity'; |
|
||||||
import * as bcrypt from 'bcrypt'; |
|
||||||
import { JwtService } from '@nestjs/jwt'; |
|
||||||
|
|
||||||
@Controller('admin') |
@Controller("admin") |
||||||
export class AdminController { |
export class AdminController { |
||||||
constructor(private readonly adminService: AdminService) {} |
constructor(private readonly adminService: AdminService) {} |
||||||
@Post('register') |
@Post("register") |
||||||
async register(@Body() body:{email: string, password: string}):Promise<Admin>{ |
async register(@Body() body: { email: string; password: string }): Promise<Admin> { |
||||||
const {email, password} = body |
const { email, password } = body; |
||||||
return this.adminService.register(email, password); |
return this.adminService.register(email, password); |
||||||
} |
} |
||||||
|
@Post("login") |
||||||
|
async login(@Body() body: { email: string; password: string }): Promise<{ token: string }> { |
||||||
|
const { email, password } = body; |
||||||
|
return this.adminService.login(email, password); |
||||||
|
} |
||||||
} |
} |
||||||
|
@ -1,40 +1,69 @@ |
|||||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; |
import { HttpException, HttpStatus, Injectable } from "@nestjs/common"; |
||||||
import { InjectModel } from '@nestjs/sequelize'; |
import { InjectModel } from "@nestjs/sequelize"; |
||||||
import { Admin } from './entities/admin.entity'; |
import { Admin } from "./entities/admin.entity"; |
||||||
|
import * as bcrypt from "bcrypt"; |
||||||
|
import { JwtService } from "@nestjs/jwt"; |
||||||
|
import { ConfigService } from "@nestjs/config"; |
||||||
@Injectable() |
@Injectable() |
||||||
export class AdminService { |
export class AdminService { |
||||||
constructor(@InjectModel(Admin) private readonly adminModel: typeof Admin){} |
constructor( |
||||||
|
@InjectModel(Admin) private readonly adminModel: typeof Admin, |
||||||
|
private readonly jwtService: JwtService, |
||||||
|
private readonly configService: ConfigService, |
||||||
|
) {} |
||||||
async register(email: string, password: string): Promise<Admin> { |
async register(email: string, password: string): Promise<Admin> { |
||||||
try { |
try { |
||||||
if (!email) { |
if (!email) { |
||||||
throw new HttpException('Email should be entered.', HttpStatus.BAD_REQUEST); |
throw new HttpException("Email should be entered.", HttpStatus.BAD_REQUEST); |
||||||
} |
} |
||||||
|
|
||||||
if (!password) { |
if (!password) { |
||||||
throw new HttpException('Password should be entered.', HttpStatus.BAD_REQUEST); |
throw new HttpException("Password should be entered.", HttpStatus.BAD_REQUEST); |
||||||
} |
} |
||||||
|
|
||||||
const existingAdmin = await this.adminModel.findOne({ where: { email } }); |
const existingAdmin = await this.adminModel.findOne({ where: { email } }); |
||||||
if (existingAdmin) { |
if (existingAdmin) { |
||||||
throw new HttpException('Email is already registered.', HttpStatus.CONFLICT); |
throw new HttpException("Email is already registered.", HttpStatus.CONFLICT); |
||||||
} |
} |
||||||
|
|
||||||
|
const hashedPassword = await bcrypt.hash(password, 10); |
||||||
|
|
||||||
const admin = await this.adminModel.create({ |
const admin = await this.adminModel.create({ |
||||||
email, |
email, |
||||||
password, |
password: hashedPassword, |
||||||
role: 'admin', |
role: "admin", |
||||||
}); |
}); |
||||||
|
|
||||||
return admin; |
return admin; |
||||||
} catch (error) { |
} catch (error) { |
||||||
if (error instanceof HttpException) { |
if (error instanceof HttpException) { |
||||||
throw error;
|
throw error; |
||||||
} |
} |
||||||
throw new HttpException( |
throw new HttpException("An error occurred during registration.", HttpStatus.INTERNAL_SERVER_ERROR); |
||||||
'An error occurred during registration.', |
} |
||||||
HttpStatus.INTERNAL_SERVER_ERROR, |
} |
||||||
|
|
||||||
|
async login(email: string, password: string): Promise<{ token: string }> { |
||||||
|
try { |
||||||
|
const admin = await this.adminModel.findOne({ where: { email } }); |
||||||
|
if (!admin) { |
||||||
|
throw new HttpException("Invalid email or password", HttpStatus.UNAUTHORIZED); |
||||||
|
} |
||||||
|
const isValidPassword = await bcrypt.compare(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 }, |
||||||
|
{ |
||||||
|
secret: this.configService.get<string>("JWT_SECRET"), |
||||||
|
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); |
||||||
} |
} |
||||||
} |
} |
||||||
} |
} |
||||||
|
Loading…
Reference in new issue