Skip to content

Commit 1e0b142

Browse files
[SDK] Implement allowedSmsCountryCodes option (#7090)
Co-authored-by: Jonas Daniels <jonas.daniels@outlook.com>
1 parent 70a519a commit 1e0b142

File tree

7 files changed

+62
-8
lines changed

7 files changed

+62
-8
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
Allow limiting the selectable countries for SMS login via a new `allowedSmsCountryCodes` option placed alongside `defaultSmsCountryCode`.

packages/thirdweb/src/react/web/wallets/in-app/CountrySelector.tsx

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,26 @@ export function getCountrySelector(countryIsoCode: SupportedSmsCountry) {
2121
export function CountrySelector({
2222
countryCode,
2323
setCountryCode,
24+
allowedCountryCodes,
2425
}: {
2526
countryCode: string;
2627
setCountryCode: React.Dispatch<React.SetStateAction<string>>;
28+
allowedCountryCodes?: SupportedSmsCountry[];
2729
}) {
2830
const selectRef = useRef<HTMLSelectElement>(null);
2931

30-
const supportedCountriesForSms = supportedSmsCountries ?? [
31-
{
32-
countryIsoCode: "US",
33-
countryName: "United States",
34-
phoneNumberCode: 1,
35-
},
36-
];
32+
const supportedCountriesForSms =
33+
allowedCountryCodes && allowedCountryCodes.length > 0
34+
? supportedSmsCountries.filter((c) =>
35+
allowedCountryCodes.includes(c.countryIsoCode as SupportedSmsCountry),
36+
)
37+
: (supportedSmsCountries ?? [
38+
{
39+
countryIsoCode: "US",
40+
countryName: "United States",
41+
phoneNumberCode: 1,
42+
},
43+
]);
3744

3845
return (
3946
<>

packages/thirdweb/src/react/web/wallets/in-app/InputSelectionUI.test.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,27 @@ describe("InputSelectionUI", () => {
4242

4343
expect(screen.getByRole("combobox")).toHaveValue("US +1");
4444
});
45+
46+
it("should filter countries based on allowedSmsCountryCodes", () => {
47+
const mockGetCountrySelector = vi.mocked(getCountrySelector);
48+
mockGetCountrySelector.mockReturnValue("IN +91");
49+
50+
render(
51+
<InputSelectionUI
52+
onSelect={vi.fn()}
53+
placeholder=""
54+
name=""
55+
type=""
56+
submitButtonText=""
57+
format="phone"
58+
allowedSmsCountryCodes={["IN", "BR"]}
59+
/>,
60+
);
61+
62+
const options = screen.getAllByRole("option");
63+
const optionTexts = options.map((o) => o.textContent);
64+
expect(optionTexts.some((t) => t?.includes("India"))).toBe(true);
65+
expect(optionTexts.some((t) => t?.includes("Brazil"))).toBe(true);
66+
expect(optionTexts.some((t) => t?.includes("United States"))).toBe(false);
67+
});
4568
});

packages/thirdweb/src/react/web/wallets/in-app/InputSelectionUI.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,16 @@ export function InputSelectionUI(props: {
2424
format?: "phone";
2525
disabled?: boolean;
2626
defaultSmsCountryCode?: SupportedSmsCountry;
27+
allowedSmsCountryCodes?: SupportedSmsCountry[];
2728
}) {
2829
const [countryCodeInfo, setCountryCodeInfo] = useState(
2930
props.defaultSmsCountryCode
3031
? getCountrySelector(props.defaultSmsCountryCode)
31-
: "US +1",
32+
: props.allowedSmsCountryCodes &&
33+
props.allowedSmsCountryCodes.length > 0 &&
34+
props.allowedSmsCountryCodes[0]
35+
? getCountrySelector(props.allowedSmsCountryCodes[0])
36+
: "US +1",
3237
);
3338
const [input, setInput] = useState("");
3439
const [error, setError] = useState<string | undefined>();
@@ -69,6 +74,7 @@ export function InputSelectionUI(props: {
6974
<CountrySelector
7075
countryCode={countryCodeInfo}
7176
setCountryCode={setCountryCodeInfo}
77+
allowedCountryCodes={props.allowedSmsCountryCodes}
7278
/>
7379
)}
7480
<Input

packages/thirdweb/src/react/web/wallets/shared/ConnectWalletSocialOptions.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,9 @@ export const ConnectWalletSocialOptions = (
451451
defaultSmsCountryCode={
452452
wallet.getConfig()?.auth?.defaultSmsCountryCode
453453
}
454+
allowedSmsCountryCodes={
455+
wallet.getConfig()?.auth?.allowedSmsCountryCodes
456+
}
454457
/>
455458
) : (
456459
<WalletTypeRowButton

packages/thirdweb/src/wallets/ecosystem/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ export type EcosystemWalletCreationOptions = {
1919
* The default country code to use for SMS authentication
2020
*/
2121
defaultSmsCountryCode?: SupportedSmsCountry;
22+
23+
/**
24+
* The list of allowed country codes to display in the country selector
25+
*/
26+
allowedSmsCountryCodes?: SupportedSmsCountry[];
2227
};
2328
/**
2429
* The partnerId of the ecosystem wallet to connect to

packages/thirdweb/src/wallets/in-app/core/wallet/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ export type InAppWalletCreationOptions =
8383
* The default country code to use for SMS authentication
8484
*/
8585
defaultSmsCountryCode?: SupportedSmsCountry;
86+
87+
/**
88+
* The list of allowed country codes to display in the country selector
89+
*/
90+
allowedSmsCountryCodes?: SupportedSmsCountry[];
8691
};
8792
/**
8893
* Metadata to display in the Connect Modal

0 commit comments

Comments
 (0)