mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-15 18:27:05 +00:00
added cooldown with OTP
This commit is contained in:
@ -71,7 +71,7 @@ export class UserAuthController {
|
||||
return {
|
||||
statusCode: HttpStatus.OK,
|
||||
data: {
|
||||
otp: otpCode,
|
||||
...otpCode,
|
||||
},
|
||||
message: 'Otp Send Successfully',
|
||||
};
|
||||
|
@ -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> {
|
||||
|
Reference in New Issue
Block a user