mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-08-26 06:09:41 +00:00
feat:forget password
This commit is contained in:
@ -1,20 +0,0 @@
|
||||
import { IsNotEmpty } from 'class-validator';
|
||||
import { UserLocale } from '~/core/enums';
|
||||
import { OtpScope, OtpType } from '../../enums';
|
||||
|
||||
export class GenerateOtpRequestDto {
|
||||
@IsNotEmpty()
|
||||
userId!: string;
|
||||
|
||||
@IsNotEmpty()
|
||||
phoneNumber!: string;
|
||||
|
||||
@IsNotEmpty()
|
||||
scope!: OtpScope;
|
||||
|
||||
@IsNotEmpty()
|
||||
language?: UserLocale = UserLocale.ENGLISH;
|
||||
|
||||
@IsNotEmpty()
|
||||
otpType!: OtpType;
|
||||
}
|
@ -1,2 +0,0 @@
|
||||
export * from './generate-otp-request.request.dto';
|
||||
export * from './verify-otp-request.dto';
|
||||
|
@ -1,16 +0,0 @@
|
||||
import { IsNotEmpty } from 'class-validator';
|
||||
import { OtpScope, OtpType } from '../../enums';
|
||||
|
||||
export class VerifyOtpRequestDto {
|
||||
@IsNotEmpty()
|
||||
userId!: string;
|
||||
|
||||
@IsNotEmpty()
|
||||
scope!: OtpScope;
|
||||
|
||||
@IsNotEmpty()
|
||||
otpType!: OtpType;
|
||||
|
||||
@IsNotEmpty()
|
||||
value!: string;
|
||||
}
|
@ -1,3 +1,4 @@
|
||||
export enum OtpScope {
|
||||
VERIFY_PHONE = 'VERIFY_PHONE',
|
||||
FORGET_PASSWORD = 'FORGET_PASSWORD',
|
||||
}
|
||||
|
2
src/common/modules/otp/interfaces/index.ts
Normal file
2
src/common/modules/otp/interfaces/index.ts
Normal file
@ -0,0 +1,2 @@
|
||||
export * from './send-otp.interface';
|
||||
export * from './verify-otp.interface';
|
9
src/common/modules/otp/interfaces/send-otp.interface.ts
Normal file
9
src/common/modules/otp/interfaces/send-otp.interface.ts
Normal file
@ -0,0 +1,9 @@
|
||||
import { OtpScope, OtpType } from '../enums';
|
||||
|
||||
export interface ISendOtp {
|
||||
userId: string;
|
||||
scope: OtpScope;
|
||||
language?: string;
|
||||
otpType: OtpType;
|
||||
recipient: string;
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
import { OtpScope, OtpType } from '../enums';
|
||||
|
||||
export interface IVerifyOtp {
|
||||
userId: string;
|
||||
scope: OtpScope;
|
||||
otpType: OtpType;
|
||||
value: string;
|
||||
}
|
@ -1,8 +1,8 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { MoreThan, Repository } from 'typeorm';
|
||||
import { VerifyOtpRequestDto } from '../dtos/request';
|
||||
import { Otp } from '../entities';
|
||||
import { IVerifyOtp } from '../interfaces';
|
||||
const FIVE = 5;
|
||||
const SIXTY = 60;
|
||||
const ONE_THOUSAND = 1000;
|
||||
@ -23,7 +23,7 @@ export class OtpRepository {
|
||||
);
|
||||
}
|
||||
|
||||
findOtp(otp: VerifyOtpRequestDto) {
|
||||
findOtp(otp: IVerifyOtp) {
|
||||
return this.otpRepository.findOne({
|
||||
where: {
|
||||
userId: otp.userId,
|
||||
|
@ -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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user