|
|
@ -17,15 +17,31 @@ export class AdminService { |
|
|
|
//register method
|
|
|
|
//register method
|
|
|
|
async register(createAdminDto: CreateAdminDto): Promise<{ message }> { |
|
|
|
async register(createAdminDto: CreateAdminDto): Promise<{ message }> { |
|
|
|
try { |
|
|
|
try { |
|
|
|
const existingAdmin = await this.adminModel.findOne({ |
|
|
|
const existingAdminByEmail = await this.adminModel.findOne({ |
|
|
|
where: { email: createAdminDto.email }, |
|
|
|
where: { email: createAdminDto.email }, |
|
|
|
}); |
|
|
|
}); |
|
|
|
if (existingAdmin) { |
|
|
|
if (existingAdminByEmail) { |
|
|
|
throw new HttpException("The provided email is already registered.", HttpStatus.CONFLICT); |
|
|
|
throw new HttpException("The provided email is already registered.", HttpStatus.CONFLICT); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const existingAdminByUsername = await this.adminModel.findOne({ |
|
|
|
|
|
|
|
where: { username: createAdminDto.username }, |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
if (existingAdminByUsername) { |
|
|
|
|
|
|
|
throw new HttpException("The provided username is already taken.", HttpStatus.CONFLICT); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const existingAdminByPhoneNumber = await this.adminModel.findOne({ |
|
|
|
|
|
|
|
where: { phoneNumber: createAdminDto.phoneNumber }, |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
if (existingAdminByPhoneNumber) { |
|
|
|
|
|
|
|
throw new HttpException("The provided phone number is already registered.", HttpStatus.CONFLICT); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
createAdminDto.password = await bcrypt.hash(createAdminDto.password, 10); |
|
|
|
createAdminDto.password = await bcrypt.hash(createAdminDto.password, 10); |
|
|
|
|
|
|
|
|
|
|
|
await this.adminModel.create(createAdminDto); |
|
|
|
await this.adminModel.create(createAdminDto); |
|
|
|
return { message: "admin register is successful" }; |
|
|
|
return { message: "Admin registration is successful." }; |
|
|
|
} catch (error) { |
|
|
|
} catch (error) { |
|
|
|
if (error instanceof HttpException) { |
|
|
|
if (error instanceof HttpException) { |
|
|
|
throw error; |
|
|
|
throw error; |
|
|
@ -34,14 +50,14 @@ export class AdminService { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
//login method
|
|
|
|
//login method
|
|
|
|
async login(loginAdminDto: LoginAdminDto): Promise<{ accessToken: string }> { |
|
|
|
async login(loginAdminDto: LoginAdminDto): Promise<{ accessToken: string; refreshToken: string }> { |
|
|
|
try { |
|
|
|
try { |
|
|
|
const admin = await this.adminModel.findOne({ |
|
|
|
const admin = await this.adminModel.findOne({ |
|
|
|
where: { email: loginAdminDto.email }, |
|
|
|
where: { email: loginAdminDto.email,username:loginAdminDto.username }, |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
if (!admin) { |
|
|
|
if (!admin) { |
|
|
|
throw new HttpException("Invalid email or password.", HttpStatus.UNAUTHORIZED); |
|
|
|
throw new HttpException("Invalid email, username or password.", HttpStatus.UNAUTHORIZED); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const isValidPassword = await bcrypt.compare(loginAdminDto.password, admin.password); |
|
|
|
const isValidPassword = await bcrypt.compare(loginAdminDto.password, admin.password); |
|
|
@ -69,7 +85,7 @@ export class AdminService { |
|
|
|
{ where: { id: admin.id } }, |
|
|
|
{ where: { id: admin.id } }, |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
return { accessToken }; |
|
|
|
return { accessToken, refreshToken }; |
|
|
|
} catch (error) { |
|
|
|
} catch (error) { |
|
|
|
if (error instanceof HttpException) { |
|
|
|
if (error instanceof HttpException) { |
|
|
|
throw error; |
|
|
|
throw error; |
|
|
@ -94,27 +110,25 @@ export class AdminService { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
//getting new access token
|
|
|
|
//getting new access token
|
|
|
|
async refreshToken(userId: number): Promise<{ accessToken: string }> { |
|
|
|
async newAccessToken(refreshToken: string) { |
|
|
|
const refreshToken = (await this.adminModel.findOne({ where: { id: userId } })).refreshToken; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!refreshToken) { |
|
|
|
if (!refreshToken) { |
|
|
|
throw new HttpException("Refresh token is required.", HttpStatus.BAD_REQUEST); |
|
|
|
throw new HttpException("Refresh token is required.", HttpStatus.BAD_REQUEST); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
let decoded; |
|
|
|
let decoded; |
|
|
|
try { |
|
|
|
try { |
|
|
|
decoded = this.jwtService.verify(refreshToken, { secret: process.env.JWT_REFRESH_SECRET }); |
|
|
|
decoded = this.jwtService.verify(refreshToken, { secret: this.configService.get<string>("JWT_REFRESH_SECRET") }); |
|
|
|
} catch (error) { |
|
|
|
} catch (error) { |
|
|
|
throw new HttpException("Invalid or expired token.", HttpStatus.UNAUTHORIZED); |
|
|
|
throw new HttpException("Invalid or expired token.", HttpStatus.UNAUTHORIZED); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const user = await this.adminModel.findOne({ where: { id: decoded.id } }); |
|
|
|
const user = await this.adminModel.findOne({where:{id:decoded.id}}); |
|
|
|
if (!user) { |
|
|
|
if (!user) { |
|
|
|
throw new HttpException("User not found.", HttpStatus.NOT_FOUND); |
|
|
|
throw new HttpException("User not found.", HttpStatus.NOT_FOUND); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (user.role !== "admin") { |
|
|
|
if (user.refreshToken !== refreshToken) { |
|
|
|
throw new HttpException("You are not authorized to get an admin token.", HttpStatus.FORBIDDEN); |
|
|
|
throw new HttpException("Invalid refresh token.", HttpStatus.FORBIDDEN); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const accessToken = this.jwtService.sign( |
|
|
|
const accessToken = this.jwtService.sign( |
|
|
|