Skip to content

Commit 0b9d4c0

Browse files
committed
feat: enable dynamic digit range for OTP generator
1 parent 410f4f3 commit 0b9d4c0

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

src/app/modules/auth/auth.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ const forgetPasswordToDB = async (email: string) => {
8989
}
9090

9191
//send mail
92-
const otp = generateOTP();
92+
const otp = generateOTP(6);
9393
const value = {
9494
otp,
9595
email: isExistUser.email,

src/app/modules/user/user.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const createUserToDB = async (payload: Partial<IUser>): Promise<IUser> => {
1818
}
1919

2020
//send email
21-
const otp = generateOTP();
21+
const otp = generateOTP(6);
2222
const values = {
2323
name: createUser.name,
2424
otp: otp,

src/util/generateOTP.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
const generateOTP = () => {
2-
return Math.floor(Math.random() * (9999 - 1000 + 1) + 1000);
1+
const generateOTP = (digitLength: number) => {
2+
const min = Math.pow(10, digitLength - 1);
3+
const max = Math.pow(10, digitLength) - 1;
4+
5+
return Math.floor(Math.random() * (max - min + 1) + min);
36
};
47

5-
export default generateOTP;
8+
export default generateOTP;

0 commit comments

Comments
 (0)