/* eslint-disable react/jsx-props-no-spreading */ import React, { useState } from 'react'; import ReactCodeInput from 'react-code-input'; import { useRouter } from 'next/router'; import { useTranslation } from 'next-i18next'; import attemptLoginMfa from '@app/components/utilities/attemptLoginMfa'; import { getTranslatedStaticProps } from '@app/components/utilities/withTranslateProps'; import { useSendMfaToken } from '@app/hooks/api/auth'; import Button from '../basic/buttons/Button'; import Error from '../basic/Error'; // The style for the verification code input const props = { inputStyle: { fontFamily: 'monospace', margin: '4px', MozAppearance: 'textfield', width: '55px', borderRadius: '5px', fontSize: '24px', height: '55px', paddingLeft: '7', backgroundColor: '#0d1117', color: 'white', border: '1px solid #2d2f33', textAlign: 'center', outlineColor: '#8ca542', borderColor: '#2d2f33' } } as const; interface VerifyMfaTokenError { response: { data: { context: { code: string; triesLeft: number; } }, status: number; } } /** * 2nd step of login - users enter their MFA code * @param {Object} obj * @param {String} obj.email - email of user * @param {String} obj.password - password of user * @param {Function} obj.setStep - function to set the login flow step * @returns */ export default function MFAStep({ email, password }: { email: string; password: string; }): JSX.Element { const router = useRouter(); const [isLoading, setIsLoading] = useState(false); const [isLoadingResend, setIsLoadingResend] = useState(false); const [mfaCode, setMfaCode] = useState(''); const [triesLeft, setTriesLeft] = useState(undefined); const { t } = useTranslation(); const sendMfaToken = useSendMfaToken(); const handleLoginMfa = async () => { try { if (mfaCode.length !== 6) { return; } setIsLoading(true); const isLoginSuccessful = await attemptLoginMfa({ email, password, mfaToken: mfaCode }); if (isLoginSuccessful) { setIsLoading(false); router.push(`/dashboard/${localStorage.getItem('projectData.id')}`); } } catch (err) { const error = err as VerifyMfaTokenError; if (error?.response?.status === 500) { window.location.reload(); } else if (error?.response?.data?.context?.triesLeft) { setTriesLeft(error?.response?.data?.context?.triesLeft); if (error.response.data.context.triesLeft === 0) { window.location.reload(); } } setIsLoading(false); } } const handleResendMfaCode = async () => { try { setIsLoadingResend(true); await sendMfaToken.mutateAsync({ email }); setIsLoadingResend(false); } catch (err) { console.error(err); setIsLoadingResend(false); } } return (

{t('mfa:step2-message')}

{email}

{typeof triesLeft === 'number' && }
{t('mfa:step2-resend-alert')}

{t('mfa:step2-spam-alert')}

); } export const getStaticProps = getTranslatedStaticProps(['auth', 'mfa']);