From 16df6945b7564f88bc9916f9149b68f550b474c8 Mon Sep 17 00:00:00 2001 From: Firman Ramdhani <33869609+firmanramdhani@users.noreply.github.com> Date: Tue, 10 Jun 2025 15:17:35 +0700 Subject: [PATCH] feat: fix validation generate otp --- src/core/helpers/otp/otp-service.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/core/helpers/otp/otp-service.ts b/src/core/helpers/otp/otp-service.ts index 0ba5e26..0b4e3e1 100644 --- a/src/core/helpers/otp/otp-service.ts +++ b/src/core/helpers/otp/otp-service.ts @@ -38,10 +38,14 @@ export class OtpService { return Object.values(counts).some((count) => count > 2); } - private hasMatchLength(str: string) { + private hasNoMatchLength(str: string) { return str.length !== this.otpLength; } + private hasStartWithZero(str: string) { + return str.split('')[0] === '0'; + } + public generateSecureOTP(): string { let otp: string; @@ -50,11 +54,12 @@ export class OtpService { Math.floor(Math.random() * 10).toString(), ).join(''); } while ( - this.hasMatchLength(otp) || + this.hasNoMatchLength(otp) || this.hasSequentialDigits(otp) || this.hasRepeatedDigits(otp) || this.isPalindrome(otp) || - this.hasPartiallyRepeatedDigits(otp) + this.hasPartiallyRepeatedDigits(otp) || + this.hasStartWithZero(otp) ); return otp; }