Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/limited-country-dropdown.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

Allow limiting the selectable countries for SMS login via a new `allowedSmsCountryCodes` option placed alongside `defaultSmsCountryCode`.
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,26 @@
export function CountrySelector({
countryCode,
setCountryCode,
allowedCountryCodes,
}: {
countryCode: string;
setCountryCode: React.Dispatch<React.SetStateAction<string>>;
allowedCountryCodes?: SupportedSmsCountry[];
}) {
const selectRef = useRef<HTMLSelectElement>(null);

const supportedCountriesForSms = supportedSmsCountries ?? [
{
countryIsoCode: "US",
countryName: "United States",
phoneNumberCode: 1,
},
];
const supportedCountriesForSms =
allowedCountryCodes && allowedCountryCodes.length > 0
? supportedSmsCountries.filter((c) =>
allowedCountryCodes.includes(c.countryIsoCode as SupportedSmsCountry),
)
: (supportedSmsCountries ?? [
{
countryIsoCode: "US",
countryName: "United States",
phoneNumberCode: 1,
},
]);

Check warning on line 43 in packages/thirdweb/src/react/web/wallets/in-app/CountrySelector.tsx

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L38 - L43 were not covered by tests

return (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,27 @@ describe("InputSelectionUI", () => {

expect(screen.getByRole("combobox")).toHaveValue("US +1");
});

it("should filter countries based on allowedSmsCountryCodes", () => {
const mockGetCountrySelector = vi.mocked(getCountrySelector);
mockGetCountrySelector.mockReturnValue("IN +91");

render(
<InputSelectionUI
onSelect={vi.fn()}
placeholder=""
name=""
type=""
submitButtonText=""
format="phone"
allowedSmsCountryCodes={["IN", "BR"]}
/>,
);

const options = screen.getAllByRole("option");
const optionTexts = options.map((o) => o.textContent);
expect(optionTexts.some((t) => t?.includes("India"))).toBe(true);
expect(optionTexts.some((t) => t?.includes("Brazil"))).toBe(true);
expect(optionTexts.some((t) => t?.includes("United States"))).toBe(false);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,16 @@ export function InputSelectionUI(props: {
format?: "phone";
disabled?: boolean;
defaultSmsCountryCode?: SupportedSmsCountry;
allowedSmsCountryCodes?: SupportedSmsCountry[];
}) {
const [countryCodeInfo, setCountryCodeInfo] = useState(
props.defaultSmsCountryCode
? getCountrySelector(props.defaultSmsCountryCode)
: "US +1",
: props.allowedSmsCountryCodes &&
props.allowedSmsCountryCodes.length > 0 &&
props.allowedSmsCountryCodes[0]
? getCountrySelector(props.allowedSmsCountryCodes[0])
: "US +1",
);
const [input, setInput] = useState("");
const [error, setError] = useState<string | undefined>();
Expand Down Expand Up @@ -69,6 +74,7 @@ export function InputSelectionUI(props: {
<CountrySelector
countryCode={countryCodeInfo}
setCountryCode={setCountryCodeInfo}
allowedCountryCodes={props.allowedSmsCountryCodes}
/>
)}
<Input
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,9 @@
defaultSmsCountryCode={
wallet.getConfig()?.auth?.defaultSmsCountryCode
}
allowedSmsCountryCodes={
wallet.getConfig()?.auth?.allowedSmsCountryCodes

Check warning on line 455 in packages/thirdweb/src/react/web/wallets/shared/ConnectWalletSocialOptions.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/wallets/shared/ConnectWalletSocialOptions.tsx#L454-L455

Added lines #L454 - L455 were not covered by tests
}
/>
) : (
<WalletTypeRowButton
Expand Down
5 changes: 5 additions & 0 deletions packages/thirdweb/src/wallets/ecosystem/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export type EcosystemWalletCreationOptions = {
* The default country code to use for SMS authentication
*/
defaultSmsCountryCode?: SupportedSmsCountry;

/**
* The list of allowed country codes to display in the country selector
*/
allowedSmsCountryCodes?: SupportedSmsCountry[];
};
/**
* The partnerId of the ecosystem wallet to connect to
Expand Down
5 changes: 5 additions & 0 deletions packages/thirdweb/src/wallets/in-app/core/wallet/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ export type InAppWalletCreationOptions =
* The default country code to use for SMS authentication
*/
defaultSmsCountryCode?: SupportedSmsCountry;

/**
* The list of allowed country codes to display in the country selector
*/
allowedSmsCountryCodes?: SupportedSmsCountry[];
};
/**
* Metadata to display in the Connect Modal
Expand Down
Loading