@@ -10,18 +10,22 @@ export class ResendOtp {
1010 submitButtonId = 'auth_submit' ,
1111 attemptsDisplayId = 'remaining-attempts' ,
1212 codeInputId = 'auth_code' ,
13- maxAttempts = 5
13+ maxAttempts = 5 ,
1414 } = options ;
1515
1616 this . btn = button ;
1717 this . hint = document . getElementById ( hintId ) ;
1818 this . submitBtn = document . getElementById ( submitButtonId ) ;
1919 this . attemptsDisplay = document . getElementById ( attemptsDisplayId ) ;
2020 this . codeInput = document . getElementById ( codeInputId ) ;
21- this . maxAttempts = maxAttempts ;
2221
2322 this . cooldownSeconds = Number ( button . dataset . cooldown || 60 ) ;
23+ this . maxAttempts = maxAttempts ;
2424 this . url = button . dataset . url ;
25+ this . textDefault = button . dataset . textDefault ;
26+ this . textSending = button . dataset . textSending ;
27+ this . textError = button . dataset . textError ;
28+ this . textCountdown = button . dataset . textCountdown ;
2529 this . storageKey = `otp_resend_until_${ this . url } ` ;
2630
2731 this . timer = null ;
@@ -39,17 +43,21 @@ export class ResendOtp {
3943 }
4044
4145 async resend ( ) {
42- console . log ( 'Resend OTP code requested' ) ;
4346 if ( this . btn . disabled ) return ;
4447
45- this . lock ( 'Sending…' ) ;
48+ this . lock ( this . textSending ) ;
4649
4750 try {
48- await fetch ( this . url , {
51+ const response = await fetch ( this . url , {
4952 method : 'POST' ,
5053 headers : { 'Content-Type' : 'application/json' }
5154 } ) ;
5255
56+ if ( ! response . ok ) {
57+ this . unlock ( ) ;
58+ return ;
59+ }
60+
5361 const until = Date . now ( ) + this . cooldownSeconds * 1000 ;
5462 this . storeUntil ( until ) ;
5563 this . startCooldown ( until ) ;
@@ -59,7 +67,7 @@ export class ResendOtp {
5967 } catch ( e ) {
6068 console . error ( e ) ;
6169 this . unlock ( ) ;
62- this . setHint ( 'Could not resend code.' ) ;
70+ this . setHint ( this . textError ) ;
6371 }
6472 }
6573
@@ -90,7 +98,6 @@ export class ResendOtp {
9098
9199 startCooldown ( until ) {
92100 clearInterval ( this . timer ) ;
93- console . log ( `Starting OTP resend cooldown until ${ new Date ( until ) . toISOString ( ) } ` ) ;
94101 const tick = ( ) => {
95102 const remaining = Math . ceil ( ( until - Date . now ( ) ) / 1000 ) ;
96103
@@ -99,7 +106,7 @@ export class ResendOtp {
99106 this . unlock ( ) ;
100107 clearInterval ( this . timer ) ;
101108 } else {
102- this . lock ( `Resend in ${ remaining } s` ) ;
109+ this . lock ( this . textCountdown . replace ( '%d' , remaining ) ) ;
103110 }
104111 } ;
105112
@@ -114,7 +121,7 @@ export class ResendOtp {
114121
115122 unlock ( ) {
116123 this . btn . disabled = false ;
117- this . btn . textContent = 'Resend code' ;
124+ this . btn . textContent = this . textDefault ;
118125 }
119126
120127 setHint ( text ) {
@@ -134,7 +141,6 @@ export class ResendOtp {
134141 }
135142
136143 restoreOnRefresh ( ) {
137- console . log ( 'Restoring OTP resend cooldown if needed' ) ;
138144 const until = this . getStoredUntil ( ) ;
139145 if ( until && until > Date . now ( ) ) {
140146 this . startCooldown ( until ) ;
0 commit comments