A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
// SPDX-License-Identifier: AGPL-3.0-or-later
import {MFA_CODE_DIGIT_COUNT} from '@app/features/app/config/I18nDisplayConstants';
import FormField from '@app/features/auth/flow/AuthFormField';
import styles from '@app/features/auth/flow/MfaScreen.module.css';
import {useAuthPresentation} from '@app/features/auth/flow/useAuthPresentation';
import {type StepTitleChangeHandler, useReportStepTitle} from '@app/features/auth/flow/useReportStepTitle';
import {useMfaController} from '@app/features/auth/hooks/useLoginFlow';
import type {LoginSuccessPayload, MfaChallenge} from '@app/features/auth/state/AuthFlow';
import {AuthCardVariant} from '@app/features/auth/state/AuthLayoutContext';
import {
AUTHENTICATOR_CODE_DESCRIPTOR,
BACK_TO_SIGN_IN_DESCRIPTOR,
BACKUP_CODE_DESCRIPTOR,
SIGN_IN_DESCRIPTOR,
TWO_FACTOR_AUTHENTICATION_DESCRIPTOR,
} from '@app/features/i18n/utils/CommonMessageDescriptors';
import {Button} from '@app/features/ui/button/Button';
import {msg} from '@lingui/core/macro';
import {useLingui} from '@lingui/react/macro';
const AUTHENTICATION_CODE_PLACEHOLDER_DESCRIPTOR = msg({
message: '6-digit code',
comment: 'Placeholder for the MFA login one-time password field.',
});
const SECURITY_KEY_OR_PASSKEY_DESCRIPTOR = msg({
message: 'Security key / passkey',
comment: 'MFA action button for authenticating with a hardware security key or passkey.',
});
const MFA_CODE_INSTRUCTIONS_DESCRIPTOR = msg({
message: 'Enter the {digitCount}-digit code from your authenticator app or a backup code.',
comment: 'MFA code entry instructions. digitCount is the expected authenticator-code length.',
});
const TRY_SECURITY_KEY_INSTEAD_DESCRIPTOR = msg({
message: 'Try security key / passkey instead',
comment: 'Secondary MFA action that switches from code entry to passkey or security-key authentication.',
});
const MFA_BACKUP_CODE_INSTRUCTIONS_DESCRIPTOR = msg({
message: "Enter one of your backup codes if you can't use your passkey.",
comment: 'MFA code entry instructions shown when the account uses passkeys as its second factor.',
});
interface MfaScreenProps {
challenge: MfaChallenge;
inviteCode?: string;
onSuccess: (payload: LoginSuccessPayload) => Promise<void> | void;
onCancel: () => void;
onStepTitleChange?: StepTitleChangeHandler | null;
}
const MfaScreen = ({challenge, inviteCode, onSuccess, onCancel, onStepTitleChange = null}: MfaScreenProps) => {
const {i18n} = useLingui();
const title = i18n._(TWO_FACTOR_AUTHENTICATION_DESCRIPTOR);
useReportStepTitle(onStepTitleChange, title);
const {form, isLoading, fieldErrors, handleWebAuthn, isWebAuthnLoading, supports} = useMfaController({
ticket: challenge.ticket,
methods: {totp: challenge.totp, webauthn: challenge.webauthn, backupCodes: challenge.backupCodes},
inviteCode,
onLoginSuccess: onSuccess,
});
useAuthPresentation({variant: AuthCardVariant.COMPACT});
const showCodeForm = supports.totp || supports.backupCodes;
const showWebAuthn = supports.webauthn;
const backupCodeOnly = !supports.totp;
const isCodePrimary = supports.totp || !showWebAuthn;
return (
<div className={styles.container} data-flx="auth.flow.mfa-screen.container--2">
{onStepTitleChange == null ? (
<h1 className={styles.title} data-flx="auth.flow.mfa-screen.title">
{title}
</h1>
) : null}
{showCodeForm && (
<p className={styles.description} data-flx="auth.flow.mfa-screen.description">
{backupCodeOnly
? i18n._(MFA_BACKUP_CODE_INSTRUCTIONS_DESCRIPTOR)
: i18n._(MFA_CODE_INSTRUCTIONS_DESCRIPTOR, {digitCount: MFA_CODE_DIGIT_COUNT})}
</p>
)}
{showCodeForm && (
<form
className={styles.form}
onSubmit={form.handleSubmit}
autoComplete="on"
name="mfa"
data-flx="auth.flow.mfa-screen.form.submit"
>
<FormField
id="totp"
name="totp"
type="text"
autoComplete="one-time-code"
autoCapitalize="none"
autoCorrect="off"
enterKeyHint="done"
inputMode="text"
spellCheck={false}
autoFocus={isCodePrimary}
data-step-focus={isCodePrimary ? 'true' : undefined}
required
placeholder={i18n._(backupCodeOnly ? BACKUP_CODE_DESCRIPTOR : AUTHENTICATION_CODE_PLACEHOLDER_DESCRIPTOR)}
label={i18n._(backupCodeOnly ? BACKUP_CODE_DESCRIPTOR : AUTHENTICATOR_CODE_DESCRIPTOR)}
value={form.getValue('code')}
onChange={(value) => form.setValue('code', value)}
error={form.getError('code') || fieldErrors?.get('code') || fieldErrors?.get('ticket')}
data-flx="auth.flow.mfa-screen.form-field.set-value.text"
/>
<Button
type="submit"
fitContainer
disabled={isLoading || form.isSubmitting}
data-flx="auth.flow.mfa-screen.button.submit"
>
{i18n._(SIGN_IN_DESCRIPTOR)}
</Button>
</form>
)}
{showWebAuthn && (
<div className={styles.webauthnSection} data-flx="auth.flow.mfa-screen.webauthn-section">
<Button
type="button"
fitContainer
variant={isCodePrimary ? 'secondary' : 'primary'}
onClick={handleWebAuthn}
disabled={isWebAuthnLoading}
autoFocus={!isCodePrimary}
data-step-focus={isCodePrimary ? undefined : 'true'}
data-flx="auth.flow.mfa-screen.button.web-authn"
>
{i18n._(isCodePrimary ? TRY_SECURITY_KEY_INSTEAD_DESCRIPTOR : SECURITY_KEY_OR_PASSKEY_DESCRIPTOR)}
</Button>
</div>
)}
<div className={styles.footerButtons} data-flx="auth.flow.mfa-screen.footer-buttons">
<Button
type="button"
variant="secondary"
onClick={onCancel}
className={styles.footerButton}
data-flx="auth.flow.mfa-screen.footer-button.cancel"
>
{i18n._(BACK_TO_SIGN_IN_DESCRIPTOR)}
</Button>
</div>
</div>
);
};
export default MfaScreen;