Skip to content

Commit

Permalink
feat(SignIn): update sign in page for passcode functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
coldlink committed Oct 16, 2024
1 parent b870a98 commit 3b2ba67
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 8 deletions.
20 changes: 20 additions & 0 deletions src/client/pages/SignIn.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,23 @@ export const IsReauthenticate = (args: SignInProps) => (
IsReauthenticate.story = {
name: 'showing /reauthenticate page',
};

export const WithPasscode = (args: SignInProps) => (
<SignIn {...{ ...args, usePasscodeDefaultChecked: true }} />
);
WithPasscode.story = {
name: 'with passcode checkbox checked',
};

export const WithPasscodeError = (args: SignInProps) => (
<SignIn
{...{
...args,
usePasscodeDefaultChecked: true,
pageError: SignInErrors.PASSCODE_EXPIRED,
}}
/>
);
WithPasscode.story = {
name: 'with passcode checkbox checked',
};
68 changes: 60 additions & 8 deletions src/client/pages/SignIn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import locations from '@/shared/lib/locations';
import { SUPPORT_EMAIL } from '@/shared/model/Configuration';
import { MinimalLayout } from '@/client/layouts/MinimalLayout';
import ThemedLink from '@/client/components/ThemedLink';
import { Checkbox } from '@guardian/source/react-components';

export type SignInProps = {
queryParams: QueryParams;
Expand All @@ -34,6 +35,8 @@ export type SignInProps = {
recaptchaSiteKey: string;
isReauthenticate?: boolean;
shortRequestId?: string;
// If true, the OTP checkbox will be checked by default
usePasscodeDefaultChecked?: boolean;
};

const resetPassword = css`
Expand Down Expand Up @@ -90,6 +93,21 @@ const getErrorContext = (
<a href={locations.SUPPORT_EMAIL_MAILTO}>{SUPPORT_EMAIL}</a>
</>
);
} else if (error === SignInErrors.PASSCODE_EXPIRED) {
return (
<>
<div>Please request a new one-time code to sign in.</div>
<br />
<div>
If you are still having trouble, please contact our customer service
team at{' '}
<ThemedLink href={locations.SUPPORT_EMAIL_MAILTO}>
{SUPPORT_EMAIL}
</ThemedLink>
.
</div>
</>
);
}
};

Expand All @@ -116,6 +134,27 @@ const showAuthProviderButtons = (
}
};

const OTPCheckbox = ({
checked = false,
onChange,
}: {
checked?: boolean;
onChange: React.ChangeEventHandler<HTMLInputElement>;
}) => {
return (
<Checkbox
label="Request a one-time code to sign in"
name="passcode"
id="passcode"
theme={{
textLabel: `var(--color-input-text)`,
}}
defaultChecked={checked}
onChange={onChange}
/>
);
};

export const SignIn = ({
email,
pageError,
Expand All @@ -124,7 +163,12 @@ export const SignIn = ({
recaptchaSiteKey,
isReauthenticate = false,
shortRequestId,
usePasscodeDefaultChecked = false,
}: SignInProps) => {
const [usePasscode, setUsePasscode] = React.useState(
usePasscodeDefaultChecked,
);

const formTrackingName = 'sign-in';

// The page level error is equivalent to this enum if social signin has been blocked.
Expand Down Expand Up @@ -157,7 +201,7 @@ export const SignIn = ({
{},
queryParams,
)}
submitButtonText="Sign in"
submitButtonText={usePasscode ? 'Request one-time code' : 'Sign in'}
recaptchaSiteKey={recaptchaSiteKey}
formTrackingName={formTrackingName}
disableOnSubmit
Expand All @@ -167,13 +211,21 @@ export const SignIn = ({
hasJobsTerms={isJobs && socialSigninBlocked}
>
<EmailInput defaultValue={email} />
<PasswordInput label="Password" autoComplete="current-password" />
<ThemedLink
href={buildUrlWithQueryParams('/reset-password', {}, queryParams)}
cssOverrides={resetPassword}
>
Reset password
</ThemedLink>
{!usePasscode && (
<>
<PasswordInput label="Password" autoComplete="current-password" />
<ThemedLink
href={buildUrlWithQueryParams('/reset-password', {}, queryParams)}
cssOverrides={resetPassword}
>
Reset password
</ThemedLink>
</>
)}
<OTPCheckbox
checked={usePasscode}
onChange={(event) => setUsePasscode(event.target.checked)}
/>
</MainForm>
{!isReauthenticate && (
<>
Expand Down

0 comments on commit 3b2ba67

Please sign in to comment.