Skip to content

Commit 6a60d15

Browse files
committed
chore(clerk-js): Introduce UserSettings in #Verify
1 parent 20572b3 commit 6a60d15

File tree

3 files changed

+190
-104
lines changed

3 files changed

+190
-104
lines changed

packages/clerk-js/src/ui/#/#Verify.test.tsx

+60-17
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { noop, render, renderJSON } from '@clerk/shared/testUtils';
22
import {
3+
AttributeData,
34
SessionResource,
4-
SignInStrategyName,
55
#Resource,
6+
UserSettingsJSON,
67
} from '@clerk/types';
8+
import { UserSettings } from 'core/resources/internal';
79
import React from 'react';
810

911
import {
@@ -25,7 +27,7 @@ const mockStartMagicLinkFlow = jest.fn(() => {
2527
},
2628
} as any as #Resource);
2729
});
28-
let mockFirstFactors: SignInStrategyName[];
30+
let mockEmailAddressAttribute: Partial<AttributeData>;
2931
let mockDisabledStrategies: string[] = [];
3032

3133
jest.mock('ui/router/RouteContext');
@@ -37,15 +39,43 @@ jest.mock('ui/contexts', () => {
3739
applicationName: 'My test app',
3840
after#Url: 'http://test.host',
3941
},
42+
userSettings: new UserSettings({
43+
attributes: {
44+
first_name: {
45+
enabled: true,
46+
required: false,
47+
},
48+
last_name: {
49+
enabled: true,
50+
required: false,
51+
},
52+
password: {
53+
enabled: true,
54+
required: false,
55+
},
56+
username: {
57+
enabled: true,
58+
required: false,
59+
},
60+
email_address: mockEmailAddressAttribute,
61+
},
62+
social: {
63+
oauth_google: {
64+
enabled: true,
65+
required: false,
66+
authenticatable: false,
67+
strategy: 'oauth_google',
68+
},
69+
oauth_facebook: {
70+
enabled: true,
71+
required: false,
72+
authenticatable: false,
73+
strategy: 'oauth_facebook',
74+
},
75+
},
76+
} as unknown as UserSettingsJSON),
4077
authConfig: {
41-
username: 'on',
42-
firstName: 'required',
43-
lastName: 'required',
44-
password: 'required',
45-
firstFactors: mockFirstFactors,
46-
identificationRequirements: [
47-
['email_address', 'phone_address', 'oauth_google', 'oauth_facebook'],
48-
],
78+
singleSessionMode: false,
4979
},
5080
})),
5181
useCoreSession: () => {
@@ -105,20 +135,33 @@ describe('<#Verify/>', () => {
105135
});
106136

107137
describe('verify email address', () => {
108-
it('renders the # verification form', async () => {
109-
mockFirstFactors = ['email_code', 'phone_code', 'password'];
138+
it('renders the # verification form', () => {
139+
mockEmailAddressAttribute = {
140+
enabled: true,
141+
first_factors: ['email_code'],
142+
verifications: ['email_code'],
143+
};
144+
// mockFirstFactors = ['email_code', 'phone_code', 'password'];
110145
const tree = renderJSON(<#VerifyEmailAddress />);
111146
expect(tree).toMatchSnapshot();
112147
});
113148

114-
it('renders the # verification form but prefers email_link if exists', async () => {
115-
mockFirstFactors = ['email_code', 'phone_code', 'password', 'email_link'];
149+
it('renders the # verification form but prefers email_link if exists', () => {
150+
mockEmailAddressAttribute = {
151+
enabled: true,
152+
first_factors: ['email_link'],
153+
verifications: ['email_link'],
154+
};
116155
const tree = renderJSON(<#VerifyEmailAddress />);
117156
expect(tree).toMatchSnapshot();
118157
});
119158

120-
it('can skip disabled verification strategies', async () => {
121-
mockFirstFactors = ['email_code', 'phone_code', 'password', 'email_link'];
159+
it('can skip disabled verification strategies', () => {
160+
mockEmailAddressAttribute = {
161+
enabled: true,
162+
first_factors: ['email_link'],
163+
verifications: ['email_link'],
164+
};
122165
mockDisabledStrategies = ['email_link'];
123166
const { container } = render(<#VerifyEmailAddress />);
124167
expect(container.querySelector('.cl-otp-input')).not.toBeNull();
@@ -131,7 +174,7 @@ describe('<#Verify/>', () => {
131174
});
132175

133176
describe('verify phone number', () => {
134-
it('renders the # verification form', async () => {
177+
it('renders the # verification form', () => {
135178
const tree = renderJSON(<#VerifyPhoneNumber />);
136179
expect(tree).toMatchSnapshot();
137180
});

packages/clerk-js/src/ui/#/#Verify.tsx

+5-6
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,17 @@ import {
2424

2525
import { #VerifyEmailAddressWithMagicLink } from './#VerifyEmailAddressWithMagicLink';
2626

27-
const emailLinkStrategy = 'email_link';
28-
2927
function _#VerifyEmailAddress(): JSX.Element {
30-
const { authConfig } = useEnvironment();
31-
const { firstFactors } = authConfig;
28+
const { userSettings } = useEnvironment();
29+
const { attributes } = userSettings;
3230

3331
// TODO: # should have a field similar to SignIn's supportedFirstFactors
3432
// listing the available strategies for this #
35-
const emailLinkStrategyEnabled = firstFactors.includes(emailLinkStrategy);
33+
const emailLinkStrategyEnabled =
34+
attributes.email_address.first_factors.includes('email_link');
3635
const disableEmailLink = shouldDisableStrategy(
3736
use#Context(),
38-
emailLinkStrategy,
37+
'email_link',
3938
);
4039

4140
if (emailLinkStrategyEnabled && !disableEmailLink) {

0 commit comments

Comments
 (0)