Skip to content
Draft
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-sign-up-email-link.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
33 changes: 23 additions & 10 deletions packages/mosaic/docs/internals/sign-in-sign-up-flow.md

Large diffs are not rendered by default.

93 changes: 88 additions & 5 deletions packages/mosaic/src/__tests__/feature/fake-fapi/sign-up.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export interface FakeSignUpSeed {
whatsAppQuotaExceeded?: boolean;
tickets?: Record<string, FakeTicket>;
oauth?: Record<string, FakeOAuthAccount>;
emailLinkRedirectUrl?: string;
}

export type FakeOAuthAccount =
Expand Down Expand Up @@ -85,6 +86,9 @@ export interface FakeSignUpState {
transfers: URLSearchParams[];
linkedStrategy?: string;
returnFromOAuth: () => void;
emailLinkRedirectUrl?: string;
openEmailLink: (clientId?: string) => string;
expireEmailLink: () => void;
issuedProtectChecks: number;
reloads: number;
proofTokens: string[];
Expand All @@ -109,6 +113,12 @@ export function createSignUpState(seed: FakeSignUpSeed = {}): FakeSignUpState {
returnFromOAuth: () => {
throw new Error('The fake FAPI has not started');
},
openEmailLink: () => {
throw new Error('The fake FAPI has not started');
},
expireEmailLink: () => {
throw new Error('The fake FAPI has not started');
},
issuedProtectChecks: 0,
reloads: 0,
proofTokens: [],
Expand Down Expand Up @@ -145,7 +155,12 @@ export default (container, { token, setWidgetVisible }) =>

let scriptIds = 0;

const STRATEGIES: Record<string, Identifier> = { email_code: 'email_address', phone_code: 'phone_number' };
const STRATEGIES: Record<string, Identifier> = {
email_code: 'email_address',
email_link: 'email_address',
phone_code: 'phone_number',
};
const EMAIL_LINK_LIFETIME_MS = 600_000;
const TEXT_FIELDS = ['email_address', 'phone_number', 'username', 'first_name', 'last_name'] as const;
const createdAt = Date.UTC(2026, 0, 1);

Expand Down Expand Up @@ -196,7 +211,12 @@ export function signUpHandlers(
return HttpResponse.json({ ...body, meta: { client: state.client } }, { status: response.status });
};

const completed = new Map<string, SignUpJSON>();

const store = (signUp: SignUpJSON) => {
if (signUp.status === 'complete') {
completed.set(signUp.id, signUp);
}
state.client = { ...state.client, sign_up: signUp.status === 'complete' ? null : signUp };
return envelope(signUp, state.client);
};
Expand Down Expand Up @@ -551,6 +571,11 @@ export function signUpHandlers(
if (signUp.verifications?.[field].status === 'verified') {
return rejected('verification_already_verified', 'This verification has already been verified.', undefined, 400);
}
const isLink = strategy === 'email_link';
const redirectUrl = body.get('redirect_url');
if (isLink && !redirectUrl) {
return rejected('form_conditional_param_missing', 'redirect_url must be included.', 'redirect_url');
}
const isPhone = field === 'phone_number';
if (isPhone && channel && channel !== 'sms' && channel !== 'whatsapp') {
return rejected('form_param_value_invalid', `${channel} does not match one of the allowed values.`, 'channel');
Expand All @@ -564,13 +589,26 @@ export function signUpHandlers(
}
const sentOver = channel === 'whatsapp' && state.signUp.whatsAppQuotaExceeded ? 'sms' : (channel ?? 'sms');
state.signUp.preparations.push(isPhone && channel ? `${strategy}:${channel}` : strategy);
const pending = signUp.verifications![field];
const linkStillOut = pending.strategy === 'email_link' && pending.status === 'unverified';
if (isLink && redirectUrl) {
state.signUp.emailLinkRedirectUrl = redirectUrl;
}
return {
...signUp,
verifications: {
...signUp.verifications!,
[field]: {
...signUp.verifications![field],
...fapiVerification(strategy),
...pending,
...fapiVerification(
strategy,
isLink
? {
object: 'verification_email_link',
expire_at: linkStillOut ? pending.expire_at : Date.now() + EMAIL_LINK_LIFETIME_MS,
}
: {},
),
next_action: 'needs_attempt',
supported_strategies: [strategy],
channel: isPhone ? (sentOver as PhoneCodeChannel) : undefined,
Expand All @@ -579,6 +617,51 @@ export function signUpHandlers(
};
};

const emailLink = () => {
const signUp = state.client.sign_up;
const pending = signUp?.verifications?.email_address;
const redirectUrl = state.signUp.emailLinkRedirectUrl;
if (!signUp?.verifications || pending?.strategy !== 'email_link' || !redirectUrl) {
throw new Error('No email link has been sent');
}
return { signUp, verifications: signUp.verifications, pending, landing: new URL(redirectUrl) };
};

state.signUp.openEmailLink = (clientId = state.client.id) => {
const { signUp, verifications, pending, landing } = emailLink();
if (pending.status === 'expired') {
landing.searchParams.append('__clerk_status', 'expired');
return landing.toString();
}
if (pending.status !== 'unverified') {
throw new Error('The email link has already been opened');
}
const verified = settle({
...signUp,
verifications: {
...verifications,
email_address: { ...pending, status: 'verified', next_action: '', verified_at_client: clientId },
},
});
store(verified);
landing.searchParams.append('__clerk_status', 'verified');
if (verified.created_session_id) {
landing.searchParams.append('__clerk_created_session', verified.created_session_id);
}
return landing.toString();
};

state.signUp.expireEmailLink = () => {
const { signUp, verifications, pending } = emailLink();
store({
...signUp,
verifications: {
...verifications,
email_address: { ...pending, status: 'expired', next_action: 'needs_prepare' },
},
});
};

if (state.client.sign_up) {
state.client = { ...state.client, sign_up: settle(state.client.sign_up) };
}
Expand Down Expand Up @@ -743,7 +826,7 @@ export function signUpHandlers(
);
}),
http.get(fapiUrl('/v1/client/sign_ups/:id'), ({ params }) => {
const signUp = current(params.id);
const signUp = current(params.id) ?? completed.get(String(params.id));
if (!signUp) {
return missing();
}
Expand Down Expand Up @@ -786,7 +869,7 @@ export function signUpHandlers(
const body = new URLSearchParams(await request.text());
const strategy = body.get('strategy') ?? '';
const code = body.get('code') ?? '';
const field = STRATEGIES[strategy];
const field = strategy === 'email_link' ? undefined : STRATEGIES[strategy];
if (!field) {
return rejected(
'form_param_value_invalid',
Expand Down
Loading
Loading