mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-16 02:36:19 +00:00
added cooldown with OTP
This commit is contained in:
@ -71,7 +71,7 @@ export class UserAuthController {
|
|||||||
return {
|
return {
|
||||||
statusCode: HttpStatus.OK,
|
statusCode: HttpStatus.OK,
|
||||||
data: {
|
data: {
|
||||||
otp: otpCode,
|
...otpCode,
|
||||||
},
|
},
|
||||||
message: 'Otp Send Successfully',
|
message: 'Otp Send Successfully',
|
||||||
};
|
};
|
||||||
|
@ -140,7 +140,10 @@ export class UserAuthService {
|
|||||||
return await this.userRepository.findOne({ where: { uuid: id } });
|
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();
|
const otpLimiter = new Date();
|
||||||
otpLimiter.setDate(
|
otpLimiter.setDate(
|
||||||
otpLimiter.getDate() - this.configService.get<number>('OTP_LIMITER'),
|
otpLimiter.getDate() - this.configService.get<number>('OTP_LIMITER'),
|
||||||
@ -178,7 +181,7 @@ export class UserAuthService {
|
|||||||
order: { createdAt: 'DESC' },
|
order: { createdAt: 'DESC' },
|
||||||
withDeleted: true,
|
withDeleted: true,
|
||||||
});
|
});
|
||||||
const cooldown = 30 * Math.pow(2, countOfOtp - 1);
|
let cooldown = 30 * Math.pow(2, countOfOtp - 1);
|
||||||
if (lastOtp) {
|
if (lastOtp) {
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
const timeSinceLastOtp = differenceInSeconds(now, lastOtp.createdAt);
|
const timeSinceLastOtp = differenceInSeconds(now, lastOtp.createdAt);
|
||||||
@ -201,10 +204,19 @@ export class UserAuthService {
|
|||||||
expiryTime,
|
expiryTime,
|
||||||
type: data.type,
|
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 subject = 'OTP send successfully';
|
||||||
const message = `Your OTP code is ${otpCode}`;
|
const message = `Your OTP code is ${otpCode}`;
|
||||||
this.emailService.sendEmail(data.email, subject, message);
|
this.emailService.sendEmail(data.email, subject, message);
|
||||||
return otpCode;
|
return { otpCode, cooldown };
|
||||||
}
|
}
|
||||||
|
|
||||||
async verifyOTP(data: VerifyOtpDto): Promise<boolean> {
|
async verifyOTP(data: VerifyOtpDto): Promise<boolean> {
|
||||||
|
Reference in New Issue
Block a user