chore: add danak services and admin login

This commit is contained in:
mahyargdz
2025-01-25 16:52:54 +03:30
parent 8af72264e7
commit 6da4b5114c
35 changed files with 505 additions and 34 deletions
+56 -11
View File
@@ -2,6 +2,7 @@ import { BadRequestException, Injectable, Logger } from "@nestjs/common";
import { TokensService } from "./tokens.service";
import { AuthMessage, UserMessage } from "../../../common/enums/message.enum";
import { RoleEnum } from "../../users/enums/role.enum";
import { UsersService } from "../../users/providers/users.service";
import { OTPService } from "../../utils/providers/otp.service";
import { PasswordService } from "../../utils/providers/password.service";
@@ -64,11 +65,7 @@ export class AuthService {
async loginWithPassword(loginDto: LoginPasswordDTO) {
const { email, password } = loginDto;
const user = await this.usersService.findOneWithEmail(email);
if (!user) throw new BadRequestException(AuthMessage.INVALID_PASSWORD);
const passCompare = await this.passwordService.comparePassword(password, user.password);
if (!passCompare) throw new BadRequestException(AuthMessage.INVALID_PASSWORD);
const user = await this.checkUserLoginCredentialWithEmail(email, password);
const tokens = this.tokensService.generateAccessAndRefreshToken({ sub: user.id, role: user.role.name });
@@ -79,6 +76,21 @@ export class AuthService {
}
//****************** */
async adminLoginWithPassword(loginDto: LoginPasswordDTO) {
const { email, password } = loginDto;
const user = await this.checkUserLoginCredentialWithEmail(email, password);
if (user.role.name !== RoleEnum.ADMIN) throw new BadRequestException(AuthMessage.NOT_ADMIN);
const tokens = this.tokensService.generateAccessAndRefreshToken({ sub: user.id, role: user.role.name });
return {
message: AuthMessage.PASSWORD_LOGIN_SUCCESS,
...tokens,
};
}
//****************** */
async checkUserExist(checkUserExistDto: CheckUserExistDto) {
const user = await this.usersService.findOneWithEmail(checkUserExistDto.email);
if (!user) throw new BadRequestException(UserMessage.USER_NOT_FOUND);
@@ -86,7 +98,6 @@ export class AuthService {
}
//****************** */
async requestLoginOtp(requestOtpDto: RequestOtpDto) {
const { phone } = requestOtpDto;
const user = await this.usersService.findOneWithPhone(phone);
@@ -114,13 +125,22 @@ export class AuthService {
async verifyLoginOtp(verifyOtpDto: VerifyOtpDto) {
const { code, phone } = verifyOtpDto;
const isValid = await this.otpService.verifyOtp(phone, code, "LOGIN");
if (!isValid) throw new BadRequestException(AuthMessage.INVALID_OTP);
const user = await this.checkUserLoginCredentialWithPhone(phone, code);
await this.otpService.delOtpFormCache(phone, "LOGIN");
const tokens = this.tokensService.generateAccessAndRefreshToken({ sub: user.id, role: user.role.name });
return {
message: AuthMessage.LOGIN_SUCCESS,
...tokens,
};
}
const user = await this.usersService.findOneWithPhone(phone);
if (!user) throw new BadRequestException(UserMessage.USER_NOT_FOUND);
//****************** */
async adminVerifyLoginOtp(verifyOtpDto: VerifyOtpDto) {
const { code, phone } = verifyOtpDto;
const user = await this.checkUserLoginCredentialWithPhone(phone, code);
if (user.role.name !== RoleEnum.ADMIN) throw new BadRequestException(AuthMessage.NOT_ADMIN);
const tokens = this.tokensService.generateAccessAndRefreshToken({ sub: user.id, role: user.role.name });
return {
@@ -129,4 +149,29 @@ export class AuthService {
};
}
//****************** */
//****************** */
private async checkUserLoginCredentialWithEmail(email: string, password: string) {
const user = await this.usersService.findOneWithEmail(email);
if (!user) throw new BadRequestException(AuthMessage.INVALID_PASSWORD);
const passCompare = await this.passwordService.comparePassword(password, user.password);
if (!passCompare) throw new BadRequestException(AuthMessage.INVALID_PASSWORD);
return user;
}
//****************** */
//****************** */
private async checkUserLoginCredentialWithPhone(phone: string, otpCode: string) {
const isValid = await this.otpService.verifyOtp(phone, otpCode, "LOGIN");
if (!isValid) throw new BadRequestException(AuthMessage.INVALID_OTP);
await this.otpService.delOtpFormCache(phone, "LOGIN");
const user = await this.usersService.findOneWithPhone(phone);
if (!user) throw new BadRequestException(UserMessage.USER_NOT_FOUND);
return user;
}
}