44 */
55
66export class ResendOtp {
7- constructor ( button , { hintId = 'resend-hint' } = { } ) {
7+ constructor ( button , options = { } ) {
8+ const {
9+ hintId = 'resend-hint' ,
10+ submitButtonId = 'auth_submit' ,
11+ attemptsDisplayId = 'remaining-attempts' ,
12+ codeInputId = 'auth_code' ,
13+ maxAttempts = 5
14+ } = options ;
15+
816 this . btn = button ;
917 this . hint = document . getElementById ( hintId ) ;
18+ this . submitBtn = document . getElementById ( submitButtonId ) ;
19+ this . attemptsDisplay = document . getElementById ( attemptsDisplayId ) ;
20+ this . codeInput = document . getElementById ( codeInputId ) ;
21+ this . maxAttempts = maxAttempts ;
1022
1123 this . cooldownSeconds = Number ( button . dataset . cooldown || 60 ) ;
1224 this . url = button . dataset . url ;
1325 this . storageKey = `otp_resend_until_${ this . url } ` ;
1426
1527 this . timer = null ;
28+
29+ this . initAttemptsCheck ( ) ;
30+ }
31+
32+ initAttemptsCheck ( ) {
33+ if ( this . attemptsDisplay ) {
34+ const attempts = parseInt ( this . attemptsDisplay . textContent , 10 ) ;
35+ if ( attempts === 0 ) {
36+ this . disableSubmit ( ) ;
37+ }
38+ }
1639 }
1740
1841 async resend ( ) {
@@ -31,13 +54,40 @@ export class ResendOtp {
3154 this . storeUntil ( until ) ;
3255 this . startCooldown ( until ) ;
3356
57+ this . resetAttempts ( ) ;
58+
3459 } catch ( e ) {
3560 console . error ( e ) ;
3661 this . unlock ( ) ;
3762 this . setHint ( 'Could not resend code.' ) ;
3863 }
3964 }
4065
66+ resetAttempts ( ) {
67+ if ( this . attemptsDisplay ) {
68+ this . attemptsDisplay . textContent = this . maxAttempts ;
69+ }
70+ this . enableSubmit ( ) ;
71+ }
72+
73+ disableSubmit ( ) {
74+ if ( this . submitBtn ) {
75+ this . submitBtn . disabled = true ;
76+ }
77+ if ( this . codeInput ) {
78+ this . codeInput . disabled = true ;
79+ }
80+ }
81+
82+ enableSubmit ( ) {
83+ if ( this . submitBtn ) {
84+ this . submitBtn . disabled = false ;
85+ }
86+ if ( this . codeInput ) {
87+ this . codeInput . disabled = false ;
88+ }
89+ }
90+
4191 startCooldown ( until ) {
4292 clearInterval ( this . timer ) ;
4393 console . log ( `Starting OTP resend cooldown until ${ new Date ( until ) . toISOString ( ) } ` ) ;
@@ -83,7 +133,7 @@ export class ResendOtp {
83133 localStorage . removeItem ( this . storageKey ) ;
84134 }
85135
86- restoreIfNeeded ( ) {
136+ restoreOnRefresh ( ) {
87137 console . log ( 'Restoring OTP resend cooldown if needed' ) ;
88138 const until = this . getStoredUntil ( ) ;
89139 if ( until && until > Date . now ( ) ) {
0 commit comments