Skip to content

Commit

Permalink
chore : added verify otp route
Browse files Browse the repository at this point in the history
  • Loading branch information
neoandmatrix committed Dec 23, 2024
1 parent 43d77d3 commit 3cff959
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 11 deletions.
10 changes: 10 additions & 0 deletions src/services/apis/generateOtp/dto/genrateOtp.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,13 @@ export class OtpDto {

email: string;
}

export class VerifyOtpDto {
/**
* The email of the user to send OTP.
* @example "
* */

email: string;
otp: string;
}
14 changes: 11 additions & 3 deletions src/services/apis/generateOtp/generateOtp.controller.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
import { Body, Controller, Post } from '@nestjs/common';
import { GenrateOtpService } from './genrateOtp.service';
import { OtpDto } from './dto/genrateOtp.dto';
import { OtpDto, VerifyOtpDto } from './dto/genrateOtp.dto';
import { Public } from '../auth/decorators/public.decorator';

@Controller('generate-OTP')
@Controller('otp')
export class GenerateOtpController {
constructor(private readonly genrateOtpService: GenrateOtpService) {}

@Public()
@Post()
@Post('generate')
async create(@Body() genrateOtpDto: OtpDto) {
return await this.genrateOtpService.enqueueOtpJob(genrateOtpDto);
}

@Public()
@Post('verify')
async verify(@Body() verifyOtpDto: VerifyOtpDto) {
return (await this.genrateOtpService.compareOtp(verifyOtpDto))
? 'OTP is correct'
: 'OTP is incorrect';
}
}
15 changes: 13 additions & 2 deletions src/services/apis/generateOtp/genrateOtp.service.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
import { Injectable } from '@nestjs/common';
import { OtpProducer } from 'src/services/bullmq/producers/otp.producer';
import { OtpDto } from './dto/genrateOtp.dto';
import { OtpDto, VerifyOtpDto } from './dto/genrateOtp.dto';
import { processEmail } from './genrateOtp.helper';
import { OtpQueueProcessor } from 'src/services/bullmq/processors/otp.processor';

@Injectable()
export class GenrateOtpService {
constructor(private readonly otpProducer: OtpProducer) {}
constructor(
private readonly otpProducer: OtpProducer,
private readonly processor: OtpQueueProcessor,
) {}

async enqueueOtpJob(createOtpDto: OtpDto) {
const email = processEmail(createOtpDto);
await this.otpProducer.pushForAsyncStream(`process-otp`, email, {
removeOnComplete: true,
});
}

async compareOtp(verifyOtpDto: VerifyOtpDto): Promise<boolean> {
return await this.processor.comapreOtp(
verifyOtpDto.email,
verifyOtpDto.otp,
);
}
}
20 changes: 14 additions & 6 deletions src/services/bullmq/processors/otp.processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import { OTP_TTL } from 'src/constants/otp.constants';
import { hashString } from 'src/common/hashing';
import { OtpJob } from '../jobs/otp.jobs';
import generateRandomNumber from 'src/common/generate-random-number';
import { Injectable } from '@nestjs/common';

@Injectable()
@Processor(OTP_QUEUE)
export class OtpQueueProcessor extends WorkerHost {
constructor(private readonly redisService: RedisService) {
Expand All @@ -26,14 +28,20 @@ export class OtpQueueProcessor extends WorkerHost {
}

async storeOtpWithTTL(email: string, otp: number): Promise<void> {
const key = await generateKey(email, otp);
const value = new Date().toISOString();
const key = await generateKey(email);
const hashedOtp = await hashString(otp.toString());
const value = hashedOtp;
await this.redisService.set(key, value, OTP_TTL);
}

async comapreOtp(email: string, otp: string): Promise<boolean> {
const key = await generateKey(email);
const hashedOtp = await hashString(otp);
const storedOtp = await this.redisService.get(key);
return hashedOtp === storedOtp;
}
}

async function generateKey(email: string, otp: number): Promise<string> {
const hashedEmail = await hashString(email);
const hashedOtp = await hashString(otp.toString());
return `verification::email:${hashedEmail}:otp:${hashedOtp}`;
async function generateKey(email: string): Promise<string> {
return `verification::email:${email}`;
}

0 comments on commit 3cff959

Please sign in to comment.