feat:forget password

This commit is contained in:
Abdalhamid Alhamad
2024-12-08 13:15:38 +03:00
parent c486d558ad
commit 90ee8023e6
19 changed files with 154 additions and 48 deletions

View File

@ -1,7 +1,8 @@
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { DEFAULT_OTP_DIGIT, DEFAULT_OTP_LENGTH } from '../constants';
import { GenerateOtpRequestDto, VerifyOtpRequestDto } from '../dtos/request';
import { OtpType } from '../enums';
import { ISendOtp, IVerifyOtp } from '../interfaces';
import { OtpRepository } from '../repositories';
import { generateRandomOtp } from '../utils';
@ -9,23 +10,25 @@ import { generateRandomOtp } from '../utils';
export class OtpService {
constructor(private readonly configService: ConfigService, private readonly otpRepository: OtpRepository) {}
private useMock = this.configService.get<boolean>('USE_MOCK', false);
async generateAndSendOtp(sendotpRequest: GenerateOtpRequestDto) {
async generateAndSendOtp(sendotpRequest: ISendOtp): Promise<string> {
const otp = this.useMock ? DEFAULT_OTP_DIGIT.repeat(DEFAULT_OTP_LENGTH) : generateRandomOtp(DEFAULT_OTP_LENGTH);
await this.otpRepository.createOtp({ ...sendotpRequest, value: otp });
this.sendOtp(sendotpRequest, otp);
return sendotpRequest.phoneNumber.replace(/.(?=.{4})/g, '*');
return sendotpRequest.otpType == OtpType.EMAIL
? sendotpRequest.recipient
: sendotpRequest.recipient?.replace(/.(?=.{4})/g, '*');
}
async verifyOtp(verifyOtpRequest: VerifyOtpRequestDto) {
async verifyOtp(verifyOtpRequest: IVerifyOtp) {
const otp = await this.otpRepository.findOtp(verifyOtpRequest);
return !!otp;
}
private sendOtp(sendotpRequest: GenerateOtpRequestDto, otp: string) {
private sendOtp(sendotpRequest: ISendOtp, otp: string) {
// TODO: send OTP to the user
return;
}