-
Notifications
You must be signed in to change notification settings - Fork 258
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
Fix/authenticator/phone validator #5518
Conversation
…e number against this map
packages/authenticator/amplify_authenticator/lib/src/mixins/authenticator_phone_field.dart
Show resolved
Hide resolved
if (phoneNumber.startsWith(prefix.substring(1))) { | ||
if (countryPhoneNumberLengths.containsKey(prefix) && | ||
phoneNumber.length > countryPhoneNumberLengths[prefix]!) { | ||
phoneNumber = phoneNumber.substring(prefix.length - 1); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
refactor: Can this be rewritten to avoid nested if statements in favor of guard clauses? Something like improves readability:
if (!phoneNumber.startsWith(prefix.substring(1))) {
return phoneNumber;
}
final prefixLength = countryPhoneNumberLengths[prefix];
if (prefixLength == null || phoneNumber.length <= prefixLength) {
return phoneNumber;
}
return phoneNumber.substring(prefix.length - 1);
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
/// A list of [ISO 3166-1](https://en.wikipedia.org/wiki/ISO_3166-1) dial codes mapped to the maximum [possible length](https://en.wikipedia.org/wiki/National_conventions_for_writing_telephone_numbers) of the phone number from that country as an integer | ||
/// | ||
/// |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Q: I assume this is coming from libphonenumber
... Can you ensure this is has the proper license & attribution for us to use?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes this comes by scraping their metadata, it looks like they have an Apache 2.0 License. Is that ok for us to use?
testWidgets( | ||
'displays message when submitted with empty phone number if the field is required', | ||
(tester) async { | ||
await tester.pumpWidget( | ||
MockAuthenticatorApp( | ||
initialStep: AuthenticatorStep.signUp, | ||
signUpForm: SignUpForm.custom( | ||
fields: [ | ||
SignUpFormField.username(), | ||
SignUpFormField.phoneNumber(required: true), | ||
SignUpFormField.password(), | ||
], | ||
), | ||
), | ||
); | ||
await tester.pumpAndSettle(); | ||
|
||
final signUpPage = SignUpPage(tester: tester); | ||
|
||
await signUpPage.submitSignUp(); | ||
|
||
await tester.pumpAndSettle(); | ||
|
||
Finder findPhoneFieldError() => find.descendant( | ||
of: signUpPage.phoneField, | ||
matching: find.text('Phone Number field must not be blank.'), | ||
); | ||
|
||
expect(findPhoneFieldError(), findsOneWidget); | ||
|
||
await signUpPage.enterPhoneNumber('1235556789'); | ||
|
||
await signUpPage.submitSignUp(); | ||
|
||
expect(findPhoneFieldError(), findsNothing); | ||
}, | ||
); | ||
testWidgets( | ||
'displays message when submitted with empty phone number if the field is required', | ||
(tester) async { | ||
await tester.pumpWidget( | ||
MockAuthenticatorApp( | ||
initialStep: AuthenticatorStep.signUp, | ||
signUpForm: SignUpForm.custom( | ||
fields: [ | ||
SignUpFormField.username(), | ||
SignUpFormField.phoneNumber(required: true), | ||
SignUpFormField.password(), | ||
], | ||
), | ||
), | ||
); | ||
await tester.pumpAndSettle(); | ||
|
||
final signUpPage = SignUpPage(tester: tester); | ||
|
||
await signUpPage.submitSignUp(); | ||
|
||
await tester.pumpAndSettle(); | ||
|
||
Finder findPhoneFieldError() => find.descendant( | ||
of: signUpPage.phoneField, | ||
matching: find.text('Phone Number field must not be blank.'), | ||
); | ||
|
||
expect(findPhoneFieldError(), findsOneWidget); | ||
|
||
await signUpPage.enterPhoneNumber('1235556789'); | ||
|
||
await signUpPage.submitSignUp(); | ||
|
||
expect(findPhoneFieldError(), findsNothing); | ||
}, | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Q: These tests appear to be identical, was this an accident? It would be nice to see the use case you're resolved tested here instead.
* chore(common,notifications): update os_detect to version 2.0.2
…5567) * fix(api): Reconnect WebSocket when resuming app from a paused state
chore(version): Bump version ### Features - feat(api): move App Sync subscription headers to protocol ([#5301](#5301)) - feat(authenticator): export unmet password requirements ([#5437](#5437)) ### Fixes - fix(api): Reconnect WebSocket when resuming app from a paused state ([#5567](#5567)) Updated-Components: amplify_lints, Amplify Flutter, Amplify Dart, Amplify UI, DB Common, Secure Storage, AWS Common, Smithy, Worker Bee
### Features - feat(auth, authenticator): Add support for Email OTP MFA ([#5449](#5449)) (#5472) ### Fixes - fix(common): db_common windows sqlite3 collision ([#5481](#5481)) Updated-Components: amplify_lints, Amplify Flutter, Amplify Dart, Amplify UI, DB Common, Secure Storage, AWS Common, Smithy, Worker Bee
…e number against this map
…m/aws-amplify/amplify-flutter into fix/authenticator/phone-validator
Issue #, if available: #4079
Description of changes:
This is done in the exact same way as #4106 from November last year. This fix was correct for the main issue in the original issue however it didn't address being able to re-input a dial code and still have it pass through validation
Using a map of dial codes to their countries' maximum length of phone number (generated from this open source repo), we will check if the inputted phone number begins with the selected dial code, if it does, then check if it surpasses the length of number in that country (since it could be the case that the number naturally begins with the dial code), if this is also true then ignore the repeat dial code and take the leftover number as the phone number
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.