Files
quicklanch-server/src/app/utils/otpGenerator.ts
T

30 lines
839 B
TypeScript
Raw Normal View History

2026-04-02 21:27:09 +06:00
export const otpGenerator = (): string => {
const upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const lower = "abcdefghijklmnopqrstuvwxyz";
const numbers = "0123456789";
const symbols = "@#$%&*!?";
const allChars = upper + lower + numbers + symbols;
let otp = "";
// Ensure at least one character from each set
otp += upper[Math.floor(Math.random() * upper.length)];
otp += lower[Math.floor(Math.random() * lower.length)];
otp += numbers[Math.floor(Math.random() * numbers.length)];
otp += symbols[Math.floor(Math.random() * symbols.length)];
// Fill the rest randomly
for (let i = otp.length; i < 6; i++) {
otp += allChars[Math.floor(Math.random() * allChars.length)];
}
// Shuffle to remove predictable order
otp = otp
.split("")
.sort(() => Math.random() - 0.5)
.join("");
return otp;
};