Skip to content

Commit

Permalink
Auto generate codes on TOTP success
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwoodnz committed Aug 28, 2024
1 parent 985f7a5 commit c79844a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
13 changes: 13 additions & 0 deletions settings/src/components/backup-codes.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,18 @@ import DownloadButton from './download-button';
export default function BackupCodes( { onSuccess = () => {} } ) {
const {
user: { hasPrimaryProvider },
shouldAutoGenerateBackupCodes,
} = useContext( GlobalContext );
const [ generating, setGenerating ] = useState( false );

useEffect( () => {
if ( shouldAutoGenerateBackupCodes ) {
setGenerating( true );
}
// This should only run once on mount.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [] );

// Prevent users from accessing directly through the URL.
if ( ! hasPrimaryProvider ) {
return (
Expand Down Expand Up @@ -61,6 +70,7 @@ function Setup( { setGenerating, onSuccess } ) {
setError,
error,
setBackupCodesVerified,
setShouldAutoGenerateBackupCodes,
} = useContext( GlobalContext );
const [ backupCodes, setBackupCodes ] = useState( [] );
const [ hasPrinted, setHasPrinted ] = useState( false );
Expand All @@ -84,6 +94,7 @@ function Setup( { setGenerating, onSuccess } ) {
} );

setBackupCodes( response.codes );
setShouldAutoGenerateBackupCodes( false );

// Update the Account Status screen in case they click `Back` without verifying the codes, but
// don't redirect to the Manage screen yet. This is mainly due to the side-effects of
Expand All @@ -96,6 +107,8 @@ function Setup( { setGenerating, onSuccess } ) {
};

generateCodes();
// This should only run once on mount.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [] );

// Finish the setup process.
Expand Down
8 changes: 7 additions & 1 deletion settings/src/components/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ import { GlobalContext } from '../script';
* Render the correct component based on the URL.
*/
export default function Settings() {
const { backupCodesEnabled, navigateToScreen, screen } = useContext( GlobalContext );
const {
user: { backupCodesEnabled },
navigateToScreen,
screen,
setShouldAutoGenerateBackupCodes,
} = useContext( GlobalContext );

// The index is the URL slug and the value is the React component.
const components = {
Expand All @@ -31,6 +36,7 @@ export default function Settings() {
<TOTP
onSuccess={ () => {
if ( ! backupCodesEnabled ) {
setShouldAutoGenerateBackupCodes( true );
navigateToScreen( 'backup-codes' );
} else {
navigateToScreen( 'home' );
Expand Down
3 changes: 3 additions & 0 deletions settings/src/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ function Main( { userId, isOnboarding } ) {
const [ globalNotice, setGlobalNotice ] = useState( '' );
const [ error, setError ] = useState( '' );
const [ backupCodesVerified, setBackupCodesVerified ] = useState( true );
const [ shouldAutoGenerateBackupCodes, setShouldAutoGenerateBackupCodes ] = useState( false );

let currentUrl = new URL( document.location.href );
const initialScreen = currentUrl.searchParams.get( 'screen' );
Expand Down Expand Up @@ -150,6 +151,8 @@ function Main( { userId, isOnboarding } ) {
setBackupCodesVerified,
setScreen,
screen,
shouldAutoGenerateBackupCodes,
setShouldAutoGenerateBackupCodes,
} }
>
<GlobalNotice notice={ globalNotice } setNotice={ setGlobalNotice } />
Expand Down

0 comments on commit c79844a

Please sign in to comment.