added cooldown with OTP

This commit is contained in:
unknown
2024-08-21 14:25:06 +03:00
parent 95a315db8f
commit eb7d294471
2 changed files with 16 additions and 4 deletions

View File

@ -71,7 +71,7 @@ export class UserAuthController {
return {
statusCode: HttpStatus.OK,
data: {
otp: otpCode,
...otpCode,
},
message: 'Otp Send Successfully',
};

View File

@ -140,7 +140,10 @@ export class UserAuthService {
return await this.userRepository.findOne({ where: { uuid: id } });
}
async generateOTP(data: UserOtpDto): Promise<string> {
async generateOTP(data: UserOtpDto): Promise<{
otpCode: string;
cooldown: number;
}> {
const otpLimiter = new Date();
otpLimiter.setDate(
otpLimiter.getDate() - this.configService.get<number>('OTP_LIMITER'),
@ -178,7 +181,7 @@ export class UserAuthService {
order: { createdAt: 'DESC' },
withDeleted: true,
});
const cooldown = 30 * Math.pow(2, countOfOtp - 1);
let cooldown = 30 * Math.pow(2, countOfOtp - 1);
if (lastOtp) {
const now = new Date();
const timeSinceLastOtp = differenceInSeconds(now, lastOtp.createdAt);
@ -201,10 +204,19 @@ export class UserAuthService {
expiryTime,
type: data.type,
});
const countOfOtpToReturn = await this.otpRepository.count({
withDeleted: true,
where: {
email: data.email,
type: data.type,
createdAt: MoreThan(otpLimiter),
},
});
cooldown = 30 * Math.pow(2, countOfOtpToReturn - 1);
const subject = 'OTP send successfully';
const message = `Your OTP code is ${otpCode}`;
this.emailService.sendEmail(data.email, subject, message);
return otpCode;
return { otpCode, cooldown };
}
async verifyOTP(data: VerifyOtpDto): Promise<boolean> {