Skip to content

Commit

Permalink
fix: interval reset
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelpeixe committed Dec 14, 2023
1 parent dd58d14 commit 26e2e6f
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions assets/reader-activation/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,33 +228,33 @@ window.newspackRAS.push( function ( readerActivation ) {
} );
} );

let clearOtpTimerInterval;
function updateOTPTimer() {
if ( clearOtpTimerInterval ) {
clearOtpTimerInterval();
let otpTimerInterval;
function resetOTPTimer() {
if ( otpTimerInterval ) {
clearInterval( otpTimerInterval );
}
if ( ! resendCodeButton ) {
return;
}
const otpTimer = localStorage.getItem( STORAGE_OTP_TIMER );
if ( otpTimer ) {
clearOtpTimerInterval = setInterval( () => {
otpTimerInterval = setInterval( () => {
const timer = Math.floor( Date.now() / 1000 ) - parseInt( otpTimer );
console.log( timer );
if ( timer < 60 ) {
resendCodeButton.textContent = `Resend code in ${ 60 - timer } seconds`;
resendCodeButton.disabled = true;
} else {
resendCodeButton.textContent = 'Resend code';
resendCodeButton.disabled = false;
localStorage.removeItem( STORAGE_OTP_TIMER );
clearInterval( otpTimerInterval );
}
}, 1000 );
}
}

if ( resendCodeButton ) {
updateOTPTimer();
resetOTPTimer();
resendCodeButton.addEventListener( 'click', function ( ev ) {
ev.preventDefault();
const body = new FormData();
Expand Down Expand Up @@ -289,7 +289,7 @@ window.newspackRAS.push( function ( readerActivation ) {
console.log( { e } );
} )
.finally( () => {
updateOTPTimer();
resetOTPTimer();
} );
} );
} );
Expand Down Expand Up @@ -485,7 +485,6 @@ window.newspackRAS.push( function ( readerActivation ) {
form.endLoginFlow( data.message, 400 );
} );
} else {
console.log( { body } );
fetch( form.getAttribute( 'action' ) || window.location.pathname, {
method: 'POST',
headers: {
Expand Down Expand Up @@ -515,7 +514,7 @@ window.newspackRAS.push( function ( readerActivation ) {
if ( otpHash && [ 'register', 'link' ].includes( action ) ) {
// Set OTP rate-limit timer
localStorage.setItem( STORAGE_OTP_TIMER, Math.floor( Date.now() / 1000 ) );
updateOTPTimer();
resetOTPTimer();
if ( status === 200 ) {
setFormAction( 'otp' );
}
Expand Down

0 comments on commit 26e2e6f

Please sign in to comment.