import QRCode from 'qrcode.react'; import React, { useCallback, useEffect, useState } from 'react'; import { useIntl, FormattedMessage, defineMessages } from 'react-intl'; import { useHistory } from 'react-router-dom'; import { setupMfa, confirmMfa, } from 'soapbox/actions/mfa'; import snackbar from 'soapbox/actions/snackbar'; import { Button, Form, FormActions, FormGroup, Input, Stack, Text } from 'soapbox/components/ui'; import { useAppDispatch } from 'soapbox/hooks'; const messages = defineMessages({ mfaCancelButton: { id: 'column.mfa_cancel', defaultMessage: 'Cancel' }, mfaSetupConfirmButton: { id: 'column.mfa_confirm_button', defaultMessage: 'Confirm' }, confirmFail: { id: 'security.confirm.fail', defaultMessage: 'Incorrect code or password. Try again.' }, qrFail: { id: 'security.qr.fail', defaultMessage: 'Failed to fetch setup key' }, mfaConfirmSuccess: { id: 'mfa.confirm.success_message', defaultMessage: 'MFA confirmed' }, codePlaceholder: { id: 'mfa.mfa_setup.code_placeholder', defaultMessage: 'Code' }, passwordPlaceholder: { id: 'mfa.mfa_setup.password_placeholder', defaultMessage: 'Password' }, }); const OtpConfirmForm: React.FC = () => { const intl = useIntl(); const history = useHistory(); const dispatch = useAppDispatch(); const [state, setState] = useState<{password: string, isLoading: boolean, code: string, qrCodeURI: string, confirmKey: string}>({ password: '', isLoading: false, code: '', qrCodeURI: '', confirmKey: '', }); useEffect(() => { dispatch(setupMfa('totp')).then((data: any) => { setState((prevState) => ({ ...prevState, qrCodeURI: data.provisioning_uri, confirmKey: data.key })); }).catch(() => { dispatch(snackbar.error(intl.formatMessage(messages.qrFail))); }); }, []); const handleInputChange = useCallback((event) => { event.persist(); setState((prevState) => ({ ...prevState, [event.target.name]: event.target.value })); }, []); const handleSubmit = (e: React.FormEvent) => { setState((prevState) => ({ ...prevState, isLoading: true })); dispatch(confirmMfa('totp', state.code, state.password) as any).then((r: any) => { dispatch(snackbar.success(intl.formatMessage(messages.mfaConfirmSuccess))); history.push('../auth/edit'); }).catch(() => { dispatch(snackbar.error(intl.formatMessage(messages.confirmFail))); setState((prevState) => ({ ...prevState, isLoading: false })); }); e.preventDefault(); }; return (
1. {state.confirmKey} 2. } > } >