2022-04-26 10:29:28 -07:00
import React , { useState , useEffect } from 'react' ;
import { useIntl , defineMessages , FormattedMessage } from 'react-intl' ;
import { useHistory } from 'react-router-dom' ;
import { fetchBackupCodes } from 'soapbox/actions/mfa' ;
import snackbar from 'soapbox/actions/snackbar' ;
2022-05-30 11:23:55 -07:00
import { Button , FormActions , Spinner , Stack , Text } from 'soapbox/components/ui' ;
2022-04-26 10:29:28 -07:00
import { useAppDispatch } from 'soapbox/hooks' ;
2022-07-06 14:25:19 -07:00
const messages = defineMessages ( {
2022-04-26 10:29:28 -07:00
mfaCancelButton : { id : 'column.mfa_cancel' , defaultMessage : 'Cancel' } ,
mfaSetupButton : { id : 'column.mfa_setup' , defaultMessage : 'Proceed to Setup' } ,
codesFail : { id : 'security.codes.fail' , defaultMessage : 'Failed to fetch backup codes' } ,
} ) ;
interface IEnableOtpForm {
displayOtpForm : boolean ,
handleSetupProceedClick : ( event : React.MouseEvent ) = > void ,
}
const EnableOtpForm : React.FC < IEnableOtpForm > = ( { displayOtpForm , handleSetupProceedClick } ) = > {
const dispatch = useAppDispatch ( ) ;
const intl = useIntl ( ) ;
const history = useHistory ( ) ;
const [ backupCodes , setBackupCodes ] = useState < Array < string > > ( [ ] ) ;
useEffect ( ( ) = > {
dispatch ( fetchBackupCodes ( ) ) . then ( ( { codes : backupCodes } : { codes : Array < string > } ) = > {
setBackupCodes ( backupCodes ) ;
} )
. catch ( ( ) = > {
dispatch ( snackbar . error ( intl . formatMessage ( messages . codesFail ) ) ) ;
} ) ;
} , [ ] ) ;
return (
< Stack space = { 4 } >
< Stack space = { 2 } >
< Text theme = 'muted' >
< FormattedMessage id = 'mfa.setup_warning' defaultMessage = "Write these codes down or save them somewhere secure - otherwise you won't see them again. If you lose access to your 2FA app and recovery codes you'll be locked out of your account." / >
< / Text >
2022-05-04 07:50:53 -07:00
< div className = 'bg-gray-100 dark:bg-slate-900/50 rounded-lg p-4' >
2022-04-26 10:29:28 -07:00
< Stack space = { 3 } >
< Text weight = 'medium' align = 'center' >
< FormattedMessage id = 'mfa.setup_recoverycodes' defaultMessage = 'Recovery codes' / >
< / Text >
{ backupCodes . length > 0 ? (
< div className = 'grid gap-3 grid-cols-2 rounded-lg text-center' >
{ backupCodes . map ( ( code , i ) = > (
< Text key = { i } theme = 'muted' size = 'sm' >
{ code }
< / Text >
) ) }
< / div >
) : (
< Spinner / >
) }
< / Stack >
< / div >
< / Stack >
{ ! displayOtpForm && (
< FormActions >
< Button
theme = 'ghost'
text = { intl . formatMessage ( messages . mfaCancelButton ) }
onClick = { ( ) = > history . push ( '../auth/edit' ) }
/ >
{ backupCodes . length > 0 && (
< Button
theme = 'primary'
text = { intl . formatMessage ( messages . mfaSetupButton ) }
onClick = { handleSetupProceedClick }
/ >
) }
< / FormActions >
) }
< / Stack >
) ;
} ;
2022-05-04 07:50:53 -07:00
export default EnableOtpForm ;