Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a wizard component for first time configurations. #286

Closed
wants to merge 33 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
f01a233
Try a setup wizard.
StevenDufresne Jul 15, 2024
8a4f4a4
More changes.
StevenDufresne Jul 16, 2024
c507d19
More changes
StevenDufresne Jul 17, 2024
2e1b1f4
Fix the linter.
StevenDufresne Jul 17, 2024
3f09cda
Clean up
StevenDufresne Jul 17, 2024
29290b1
Update account-status to be 'home'.
StevenDufresne Jul 18, 2024
5f4f8af
Add back empty line.
StevenDufresne Jul 18, 2024
5726e0c
Update copy.
StevenDufresne Aug 8, 2024
c5ebb5b
Add redirect logic.
StevenDufresne Aug 8, 2024
fc49452
Fix spacing.
StevenDufresne Aug 9, 2024
094a3ec
Improve css.
StevenDufresne Aug 9, 2024
1d3f9a2
Font show if they have a primary.
StevenDufresne Aug 9, 2024
a266649
We can't check via the component because it reloads.
StevenDufresne Aug 9, 2024
fbb0e53
Remove eslint disable line.
StevenDufresne Aug 9, 2024
0089358
Remove empty line.
StevenDufresne Aug 9, 2024
eecec3d
Clean up formatting.
StevenDufresne Aug 11, 2024
8461a28
Revert "Clean up formatting."
StevenDufresne Aug 11, 2024
fbd10e5
clean up formatting.
StevenDufresne Aug 11, 2024
2429c3e
Lowercase setup radial.
StevenDufresne Aug 12, 2024
f1440bc
Trap the keyboard focus within the window.
StevenDufresne Aug 12, 2024
f4d60c3
Update the padding-top for the wizard.
StevenDufresne Aug 12, 2024
d8125e3
Update the component name.
StevenDufresne Aug 12, 2024
b373122
Update settings/src/components/first-time/first-time.js
StevenDufresne Aug 12, 2024
ab6acb2
Prefer keys as the first option.
StevenDufresne Aug 12, 2024
ecbd3d3
Move progress indicator about window.
StevenDufresne Aug 12, 2024
4f60760
Default to 2fa view.
StevenDufresne Aug 12, 2024
730ee19
move the progress component.
StevenDufresne Aug 12, 2024
d4483d5
Update selected radial to be webauthn.
StevenDufresne Aug 12, 2024
dcde527
Fix react error with map key.
StevenDufresne Aug 12, 2024
221f181
Update radio list styles.
StevenDufresne Aug 12, 2024
52daa65
Update progress bar and titles.
StevenDufresne Aug 13, 2024
82d7913
Move congratulations to its own component.
StevenDufresne Aug 13, 2024
142b57d
Remove unsued button.
StevenDufresne Aug 13, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions settings/src/components/backup-codes.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@

/**
* Setup and manage backup codes.
*
* @param props
* @param props.onSuccess
*/
export default function BackupCodes() {
export default function BackupCodes( { onSuccess = () => {} } ) {
const {
user: { backupCodesEnabled, hasPrimaryProvider, backupCodesRemaining },
backupCodesVerified,
Expand All @@ -32,10 +35,7 @@
<Notice status="error" isDismissible={ false }>
<Icon icon={ cancelCircleFilled } />
Please
<ScreenLink
screen="account-status"
anchorText="enable a Two-Factor security key or app"
/>
<ScreenLink screen="home" anchorText="enable a Two-Factor security key or app" />
before enabling backup codes.
</Notice>
);
Expand All @@ -47,7 +47,7 @@
regenerating ||
! backupCodesVerified
) {
return <Setup setRegenerating={ setRegenerating } />;
return <Setup setRegenerating={ setRegenerating } onSuccess={ onSuccess } />;
}

return <Manage setRegenerating={ setRegenerating } />;
Expand All @@ -58,8 +58,9 @@
*
* @param props
* @param props.setRegenerating
* @param props.onSuccess
*/
function Setup( { setRegenerating } ) {
function Setup( { setRegenerating, onSuccess } ) {
const {
setGlobalNotice,
user: { userRecord },
Expand Down Expand Up @@ -101,16 +102,17 @@
};

generateCodes();
}, [] );

Check warning on line 105 in settings/src/components/backup-codes.js

View workflow job for this annotation

GitHub Actions / All

React Hook useEffect has missing dependencies: 'setBackupCodesVerified', 'setError', and 'userRecord'. Either include them or remove the dependency array

// Finish the setup process.
const handleFinished = useCallback( async () => {

Check warning on line 108 in settings/src/components/backup-codes.js

View workflow job for this annotation

GitHub Actions / All

React Hook useCallback does nothing when called with only one argument. Did you forget to pass an array of dependencies?
// TODO: Add try catch here after https://github.com/WordPress/wporg-two-factor/pull/187/files is merged.
// The codes have already been saved to usermeta, see `generateCodes()` above.
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 );
onSuccess();
} );

return (
Expand Down
6 changes: 6 additions & 0 deletions settings/src/components/backup-codes.scss
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
margin: 0;
}

@media (max-width: 600px) {
ol {
column-count: 1;
}
}

li::marker {
/* todo: a11y issues w/ contrast here? mockup calls for this to be lighter than the backup code
-- presumably so that users don't mistakenly think it's part of the code --
Expand Down
50 changes: 50 additions & 0 deletions settings/src/components/first-time/congratulations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* WordPress dependencies
*/
import { Button } from '@wordpress/components';

/**
* Check if the URL is valid. Make sure it stays on wordpress.org.
*
* @param url
* @return {boolean} Whether it's a valid URL.
*/
const isValidUrl = ( url ) => {
try {
const { hostname } = new URL( url );
return hostname.endsWith( 'wordpress.org' );
} catch ( exception ) {
return false;
}
};

export default function Congratulations() {
return (
<>
<p>
To ensure the highest level of security for your account, please remember to keep
your authentication methods up-to-date. We recommend configuring multiple
authentication methods to guarantee you always have access to your account.
</p>
<div className="wporg-2fa__submit-actions">
<Button
onClick={ () => {
const redirectTo = new URLSearchParams( window.location.search ).get(
'redirect_to'
);

if ( redirectTo && isValidUrl( redirectTo ) ) {
window.location.href = redirectTo;
} else {
window.location.href =
'//profiles.wordpress.org/me/profile/edit/group/3';
}
} }
isPrimary
>
Continue
</Button>
</div>
</>
);
}
156 changes: 156 additions & 0 deletions settings/src/components/first-time/first-time.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/**
* WordPress dependencies
*/
import { useEffect, useContext, useRef } from '@wordpress/element';

/**
* Internal dependencies
*/
import ScreenNavigation from '../screen-navigation';
import TOTP from '../totp';
import WebAuthn from '../webauthn/webauthn';
import BackupCodes from '../backup-codes';
import SetupProgressBar from './setup-progress-bar';
import Home from './home';
import Congratulations from './congratulations';
import WordPressLogo from './wordpress-logo';
import { GlobalContext } from '../../script';

/**
* Render the correct component based on the URL.
*/
export default function FirstTime() {
const modalRef = useRef( null );
const { navigateToScreen, screen } = useContext( GlobalContext );

// The index is the URL slug and the value is the React component.
const screens = {
home: {
stepIndex: 0,
title: 'Set up two-factor authentication',
component: (
<Home
onSelect={ ( val ) => {
navigateToScreen( val );
} }
/>
),
},
totp: {
stepIndex: 1,
title: 'Set up one-time password',
component: (
<TOTP
onSuccess={ () => {
navigateToScreen( 'backup-codes' );
} }
/>
),
},
webauthn: {
stepIndex: 1,
title: 'Set up security key',
component: (
<WebAuthn
onKeyAdd={ () => {
navigateToScreen( 'backup-codes' );
} }
/>
),
},
'backup-codes': {
stepIndex: 2,
title: 'Print backup codes',
component: (
<BackupCodes
onSuccess={ () => {
navigateToScreen( 'congratulations' );
} }
/>
),
},
congratulations: {
stepIndex: 3,
component: <Congratulations />,
},
};

// Lock the scroll when the modal is open, and trap tab navigation.
useEffect( () => {
const modal = modalRef.current;
const focusableElements = modal.querySelectorAll(
'a, button, input, textarea, select, [tabindex]:not([tabindex="-1"])'
);
const firstFocusableElement = focusableElements[ 0 ];
const lastFocusableElement = focusableElements[ focusableElements.length - 1 ];

const trapFocus = ( event ) => {
const isTabPressed = event.key === 'Tab' || event.keyCode === 9;
if ( ! isTabPressed ) {
return;
}

if ( event.shiftKey ) {
// eslint-disable-next-line @wordpress/no-global-active-element
if ( document.activeElement === firstFocusableElement ) {
lastFocusableElement.focus();
event.preventDefault();
}
return;
}

// eslint-disable-next-line @wordpress/no-global-active-element
if ( document.activeElement === lastFocusableElement ) {
firstFocusableElement.focus();
event.preventDefault();
}
};

modal.addEventListener( 'keydown', trapFocus );

document.querySelector( 'html' ).style.overflow = 'hidden';

// Focus the first focusable element in the modal when it opens
firstFocusableElement.focus();

return () => {
modal.removeEventListener( 'keydown', trapFocus );
document.querySelector( 'html' ).style.overflow = 'initial';
};
}, [ screen ] );

const currentStepIndex = screens[ screen ].stepIndex;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could do with a check here for the screen existing. Between switching from prod to sandbox I ended up on ?first-time=true&screen=account-status, resulting in an error. Shouldn't be possible normally of course but it highlighted an edge case.

let currentScreenComponent = null;

if ( 'congratulations' === screen ) {
currentScreenComponent = (
<ScreenNavigation screen={ screen } title={ 'Setup complete' } canNavigate={ false }>
{ screens[ screen ].component }
</ScreenNavigation>
);
} else {
currentScreenComponent = (
<>
<SetupProgressBar currentStepIndex={ currentStepIndex } stepCount={ 3 } />
<ScreenNavigation
screen={ screen }
title={ screens[ screen ].title }
canNavigate={ currentStepIndex !== 0 }
>
{ screens[ screen ].component }
</ScreenNavigation>
</>
);
}

return (
<div className="wporg-2fa__first-time" ref={ modalRef }>
<div className="wporg-2fa__first-time__inner">
<div className="wporg-2fa__first-time__inner-content">
<WordPressLogo />
{ currentScreenComponent }
</div>
</div>
</div>
);
}
89 changes: 89 additions & 0 deletions settings/src/components/first-time/first-time.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
.wporg-2fa__first-time {
position: fixed;
background: #fff;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 1000;

.wporg-2fa__first-time__inner {
box-sizing: border-box;
display: flex;
flex-direction: column;
align-items: center;
width: 100%;
height: 100%;
overflow: scroll;
}

.wporg-2fa__first-time__inner-content {
padding: 8vh 16px;
display: flex;
flex-direction: column;
align-items: center;

@media (min-width: 600px) {
width: 600px;
}

> svg {
margin-bottom: 32px;
width: 250px;
}

// Preven cards from shrinking.
.components-card {
width: 100%;
}

.components-card__body {
padding: 32px;
padding-top: 16px;
}
}

.wporg-2fa__first-time-default {
display: flex;
flex-direction: column;
gap: 18px;
padding-top: 8px;
}

.wporg-2fa__first-time-default-item {
margin: 0;
position: relative;
display: flex;
gap: 10px;
align-items: flex-start;
cursor: pointer;

span {
font-weight: 600;
z-index: 1;
}

input {
position: relative;
z-index: 1;
margin-top: 4px;
margin-left: 6px;
}

> div {
display: flex;
flex-direction: column;
align-items: flex-start;

p {
color: var(--wp--preset--color--charcoal-4, #656a71);
margin: 0;
z-index: 1;
}
}
}
}

.wporg-2fa__congratulations h3 {
margin-bottom: 14px;
}
Loading
Loading