Skip to content

Remove phone number validation on the client side #840

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 1, 2017
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
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,6 @@ static void load() {
*/
@Nullable
static String formatPhoneNumber(@NonNull String phoneNumber, @NonNull CountryInfo countryInfo) {

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
return android.telephony.PhoneNumberUtils
.formatNumberToE164(phoneNumber, countryInfo.locale.getCountry());
}
return phoneNumber.startsWith("+")
? phoneNumber
: ("+" + String.valueOf(countryInfo.countryCode)
Expand All @@ -93,6 +88,16 @@ static String formatPhoneNumber(@NonNull String phoneNumber, @NonNull CountryInf
static String formatPhoneNumberUsingCurrentCountry(
@NonNull String phoneNumber, Context context) {
final CountryInfo currentCountry = PhoneNumberUtils.getCurrentCountryInfo(context);

// TODO(samstern): Remove this call once the next version of Play servics is released
// estimated timelime August 10th. It is a known issue that phone numbers
// from the hint selector are susceptible to the false negatives of this
// method.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
return android.telephony.PhoneNumberUtils
.formatNumberToE164(phoneNumber, currentCountry.locale.getCountry());
}

return formatPhoneNumber(phoneNumber, currentCountry);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import static com.firebase.ui.auth.ui.phone.PhoneNumberUtils.getPhoneNumber;
import static com.firebase.ui.auth.ui.phone.PhoneTestConstants.RAW_PHONE;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -83,40 +82,6 @@ public void testGetCountryCode() throws Exception {
assertEquals(null, getCountryCode(new Locale("", "DJJZ").getCountry()));
}

@Test
@Config(constants = BuildConfig.class, sdk = 21)
public void testFormatNumberToE164_aboveApi21() {
String validPhoneNumber = "+919994947354";
CountryInfo indiaCountryInfo = new CountryInfo(new Locale("", "IN"), 91);
//no leading plus
assertEquals(validPhoneNumber, formatPhoneNumber("9994947354", indiaCountryInfo));
//no country code
assertEquals(validPhoneNumber, formatPhoneNumber("919994947354", indiaCountryInfo));
//fully formatted
assertEquals(validPhoneNumber, formatPhoneNumber("+919994947354", indiaCountryInfo));
//with hyphens
assertEquals(validPhoneNumber, formatPhoneNumber("+91-(999)-(49)-(47354)", indiaCountryInfo));
//with spaces leading plus
assertEquals(validPhoneNumber, formatPhoneNumber("+91 99949 47354", indiaCountryInfo));
// space formatting
assertEquals(validPhoneNumber, formatPhoneNumber("91 99949 47354", indiaCountryInfo));
// parantheses and hyphens
assertEquals(validPhoneNumber, formatPhoneNumber("(99949) 47-354", indiaCountryInfo));
// mismatched country
assertEquals(validPhoneNumber, formatPhoneNumber("+919994947354",
new CountryInfo(
new Locale("", "US"), 1)));
// incorrect country with well formatted number
assertNull(formatPhoneNumber("999474735", indiaCountryInfo));

// incorrect country with unformattednumber
assertNull(validPhoneNumber, formatPhoneNumber(
"919994947354", new CountryInfo(new Locale("", "US"), 1)));
//incorrect country, incorrect phone number
assertNull(formatPhoneNumber("+914349873457", new CountryInfo(
new Locale("", "US"), 1)));
}

@Test
@Config(constants = BuildConfig.class, sdk = 16)
public void testFormatNumberToE164_belowApi21() {
Expand Down