Skip to content

Commit 14a687f

Browse files
feat(mosaic): add the sign-in email link outcome screens (#10811)
1 parent 03da9b1 commit 14a687f

13 files changed

Lines changed: 286 additions & 78 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
---

‎packages/mosaic/src/features/sign-in/__tests__/sign-in.utils.test.ts‎

Lines changed: 70 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,90 @@ import { describe, expect, it } from 'vitest';
22

33
import { signInMessages } from '../sign-in.messages';
44
import type { SignInStep } from '../sign-in.types';
5+
import type { SignInStepKey } from '../sign-in.utils';
56
import {
67
codeCopy,
78
codeIdentity,
89
codeNotice,
910
codeSecondaryAction,
11+
emailLinkStatusCopy,
1012
httpsUrl,
1113
identifierFieldCopy,
1214
splitLastUsedProvider,
1315
startPrompt,
16+
stepIds,
1417
stepKey,
1518
} from '../sign-in.utils';
1619

20+
const provider = { id: 'whatsapp', name: 'WhatsApp', iconUrl: 'https://img.clerk.com/static/whatsapp.svg' };
21+
22+
const stepsByKey: Record<SignInStepKey, SignInStep> = {
23+
identifier: { kind: 'identifier', field: 'email' },
24+
'identifier:channel': { kind: 'identifier', field: 'phone', channel: provider },
25+
'credential:password': { kind: 'credential', credential: 'password', identifier: 'jane@acme.com' },
26+
'credential:backup-code': { kind: 'credential', credential: 'backup-code', identifier: 'jane@acme.com' },
27+
'credential:passkey': { kind: 'credential', credential: 'passkey', identifier: 'jane@acme.com' },
28+
'code:first-factor': { kind: 'code', purpose: 'first-factor', channel: 'email', identifier: 'jane@acme.com' },
29+
'code:second-factor': { kind: 'code', purpose: 'second-factor', channel: 'totp' },
30+
'code:client-trust': { kind: 'code', purpose: 'client-trust', channel: 'email' },
31+
'code:sso-bypass': { kind: 'code', purpose: 'sso-bypass', channel: 'email' },
32+
'code:reset-password': { kind: 'code', purpose: 'reset-password', channel: 'email', identifier: 'jane@acme.com' },
33+
'code:verify-email': { kind: 'code', purpose: 'verify-email', channel: 'email', identifier: 'new@acme.com' },
34+
'code:verify-phone': { kind: 'code', purpose: 'verify-phone', channel: 'phone' },
35+
'choice:methods': { kind: 'choice', purpose: 'methods', options: [] },
36+
'choice:reset-methods': { kind: 'choice', purpose: 'reset-methods', options: [] },
37+
'choice:enterprise-connections': { kind: 'choice', purpose: 'enterprise-connections', options: [] },
38+
'choice:accounts': { kind: 'choice', purpose: 'accounts', options: [] },
39+
'choice:wallets': { kind: 'choice', purpose: 'wallets', options: [] },
40+
'notice:help': { kind: 'notice', purpose: 'help' },
41+
'notice:forgot-password': { kind: 'notice', purpose: 'forgot-password', identifier: 'jane@acme.com' },
42+
'notice:no-methods': { kind: 'notice', purpose: 'no-methods' },
43+
'notice:action-blocked': { kind: 'notice', purpose: 'action-blocked' },
44+
'notice:password-compromised': { kind: 'notice', purpose: 'password-compromised' },
45+
'notice:enterprise-sso': { kind: 'notice', purpose: 'enterprise-sso' },
46+
'notice:email-link': { kind: 'notice', purpose: 'email-link', identifier: 'jane@acme.com' },
47+
'notice:email-link-status': { kind: 'notice', purpose: 'email-link-status', status: 'expired' },
48+
'new-password:forgot': { kind: 'new-password', reason: 'forgot' },
49+
'sign-up': { kind: 'sign-up' },
50+
};
51+
1752
describe('stepKey', () => {
1853
it('keys each step by its kind and the variant that changes its copy', () => {
19-
const steps: [SignInStep, string][] = [
20-
[{ kind: 'identifier', field: 'email' }, 'identifier'],
21-
[
22-
{
23-
kind: 'identifier',
24-
field: 'phone',
25-
channel: { id: 'whatsapp', name: 'WhatsApp', iconUrl: 'https://img.clerk.com/static/whatsapp.svg' },
26-
},
27-
'identifier:channel',
28-
],
29-
[{ kind: 'credential', credential: 'password', identifier: 'jane@acme.com' }, 'credential:password'],
30-
[{ kind: 'credential', credential: 'backup-code', identifier: 'jane@acme.com' }, 'credential:backup-code'],
31-
[{ kind: 'credential', credential: 'passkey', identifier: 'jane@acme.com' }, 'credential:passkey'],
32-
[{ kind: 'code', purpose: 'first-factor', channel: 'email', identifier: 'jane@acme.com' }, 'code:first-factor'],
33-
[{ kind: 'code', purpose: 'second-factor', channel: 'totp' }, 'code:second-factor'],
34-
[{ kind: 'code', purpose: 'verify-email', channel: 'email', identifier: 'new@acme.com' }, 'code:verify-email'],
35-
[{ kind: 'choice', purpose: 'methods', options: [] }, 'choice:methods'],
36-
[{ kind: 'choice', purpose: 'reset-methods', options: [] }, 'choice:reset-methods'],
37-
[{ kind: 'choice', purpose: 'accounts', options: [] }, 'choice:accounts'],
38-
[{ kind: 'choice', purpose: 'wallets', options: [] }, 'choice:wallets'],
39-
[{ kind: 'notice', purpose: 'help' }, 'notice:help'],
40-
[
41-
{ kind: 'code', purpose: 'reset-password', channel: 'email', identifier: 'jane@acme.com' },
42-
'code:reset-password',
43-
],
44-
[{ kind: 'notice', purpose: 'forgot-password', identifier: 'jane@acme.com' }, 'notice:forgot-password'],
45-
[{ kind: 'notice', purpose: 'action-blocked' }, 'notice:action-blocked'],
46-
[{ kind: 'notice', purpose: 'email-link', identifier: 'jane@acme.com' }, 'notice:email-link'],
47-
[{ kind: 'new-password', reason: 'forgot' }, 'new-password:forgot'],
48-
[{ kind: 'sign-up' }, 'sign-up'],
49-
];
50-
51-
expect(steps.map(([step]) => stepKey(step))).toEqual(steps.map(([, key]) => key));
54+
const steps = Object.entries(stepsByKey);
55+
56+
expect(steps.map(([, step]) => stepKey(step))).toEqual(steps.map(([key]) => key));
57+
});
58+
59+
it('gives every step a key the view lists in a flow step, and lists no key a step cannot produce', () => {
60+
const listed = Object.values(stepIds).flat();
61+
const produced = Object.values(stepsByKey).map(step => stepKey(step));
62+
63+
expect([...listed].sort()).toEqual([...produced].sort());
64+
});
65+
});
66+
67+
describe('emailLinkStatusCopy', () => {
68+
const m = signInMessages;
69+
70+
it('tells the tab the link opened in to go back to the original tab', () => {
71+
expect(emailLinkStatusCopy('signed-in', m)).toEqual({
72+
title: 'Successfully signed in',
73+
description: 'Return to original tab to continue',
74+
});
75+
expect(emailLinkStatusCopy('transferable', m).title).toBe('Email verified');
76+
});
77+
78+
it('tells the original tab to go to the newly opened one', () => {
79+
expect(emailLinkStatusCopy('signed-in-other-tab', m)).toEqual({
80+
title: 'Signed in on other tab',
81+
description: 'Return to the newly opened tab to continue',
82+
});
83+
});
84+
85+
it('names what went wrong with the link', () => {
86+
expect(emailLinkStatusCopy('expired', m).title).toBe('This verification link has expired');
87+
expect(emailLinkStatusCopy('failed', m).title).toBe('This verification link is invalid');
88+
expect(emailLinkStatusCopy('client-mismatch', m).title).toBe('Verification link is invalid for this device');
5289
});
5390
});
5491

‎packages/mosaic/src/features/sign-in/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export { SignInView } from './sign-in.view';
22
export type {
33
SignInAccount,
44
SignInActionBlockedDetails,
5+
SignInEmailLinkStatus,
56
SignInFactor,
67
SignInIdentifierField,
78
SignInProvider,

‎packages/mosaic/src/features/sign-in/sign-in.messages.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,36 @@ export const signInMessages = {
7979
resendText: 'Didn’t receive a link?',
8080
resend: 'Resend',
8181
resendCountdown: 'Resend ({seconds})',
82+
loading: {
83+
title: 'Signing in...',
84+
description: 'You will be redirected soon',
85+
},
86+
signedIn: {
87+
title: 'Successfully signed in',
88+
description: 'Return to original tab to continue',
89+
},
90+
signedInOtherTab: {
91+
title: 'Signed in on other tab',
92+
description: 'Return to the newly opened tab to continue',
93+
},
94+
transferable: {
95+
title: 'Email verified',
96+
description: 'Return to original tab to continue',
97+
},
98+
expired: {
99+
title: 'This verification link has expired',
100+
description: 'Return to the original tab to continue.',
101+
},
102+
failed: {
103+
title: 'This verification link is invalid',
104+
description: 'Return to the original tab to continue.',
105+
},
106+
clientMismatch: {
107+
title: 'Verification link is invalid for this device',
108+
description:
109+
'To continue, open the verification link on the device and browser from which you initiated the sign-in',
110+
},
111+
closeTab: 'You may close this tab',
82112
},
83113
passkey: {
84114
title: 'Use your passkey',

‎packages/mosaic/src/features/sign-in/sign-in.styles.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,9 @@ export const styles = stylex.create({
6464
footerText: {
6565
textAlign: 'center',
6666
},
67+
status: {
68+
alignItems: 'center',
69+
display: 'flex',
70+
flexDirection: 'column',
71+
},
6772
});

‎packages/mosaic/src/features/sign-in/sign-in.types.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,19 @@ export type SignInStep =
4545
| { kind: 'notice'; purpose: 'no-methods' | 'password-compromised' | 'enterprise-sso' }
4646
| { kind: 'notice'; purpose: 'action-blocked'; details?: SignInActionBlockedDetails }
4747
| { kind: 'notice'; purpose: 'email-link'; identifier: string }
48+
| { kind: 'notice'; purpose: 'email-link-status'; status: SignInEmailLinkStatus }
4849
| { kind: 'new-password'; reason: 'forgot' }
4950
| { kind: 'sign-up' };
5051

52+
export type SignInEmailLinkStatus =
53+
| 'loading'
54+
| 'signed-in'
55+
| 'signed-in-other-tab'
56+
| 'transferable'
57+
| 'expired'
58+
| 'failed'
59+
| 'client-mismatch';
60+
5161
export interface SignInActionBlockedDetails {
5262
title?: string;
5363
description?: string;

‎packages/mosaic/src/features/sign-in/sign-in.utils.ts‎

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,45 @@ import { stringToFormattedPhoneString } from '@clerk/shared/phone';
22

33
import type { MosaicMessages } from '../../localization';
44
import { fill } from '../../localization';
5-
import type { SignInIdentifierField, SignInProvider, SignInStep } from './sign-in.types';
5+
import type { SignInEmailLinkStatus, SignInIdentifierField, SignInProvider, SignInStep } from './sign-in.types';
66

7-
export function stepKey(step: SignInStep): string {
7+
export const stepIds = {
8+
identifier: ['identifier'],
9+
identifierChannel: ['identifier:channel'],
10+
credential: ['credential:password', 'credential:backup-code', 'credential:passkey'],
11+
code: [
12+
'code:first-factor',
13+
'code:second-factor',
14+
'code:client-trust',
15+
'code:sso-bypass',
16+
'code:reset-password',
17+
'code:verify-email',
18+
'code:verify-phone',
19+
],
20+
choice: [
21+
'choice:methods',
22+
'choice:reset-methods',
23+
'choice:enterprise-connections',
24+
'choice:accounts',
25+
'choice:wallets',
26+
],
27+
notice: [
28+
'notice:help',
29+
'notice:forgot-password',
30+
'notice:no-methods',
31+
'notice:action-blocked',
32+
'notice:password-compromised',
33+
'notice:enterprise-sso',
34+
'notice:email-link',
35+
'notice:email-link-status',
36+
],
37+
newPassword: ['new-password:forgot'],
38+
signUp: ['sign-up'],
39+
} as const;
40+
41+
export type SignInStepKey = (typeof stepIds)[keyof typeof stepIds][number];
42+
43+
export function stepKey(step: SignInStep): SignInStepKey {
844
switch (step.kind) {
945
case 'identifier':
1046
return 'channel' in step ? 'identifier:channel' : 'identifier';
@@ -15,9 +51,11 @@ export function stepKey(step: SignInStep): string {
1551
case 'new-password':
1652
return `new-password:${step.reason}`;
1753
case 'code':
54+
return `code:${step.purpose}`;
1855
case 'choice':
56+
return `choice:${step.purpose}`;
1957
case 'notice':
20-
return `${step.kind}:${step.purpose}`;
58+
return `notice:${step.purpose}`;
2159
}
2260
}
2361

@@ -36,6 +74,28 @@ type Messages = MosaicMessages['signIn'];
3674
type CodeStepData = Extract<SignInStep, { kind: 'code' }>;
3775
type CodeResendCopy = Pick<Messages['emailCode'], 'resendText' | 'resend' | 'resendCountdown'>;
3876

77+
export function emailLinkStatusCopy(
78+
status: SignInEmailLinkStatus,
79+
m: Messages,
80+
): { title: string; description: string } {
81+
switch (status) {
82+
case 'loading':
83+
return m.emailLink.loading;
84+
case 'signed-in':
85+
return m.emailLink.signedIn;
86+
case 'signed-in-other-tab':
87+
return m.emailLink.signedInOtherTab;
88+
case 'transferable':
89+
return m.emailLink.transferable;
90+
case 'expired':
91+
return m.emailLink.expired;
92+
case 'failed':
93+
return m.emailLink.failed;
94+
case 'client-mismatch':
95+
return m.emailLink.clientMismatch;
96+
}
97+
}
98+
3999
export function codeCopy(
40100
step: CodeStepData,
41101
m: Messages,

0 commit comments

Comments
 (0)