diff --git a/web/apps/auth/src/pages/auth.tsx b/web/apps/auth/src/pages/auth.tsx index 526489779..d750c5f7c 100644 --- a/web/apps/auth/src/pages/auth.tsx +++ b/web/apps/auth/src/pages/auth.tsx @@ -187,28 +187,21 @@ const CodeDisplay: React.FC = ({ code }) => { useEffect(() => { // Generate to set the initial otp and nextOTP on component mount. regen(); - const codeType = code.type; - const codePeriodInMs = code.period * 1000; - const timeToNextCode = - codePeriodInMs - (new Date().getTime() % codePeriodInMs); - const interval = null; + + const periodMs = code.period * 1000; + const timeToNextCode = periodMs - (Date.now() % periodMs); + + let interval: ReturnType | undefined; // Wait until we are at the start of the next code period, and then // start the interval loop. setTimeout(() => { // We need to call regen() once before the interval loop to set the // initial otp and nextOTP. regen(); - codeType.toLowerCase() === "totp" || - codeType.toLowerCase() === "hotp" - ? setInterval(() => { - regen(); - }, codePeriodInMs) - : null; + interval = setInterval(() => regen, periodMs); }, timeToNextCode); - return () => { - if (interval) clearInterval(interval); - }; + return () => interval && clearInterval(interval); }, [code]); return ( @@ -346,7 +339,7 @@ const TimerProgress: React.FC = ({ period }) => { useEffect(() => { const advance = () => { - const timeRemaining = us - ((new Date().getTime() * 1000) % us); + const timeRemaining = us - ((Date.now() * 1000) % us); setProgress(timeRemaining / us); };