add : forget password
This commit is contained in:
@@ -17,6 +17,10 @@ import { ChangePasswordDto } from "../DTO/change-password.dto";
|
||||
import { CheckUserExistDto, LoginPasswordDTO } from "../DTO/loginPassword.dto";
|
||||
import { RequestOtpDto } from "../DTO/request-otp.dto";
|
||||
import { VerifyOtpDto } from "../DTO/verify-otp.dto";
|
||||
import { ForgetPasswordDto } from "../DTO/forget-password.dto";
|
||||
import { VerifyForgotPasswordOtpDto } from "../DTO/verify-forgot-otp.dto";
|
||||
import { EmailService } from "../../utils/providers/email.service";
|
||||
|
||||
@Injectable()
|
||||
export class AuthService {
|
||||
private readonly logger = new Logger(AuthService.name);
|
||||
@@ -29,7 +33,8 @@ export class AuthService {
|
||||
private readonly dataSource: DataSource,
|
||||
private readonly notificationQueue: NotificationQueue,
|
||||
private readonly referralsService: ReferralsService,
|
||||
) {}
|
||||
private readonly emailService: EmailService,
|
||||
) { }
|
||||
|
||||
//****************** */
|
||||
//****************** */
|
||||
@@ -99,7 +104,7 @@ export class AuthService {
|
||||
await this.notificationQueue.addLoginOtpNotification({ phone, code: otpCode });
|
||||
|
||||
return {
|
||||
message: AuthMessage.OTP_SENT+otpCode,
|
||||
message: AuthMessage.OTP_SENT + otpCode,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -244,7 +249,58 @@ export class AuthService {
|
||||
|
||||
return user;
|
||||
}
|
||||
//****************** */
|
||||
//****************** */
|
||||
async requestForgotPasswordOtp(dto: ForgetPasswordDto) {
|
||||
const { email } = dto;
|
||||
|
||||
const user = await this.usersService.findOneWithEmail(email)
|
||||
|
||||
if (!user) {
|
||||
throw new BadRequestException("کاربری با این ایمیل یافت نشد")
|
||||
}
|
||||
|
||||
const existCode = await this.otpService.checkExistOtp(email, "FORGOT_PASSWORD");
|
||||
|
||||
if (existCode) {
|
||||
return {
|
||||
message: AuthMessage.OTP_ALREADY_SENT,
|
||||
ttlSecond: existCode,
|
||||
};
|
||||
}
|
||||
|
||||
const otpCode = await this.otpService.generateAndSetInCache(email, "FORGOT_PASSWORD");
|
||||
|
||||
await this.emailService.sendOtpEmail({ userEmail: email, otp: otpCode });
|
||||
|
||||
this.logger.log(`otp code: ${otpCode} for email: ${email}`);
|
||||
|
||||
return { message: 'success' }
|
||||
}
|
||||
|
||||
async verifyForgotPasswordOtp(dto: VerifyForgotPasswordOtpDto) {
|
||||
const { code, email, newPassword } = dto;
|
||||
|
||||
const isValid = await this.otpService.verifyOtp(email, code, "FORGOT_PASSWORD");
|
||||
|
||||
if (!isValid) throw new BadRequestException(AuthMessage.INVALID_OTP);
|
||||
|
||||
let user = await this.usersService.findOneWithEmail(email);
|
||||
|
||||
if (!user) {
|
||||
throw new BadRequestException('کاربر پیدا نشد .')
|
||||
}
|
||||
|
||||
const hashedPassword = await this.passwordService.hashPassword(newPassword);
|
||||
|
||||
await this.otpService.delOtpFormCache(email, "FORGOT_PASSWORD");
|
||||
|
||||
await this.usersService.updateUserPassword(user.id, hashedPassword);
|
||||
|
||||
return {
|
||||
message: AuthMessage.PASSWORD_CHANGED_SUCCESSFULLY
|
||||
};
|
||||
}
|
||||
//****************** */
|
||||
//****************** */
|
||||
private checkUserIsAdmin(roles: Role[]) {
|
||||
|
||||
Reference in New Issue
Block a user