Skip to content

Commit

Permalink
Disable backup codes UI entirely if another provider isn't enabled
Browse files Browse the repository at this point in the history
- Don't show link to backup codes screen
- Navigate to account screen if screen is accessed directly
  • Loading branch information
adamwoodnz committed May 18, 2023
1 parent 8d5f4f8 commit a719132
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 24 deletions.
18 changes: 10 additions & 8 deletions settings/src/components/account-status.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,16 @@ export default function AccountStatus() {
}
/>

<SettingStatusCard
screen="backup-codes"
status={ backupCodesStatus }
headerText="Two-Factor Backup Codes"
bodyText={ `You have ${
'enabled' === backupCodesStatus ? '' : 'not'
} verified your backup codes for two-factor authentication.` }
/>
{ totpStatus === 'enabled' && (
<SettingStatusCard
screen="backup-codes"
status={ backupCodesStatus }
headerText="Two-Factor Backup Codes"
bodyText={ `You have ${
'enabled' === backupCodesStatus ? '' : 'not'
} verified your backup codes for two-factor authentication.` }
/>
) }
</div>
);
}
Expand Down
31 changes: 20 additions & 11 deletions settings/src/components/backup-codes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,28 @@ import { refreshRecord } from '../utilities';
* Setup and manage backup codes.
*/
export default function BackupCodes() {
const { userRecord } = useContext( GlobalContext );
const {
userRecord: {
record: { '2fa_available_providers': availableProviders },
},
navigateToScreen,
} = useContext( GlobalContext );
const [ regenerating, setRegenerating ] = useState( false );
const backupCodesProviderAvailable = availableProviders.includes( 'Two_Factor_Backup_Codes' );
const onlyProviderIsBackupCodes =
availableProviders.length === 1 && backupCodesProviderAvailable;

const backupCodesStatus = userRecord.record[ '2fa_available_providers' ].includes(
'Two_Factor_Backup_Codes'
)
? 'enabled'
: 'disabled';

if ( 'enabled' === backupCodesStatus && ! regenerating ) {
return <Manage setRegenerating={ setRegenerating } />;
}
return <Setup setRegenerating={ setRegenerating } />;
useEffect( () => {
if ( onlyProviderIsBackupCodes ) {
navigateToScreen( 'account-status' );
}
}, [ onlyProviderIsBackupCodes ] );

return backupCodesProviderAvailable && ! regenerating ? (
<Manage setRegenerating={ setRegenerating } />
) : (
<Setup setRegenerating={ setRegenerating } />
);
}

/**
Expand Down
22 changes: 17 additions & 5 deletions settings/src/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,8 @@ function Main( { userId } ) {
* This is used in conjunction with real links in order to preserve deep linking and other foundational
* behaviors that are broken otherwise.
*/
const clickScreenLink = useCallback(
( event, nextScreen ) => {
event.preventDefault();

const navigateToScreen = useCallback(
( nextScreen ) => {
// Reset to initial after navigating away from a page.
if ( hasEdits ) {
edit( record );
Expand All @@ -126,6 +124,18 @@ function Main( { userId } ) {
[ hasEdits ]
);

/**
* Handle clicks on screen links.
*
* @param event
* @param nextScreen
*/
const clickScreenLink = useCallback( ( event, nextScreen ) => {
event.preventDefault();

navigateToScreen( nextScreen );
}, [] );

if ( ! hasResolved ) {
return <Spinner />;
}
Expand Down Expand Up @@ -180,7 +190,9 @@ function Main( { userId } ) {
}

return (
<GlobalContext.Provider value={ { clickScreenLink, userRecord, setGlobalNotice } }>
<GlobalContext.Provider
value={ { clickScreenLink, userRecord, setGlobalNotice, navigateToScreen } }
>
<GlobalNotice notice={ globalNotice } setNotice={ setGlobalNotice } />
{ screenContent }
</GlobalContext.Provider>
Expand Down

0 comments on commit a719132

Please sign in to comment.