Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions .changeset/mosaic-auth-flow-shared.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
2 changes: 2 additions & 0 deletions packages/mosaic/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ Most problems a feature hits already have a shared answer. Find the line for you
- An action that may need step-up verification → `useReverificationFlow` (`src/features/reverification/`) in the model, passed to `useConfirmController`
- A direct action that needs step-up verification → `useReverificationFlow` in the model, with `ReverificationDialog` (`src/blocks/reverification/`) in the view
- A step that verifies a factor (a code, password, backup code or passkey, the method picker, help) → the steps in `src/blocks/verification/`, such as `VerificationCode`
- A screen sign-in and sign-up both show (an email link and its status, a loading or protect-check notice, a blocked request, the wallet or enterprise connection picker, the alternative phone channel, the identity line, the captcha) → the steps in `src/blocks/auth/`, such as `AuthEmailLink`
- Showing any error to the user → One of the owners above. They call `toLocalizableError` (or `toLocalizableErrors` for all errors) and resolve copy with `useErrorText`
- Moving focus after a list row is removed → `useListRemovalFocus` (`src/hooks/`)
- A debounced, abortable async check as the user types → `useDebouncedAsync` (`src/hooks/`)
Expand All @@ -85,6 +86,7 @@ Most problems a feature hits already have a shared answer. Find the line for you
- An inline message row; height is undefined until measured → `useHeldMessage`, `useMessageHeight`, `FeedbackBody` (`src/utils/feedback.tsx`) + `src/styles/feedback.styles.ts`
- Data read from a Clerk resource that should be cached across mounts → A query hook in `src/queries/`, called from the model (`src/queries/use-user-sessions-query.ts`)
- The seconds left on a resend cooldown, for display → `useResendSeconds` (`src/hooks/`), from a `resendAvailableAt` time kept in the machine
- The Resend link on a code or email link step, with its countdown → `verificationResend` (`src/blocks/verification/`), passed to the step as `resend`
- An avatar or logo row: picker, drop target, crop dialog, Upload button or manage menu → `AvatarUpload` (`src/blocks/avatar-upload/`), laid out by the caller
- Any other file picker or drop zone, such as a metadata or key file → `FileUpload` (`src/primitives/file-upload/`), see its `README.md`
- Cropping an image to a square: pan, zoom, rotate, then draw the result → `ImageCropper` and `cropImage` (`src/primitives/image-cropper/`), see its `README.md`
Expand Down
1 change: 1 addition & 0 deletions packages/mosaic/docs/views.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ A block is a view fragment that owns one piece of state nothing outside it can u
- `blocks/confirmation` (`Confirmation`) is a confirm step with no typed phrase.
- `blocks/avatar-upload` (`AvatarUpload`) is an avatar or logo picker. Its parts share the file input, so the caller places them in its own layout.
- `blocks/verification` holds the steps that verify a factor, such as `VerificationCode`. Each renders the inside of a card and takes its copy as `messages`, so reverification and sign-in show them with their own strings.
- `blocks/auth` holds the screens sign-in and sign-up share, such as `AuthEmailLink`. Each renders the inside of a card and takes its copy as `messages`, so sign-up shows them with its own strings.

`Destructive` and `Confirmation` take a `controller` from `useConfirmController` (`blocks/confirm/`), which decides whether the dialog closes or explains itself. The hook runs in the wrapper or controller, never in the view. A dialog next to its button takes the button as `trigger`:

Expand Down
14 changes: 14 additions & 0 deletions packages/mosaic/src/blocks/auth/__tests__/auth.utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { describe, expect, it } from 'vitest';

import { httpsUrl } from '../auth.utils';

describe('httpsUrl', () => {
it('keeps an https link and drops any other scheme or an unparsable value', () => {
expect(httpsUrl('https://acme.com/support')).toBe('https://acme.com/support');
expect(httpsUrl('javascript:alert(1)')).toBeUndefined();
expect(httpsUrl('http://acme.com/support')).toBeUndefined();
expect(httpsUrl('data:text/html,hi')).toBeUndefined();
expect(httpsUrl('/support')).toBeUndefined();
expect(httpsUrl(undefined)).toBeUndefined();
});
});
65 changes: 65 additions & 0 deletions packages/mosaic/src/blocks/auth/auth-action-blocked.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { Card } from '../../components/card';
import { CopyButton } from '../../components/copy-button';
import { Field } from '../../components/field';
import { InputGroup } from '../../components/input-group';
import { Link } from '../../components/link';
import type { AuthActionBlockedDetails } from './auth.types';
import { httpsUrl } from './auth.utils';

export interface AuthActionBlockedMessages {
title: string;
description: string;
traceIdLabel: string;
copyTraceId: string;
traceIdCopied: string;
}

interface AuthActionBlockedProps {
messages: AuthActionBlockedMessages;
details?: AuthActionBlockedDetails;
}

export function AuthActionBlocked({ messages, details }: AuthActionBlockedProps) {
const href = httpsUrl(details?.linkUrl);

return (
<>
<Card.Header>
<Card.Title>{details?.title ?? messages.title}</Card.Title>
<Card.Description>{details?.description ?? messages.description}</Card.Description>
{href ? (
<Card.Description>
<Link
href={href}
target='_blank'
rel='noopener noreferrer'
size='sm'
>
{details?.linkText ?? href}
</Link>
</Card.Description>
) : null}
</Card.Header>
{details?.traceId ? (
<Card.Content>
<Field.Root>
<Field.Label>{messages.traceIdLabel}</Field.Label>
<InputGroup.Root>
<InputGroup.Input
value={details.traceId}
readOnly
/>
<InputGroup.End>
<CopyButton
value={details.traceId}
label={messages.copyTraceId}
copiedLabel={messages.traceIdCopied}
/>
</InputGroup.End>
</InputGroup.Root>
</Field.Root>
</Card.Content>
) : null}
</>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,51 @@ import { useMergeRefs } from '@floating-ui/react';
import * as stylex from '@stylexjs/stylex';
import type { ReactNode, Ref } from 'react';

import { Button, SubmitButton } from '../../../components/button';
import { Card } from '../../../components/card';
import { Field } from '../../../components/field';
import { useFlowAutoFocus } from '../../../components/flow';
import type { FieldFeedback, UseFormResult } from '../../../components/form';
import { PhoneInput } from '../../../components/phone-input';
import { fill, useMessages } from '../../../localization';
import { Form } from '../../../primitives/form';
import { styles } from '../sign-in.styles';
import type { SignInProvider, SignInSubmitEvent, SignInValues } from '../sign-in.types';
import { SignInCaptcha } from './sign-in-captcha';
import { Button, SubmitButton } from '../../components/button';
import { Card } from '../../components/card';
import { Field } from '../../components/field';
import { useFlowAutoFocus } from '../../components/flow';
import type { FieldFeedback, UseFormResult } from '../../components/form';
import { PhoneInput } from '../../components/phone-input';
import { fill } from '../../localization';
import { Form } from '../../primitives/form';
import { styles } from './auth.styles';
import type { AuthProvider } from './auth.types';
import { AuthCaptcha } from './auth-captcha';

interface SignInAlternativeChannelProps {
channel: SignInProvider;
export interface AuthAlternativeChannelMessages {
title: string;
description: string;
fieldLabel: string;
phonePlaceholder: string;
selectedCountry: string;
chooseCountry: string;
searchCountries: string;
countrySearchPlaceholder: string;
noCountries: string;
secondaryActionLabel: string;
primaryActionLabel: string;
pendingLabel: string;
}

interface AuthAlternativeChannelProps<TValues extends { phoneNumber: string }> {
messages: AuthAlternativeChannelMessages;
channel: AuthProvider;
applicationName: string;
logo?: { src: string; alt: string };
form: UseFormResult<SignInValues>;
form: UseFormResult<TValues>;
errorMessage?: string;
globalErrorMessage?: string;
isPending: boolean;
isDisabled: boolean;
onSubmit: (event: SignInSubmitEvent) => void;
onSubmit: (event: { preventDefault: () => void }) => void;
onCancel?: () => void;
focusRef?: Ref<HTMLElement>;
copy?: { title: string; description: string; label: string };
children?: ReactNode;
}

export function SignInAlternativeChannel({
export function AuthAlternativeChannel<TValues extends { phoneNumber: string }>({
messages,
channel,
applicationName,
logo,
Expand All @@ -42,15 +58,12 @@ export function SignInAlternativeChannel({
onSubmit,
onCancel,
focusRef,
copy: copyProp,
children,
}: SignInAlternativeChannelProps) {
const m = useMessages('signIn');
}: AuthAlternativeChannelProps<TValues>) {
const { ref: phoneFieldRef, ...phone } = form.registerValue('phoneNumber');
const phoneRef = useMergeRefs<HTMLElement>([phoneFieldRef, focusRef, useFlowAutoFocus<HTMLInputElement>()]);
const feedback: FieldFeedback | undefined = errorMessage ? { type: 'error', message: errorMessage } : undefined;
const provider = channel.name;
const copy = copyProp ?? m.alternativeChannel;

return (
<>
Expand All @@ -64,8 +77,8 @@ export function SignInAlternativeChannel({
/>
</Card.Media>
) : null}
<Card.Title>{fill(copy.title, { applicationName, provider })}</Card.Title>
<Card.Description>{fill(copy.description, { provider })}</Card.Description>
<Card.Title>{fill(messages.title, { applicationName, provider })}</Card.Title>
<Card.Description>{fill(messages.description, { provider })}</Card.Description>
</Card.Header>
<Card.Banner
role='alert'
Expand All @@ -85,24 +98,24 @@ export function SignInAlternativeChannel({
disabled={isDisabled}
focusableWhenDisabled
>
<Field.Label>{fill(copy.label, { provider })}</Field.Label>
<Field.Label>{fill(messages.fieldLabel, { provider })}</Field.Label>
<PhoneInput
ref={phoneRef}
autoComplete='tel'
placeholder={m.start.phonePlaceholder}
countryTriggerLabel={m.start.selectedCountry}
countryPopupLabel={m.start.chooseCountry}
countrySearchLabel={m.start.searchCountries}
countrySearchPlaceholder={m.start.countrySearchPlaceholder}
noResultsMessage={m.start.noCountries}
placeholder={messages.phonePlaceholder}
countryTriggerLabel={messages.selectedCountry}
countryPopupLabel={messages.chooseCountry}
countrySearchLabel={messages.searchCountries}
countrySearchPlaceholder={messages.countrySearchPlaceholder}
noResultsMessage={messages.noCountries}
{...phone}
/>
<Field.Feedback feedback={feedback} />
</Field.Root>
{children}
</div>
</Form>
<SignInCaptcha />
<AuthCaptcha />
</Card.Content>
<Card.Footer>
{onCancel ? (
Expand All @@ -114,16 +127,16 @@ export function SignInAlternativeChannel({
disabled={isDisabled}
onClick={onCancel}
>
{m.useAnotherMethod}
{messages.secondaryActionLabel}
</Button>
) : null}
<SubmitButton
form={form.id}
fullWidth
isPending={isPending}
pendingLabel={m.verifying}
pendingLabel={messages.pendingLabel}
>
{m.continue}
{messages.primaryActionLabel}
</SubmitButton>
</Card.Footer>
</>
Expand Down
20 changes: 20 additions & 0 deletions packages/mosaic/src/blocks/auth/auth-captcha.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Captcha } from '../../components/captcha';
import { useFlowStepActive } from '../../primitives/flow';
import { styles } from './auth.styles';

interface AuthCaptchaProps {
onInteractiveChange?: (interactive: boolean) => void;
}

export function AuthCaptcha({ onInteractiveChange }: AuthCaptchaProps) {
const isActive = useFlowStepActive();
if (!isActive) {
return null;
}
return (
<Captcha
xstyle={styles.captcha}
onInteractiveChange={onInteractiveChange}
/>
);
}
59 changes: 59 additions & 0 deletions packages/mosaic/src/blocks/auth/auth-email-link-status.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { Card } from '../../components/card';
import { Spinner } from '../../components/spinner';
import { Text } from '../../components/text';
import { styles } from './auth.styles';
import type { AuthEmailLinkStatusValue as Status } from './auth.types';

interface StatusCopy {
title: string;
description: string;
}

export interface AuthEmailLinkStatusMessages {
loading: StatusCopy;
signedIn: StatusCopy;
signedInOtherTab: StatusCopy;
transferable: StatusCopy;
expired: StatusCopy;
failed: StatusCopy;
clientMismatch: StatusCopy;
closeTab: string;
}

const statusCopy = {
loading: 'loading',
'signed-in': 'signedIn',
'signed-in-other-tab': 'signedInOtherTab',
transferable: 'transferable',
expired: 'expired',
failed: 'failed',
'client-mismatch': 'clientMismatch',
} as const satisfies Record<Status, keyof AuthEmailLinkStatusMessages>;

interface AuthEmailLinkStatusProps {
messages: AuthEmailLinkStatusMessages;
status: Status;
globalErrorMessage?: string;
}

export function AuthEmailLinkStatus({ messages, status, globalErrorMessage }: AuthEmailLinkStatusProps) {
const copy = messages[statusCopy[status]];

return (
<>
<Card.Header>
<Card.Title>{copy.title}</Card.Title>
<Card.Description>{copy.description}</Card.Description>
</Card.Header>
<Card.Banner
role='alert'
color='negative'
>
{globalErrorMessage}
</Card.Banner>
<Card.Content xstyle={styles.status}>
{status === 'loading' ? <Spinner /> : <Text color='foreground-secondary'>{messages.closeTab}</Text>}
</Card.Content>
</>
);
}
Loading
Loading