mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 12:34:54 +00:00
fixed forgot password
This commit is contained in:
@ -76,11 +76,14 @@ export class UserAuthController {
|
|||||||
|
|
||||||
@Post('user/forget-password')
|
@Post('user/forget-password')
|
||||||
async forgetPassword(@Body() forgetPasswordDto: ForgetPasswordDto) {
|
async forgetPassword(@Body() forgetPasswordDto: ForgetPasswordDto) {
|
||||||
const otpResult = await this.userAuthService.verifyOTP({
|
const otpResult = await this.userAuthService.verifyOTP(
|
||||||
|
{
|
||||||
otpCode: forgetPasswordDto.otpCode,
|
otpCode: forgetPasswordDto.otpCode,
|
||||||
email: forgetPasswordDto.email,
|
email: forgetPasswordDto.email,
|
||||||
type: OtpType.PASSWORD,
|
type: OtpType.PASSWORD,
|
||||||
});
|
},
|
||||||
|
true,
|
||||||
|
);
|
||||||
if (otpResult) {
|
if (otpResult) {
|
||||||
await this.userAuthService.forgetPassword(forgetPasswordDto);
|
await this.userAuthService.forgetPassword(forgetPasswordDto);
|
||||||
return {
|
return {
|
||||||
@ -89,11 +92,11 @@ export class UserAuthController {
|
|||||||
message: 'Password changed successfully',
|
message: 'Password changed successfully',
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
throw new BadRequestException({
|
throw new BadRequestException({
|
||||||
statusCode: HttpStatus.BAD_REQUEST,
|
statusCode: HttpStatus.BAD_REQUEST,
|
||||||
data: {},
|
data: {},
|
||||||
message: 'Otp is incorrect',
|
message: 'Otp is incorrect',
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
|
|||||||
@ -210,7 +210,7 @@ export class UserAuthService {
|
|||||||
}
|
}
|
||||||
const otpCode = Math.floor(100000 + Math.random() * 900000).toString();
|
const otpCode = Math.floor(100000 + Math.random() * 900000).toString();
|
||||||
const expiryTime = new Date();
|
const expiryTime = new Date();
|
||||||
expiryTime.setMinutes(expiryTime.getMinutes() + 1);
|
expiryTime.setMinutes(expiryTime.getMinutes() + 10);
|
||||||
await this.otpRepository.save({
|
await this.otpRepository.save({
|
||||||
email: data.email,
|
email: data.email,
|
||||||
otpCode,
|
otpCode,
|
||||||
@ -232,7 +232,10 @@ export class UserAuthService {
|
|||||||
return { otpCode, cooldown };
|
return { otpCode, cooldown };
|
||||||
}
|
}
|
||||||
|
|
||||||
async verifyOTP(data: VerifyOtpDto): Promise<boolean> {
|
async verifyOTP(
|
||||||
|
data: VerifyOtpDto,
|
||||||
|
fromNewPassword: boolean = false,
|
||||||
|
): Promise<boolean> {
|
||||||
const otp = await this.otpRepository.findOne({
|
const otp = await this.otpRepository.findOne({
|
||||||
where: { email: data.email, type: data.type },
|
where: { email: data.email, type: data.type },
|
||||||
});
|
});
|
||||||
@ -253,7 +256,7 @@ export class UserAuthService {
|
|||||||
throw new BadRequestException('You entered wrong otp');
|
throw new BadRequestException('You entered wrong otp');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (otp.expiryTime < new Date()) {
|
if (otp.expiryTime < new Date() || fromNewPassword) {
|
||||||
await this.otpRepository.delete(otp.uuid);
|
await this.otpRepository.delete(otp.uuid);
|
||||||
throw new BadRequestException('OTP expired');
|
throw new BadRequestException('OTP expired');
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user