Skip to content

Commit

Permalink
Require explicit action to create backup codes
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwoodnz committed Aug 14, 2024
1 parent fe04b98 commit f87ee1d
Showing 1 changed file with 26 additions and 33 deletions.
59 changes: 26 additions & 33 deletions settings/src/components/backup-codes.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ import DownloadButton from './download-button';
*/
export default function BackupCodes() {
const {
user: { backupCodesEnabled, hasPrimaryProvider, backupCodesRemaining },
backupCodesVerified,
user: { hasPrimaryProvider },
} = useContext( GlobalContext );
const [ regenerating, setRegenerating ] = useState( false );
const [ generating, setGenerating ] = useState( false );

// Prevent users from accessing directly through the URL.
if ( ! hasPrimaryProvider ) {
Expand All @@ -41,25 +40,20 @@ export default function BackupCodes() {
);
}

if (
! backupCodesEnabled ||
backupCodesRemaining === 0 ||
regenerating ||
! backupCodesVerified
) {
return <Setup setRegenerating={ setRegenerating } />;
}

return <Manage setRegenerating={ setRegenerating } />;
return generating ? (
<Setup setGenerating={ setGenerating } />
) : (
<Manage setGenerating={ setGenerating } />
);
}

/**
* Setup the Backup Codes provider.
*
* @param props
* @param props.setRegenerating
* @param props.setGenerating
*/
function Setup( { setRegenerating } ) {
function Setup( { setGenerating } ) {
const {
setGlobalNotice,
user: { userRecord },
Expand Down Expand Up @@ -110,17 +104,13 @@ function Setup( { setRegenerating } ) {
setBackupCodesVerified( true );
await refreshRecord( userRecord ); // This has the intended side-effect of redirecting to the Manage screen.
setGlobalNotice( 'Backup codes have been enabled.' );
setRegenerating( false );
setGenerating( false );
} );

return (
<>
<div className="wporg-2fa__screen-intro">
<p>
Backup codes let you access your account if your primary two-factor
authentication method is unavailable, like if your phone is lost or stolen. Each
code can only be used once.
</p>
<IntroText />

<p>Please print the codes and keep them in a safe place.</p>
</div>
Expand Down Expand Up @@ -201,33 +191,36 @@ function CodeList( { codes } ) {
);
}

const IntroText = () => (
<p>
Backup codes let you access your account if your primary two-factor authentication method is
unavailable, like if your phone is lost or stolen. Each code can only be used once.
</p>
);

/**
* Render the screen where users can manage Backup Codes.
*
* @param props
* @param props.setRegenerating
* @param props.setGenerating
*/
function Manage( { setRegenerating } ) {
function Manage( { setGenerating } ) {
const {
user: { backupCodesRemaining },
user: { backupCodesEnabled, backupCodesRemaining },
} = useContext( GlobalContext );

return (
<>
<div className="wporg-2fa__screen-intro">
<p>
Backup codes let you access your account if your primary two-factor
authentication method is unavailable, like if your phone is lost or stolen. Each
code can only be used once.
</p>
<IntroText />

{ backupCodesRemaining > 5 && (
{ backupCodesEnabled && backupCodesRemaining > 5 && (
<p>
You have <strong>{ backupCodesRemaining }</strong> backup codes remaining.
</p>
) }

{ backupCodesRemaining <= 5 && (
{ backupCodesEnabled && backupCodesRemaining <= 5 && (
<Notice status="warning" isDismissible={ false }>
<Icon icon={ warning } />
You only have <strong>{ backupCodesRemaining }</strong> backup codes
Expand All @@ -238,8 +231,8 @@ function Manage( { setRegenerating } ) {
) }
</div>

<Button isSecondary onClick={ () => setRegenerating( true ) }>
Generate new backup codes
<Button isSecondary onClick={ () => setGenerating( true ) }>
{ backupCodesEnabled ? 'Regenerate backup codes' : 'Generate backup codes' }
</Button>
</>
);
Expand Down

0 comments on commit f87ee1d

Please sign in to comment.