Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
JC-Coder committed Jun 23, 2024
1 parent 539a4aa commit d0fd29c
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 70 deletions.
4 changes: 0 additions & 4 deletions src/common/enums/otp.enum.ts

This file was deleted.

6 changes: 6 additions & 0 deletions src/common/interfaces/otp.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface ISendOtp {
email: string;
title: string;
template: string;
code: number;
}
33 changes: 20 additions & 13 deletions src/module/v1/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import {
import { BaseHelper } from '../../../common/utils/helper.util';
import { JwtService } from '@nestjs/jwt';
import { OtpService } from '../otp/otp.service';
import { OtpTypeEnum } from 'src/common/enums/otp.enum';
import { VerifyEmailTemplate } from 'src/module/v1/mail/templates/verify-email.email';
import { ForgotPasswordTemplate } from 'src/module/v1/mail/templates/forgot-password.email';

@Injectable()
export class AuthService {
Expand All @@ -32,10 +33,8 @@ export class AuthService {
async register(payload: CreateUserDto) {
const user = await this.userService.createUser(payload);

await this.otpService.sendOTP({
email: user.email,
type: OtpTypeEnum.VERIFY_EMAIL,
});
// send verification email
await this.sendVerificationMail({ email: user?.email });

return user;
}
Expand Down Expand Up @@ -83,29 +82,38 @@ export class AuthService {
await this.otpService.verifyOTP({
code,
email,
type: OtpTypeEnum.VERIFY_EMAIL,
});

await this.userService.updateUserByEmail(email, {
emailVerified: true,
});
}

// resend verification email
async sendVerificationMail(payload: RequestVerifyEmailOtpDto) {
await this.userService.checkUserExistByEmail(payload.email);
const { email } = payload;

await this.userService.checkUserExistByEmail(email);

const code = BaseHelper.generateOTP();
await this.otpService.sendOTP({
...payload,
type: OtpTypeEnum.VERIFY_EMAIL,
email,
title: 'Verify Email',
template: VerifyEmailTemplate({ code }),
code,
});
}

async sendPasswordResetEmail(payload: ForgotPasswordDto) {
await this.userService.checkUserExistByEmail(payload.email);
const { email } = payload;
await this.userService.checkUserExistByEmail(email);

const code = BaseHelper.generateOTP();
await this.otpService.sendOTP({
...payload,
type: OtpTypeEnum.RESET_PASSWORD,
email,
title: 'Reset Password',
code,
template: ForgotPasswordTemplate({ code }),
});
}

Expand All @@ -119,7 +127,6 @@ export class AuthService {
await this.otpService.verifyOTP({
email,
code,
type: OtpTypeEnum.RESET_PASSWORD,
});

const hashedPassword = await BaseHelper.hashData(password);
Expand Down
23 changes: 4 additions & 19 deletions src/module/v1/otp/dto/otp.dto.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,13 @@
import { IsEmail, IsEnum, IsNumber } from 'class-validator';
import { OtpTypeEnum } from 'src/common/enums/otp.enum';
import { IsEmail, IsNotEmpty, IsNumber } from 'class-validator';

export class CreateOtpDto {
@IsEnum(OtpTypeEnum)
type: OtpTypeEnum;

@IsEmail()
email: string;

@IsNumber()
code: number;
}

export class SendOtpDto {
@IsEnum(OtpTypeEnum)
type: OtpTypeEnum;

@IsEmail()
@IsNotEmpty()
email: string;
}

export class VerifyOtpDto extends SendOtpDto {
@IsNumber()
@IsNotEmpty()
code: number;
}

export class ValidateOtpDto extends VerifyOtpDto {}
export class ValidateOtpDto extends CreateOtpDto {}
37 changes: 9 additions & 28 deletions src/module/v1/otp/otp.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ import { Injectable, InternalServerErrorException, NotFoundException } from '@ne
import { InjectModel } from '@nestjs/mongoose';
import { OTP, OTPDocument } from './schemas/otp.schema';
import { Model } from 'mongoose';
import { BaseHelper } from 'src/common/utils/helper.util';
import { CreateOtpDto, SendOtpDto, ValidateOtpDto, VerifyOtpDto } from './dto/otp.dto';
import { CreateOtpDto, ValidateOtpDto } from './dto/otp.dto';
import { MailService } from '../mail/mail.service';
import { OtpTypeEnum } from 'src/common/enums/otp.enum';
import { VerifyEmailTemplate } from '../mail/templates/verify-email.email';
import { ForgotPasswordTemplate } from '../mail/templates/forgot-password.email';
import { ISendOtp } from 'src/common/interfaces/otp.interface';

@Injectable()
export class OtpService {
Expand All @@ -32,10 +29,11 @@ export class OtpService {
return this.otpModel.findOne({ code });
}

// this only checks if the otp is valid, it won't delete it
async validateOTP(payload: ValidateOtpDto) {
const { email, code, type } = payload;
const { email, code } = payload;

const otp = await this.otpModel.findOne({ email, code, type });
const otp = await this.otpModel.findOne({ email, code });

if (!otp) {
throw new NotFoundException('Invalid OTP code');
Expand All @@ -44,42 +42,25 @@ export class OtpService {
return otp;
}

async verifyOTP(payload: VerifyOtpDto) {
async verifyOTP(payload: ValidateOtpDto) {
const otp = await this.validateOTP(payload);

await this.deleteOTP(otp._id.toString());

return true;
}

async sendOTP(payload: SendOtpDto) {
const { email, type } = payload;

const code = BaseHelper.generateOTP();

let template: string;
let subject: string;

switch (type) {
case OtpTypeEnum.RESET_PASSWORD:
template = ForgotPasswordTemplate({ code });
subject = 'Reset Your Password';
break;
case OtpTypeEnum.VERIFY_EMAIL:
template = VerifyEmailTemplate({ code });
subject = 'Verify Email';
break;
}
async sendOTP(payload: ISendOtp) {
const { email, title, template, code } = payload;

const otp = await this.createOTP({
email,
code,
type,
});

if (!otp) throw new InternalServerErrorException('Unable to send otp at the moment , try again later');

await this.mailService.sendEmail(email, subject, template);
await this.mailService.sendEmail(email, title, template);
}

async deleteOTP(id: string) {
Expand Down
8 changes: 2 additions & 6 deletions src/module/v1/otp/schemas/otp.schema.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { OtpTypeEnum } from 'src/common/enums/otp.enum';

export type OTPDocument = OTP & Document;

@Schema({ expires: 300 })
@Schema({ expires: 600 })
export class OTP {
@Prop({ required: true, unique: true })
email: string;

@Prop({ required: true, unique: true })
code: number;

@Prop({ required: true, enum: OtpTypeEnum })
type: string;

@Prop({ default: new Date() })
createdAt: Date;

@Prop({ default: Date.now(), expires: 300 })
@Prop({ default: Date.now, expires: 600 })
expiresAt: Date;
}

Expand Down

0 comments on commit d0fd29c

Please sign in to comment.