Files
MinglarBackendNestJS/src/common/utils/helper/resendOtpHelper.ts

63 lines
1.4 KiB
TypeScript
Raw Normal View History

2025-11-28 12:23:08 +05:30
import * as bcrypt from "bcryptjs";
import { OtpGenerator, OtpGeneratorSixDigit } from "./OtpGenerator";
import { encryptUserId } from "./CodeGenerator";
export interface OtpResult {
otp: string;
hashedOtp: string;
expiry: Date;
encryptedId: string;
}
export async function resendOtpHelper(
prisma: any,
userId: number,
emailPurpose: "Register" | "Login" | "ForgotPassword",
otpLength: 4 | 6 = 4,
expiryMinutes: number = 5
): Promise<OtpResult> {
// 1⃣ Deactivate previous OTPs
await prisma.userOtp.updateMany({
where: {
userXid: userId,
otpType: emailPurpose,
isActive: true,
},
data: {
isActive: false,
isVerified: true,
},
});
// 2⃣ Generate new OTP
const otp =
otpLength === 6
? OtpGeneratorSixDigit.generateOtp()
: OtpGenerator.generateOtp();
const hashedOtp = await bcrypt.hash(otp, 10);
const expiry = new Date(Date.now() + expiryMinutes * 60000);
const encryptedId = encryptUserId(userId.toString());
// 3⃣ Insert new OTP into table
await prisma.userOtp.create({
data: {
userXid: userId,
otpType: emailPurpose,
otpCode: hashedOtp,
expiresOn: expiry,
isVerified: false,
isActive: true,
},
});
// 4⃣ Return new OTP (email will use this)
return {
otp,
hashedOtp,
expiry,
encryptedId,
};
}