-
Notifications
You must be signed in to change notification settings - Fork 249
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(amplify_authenticator): authenticator phone OR email confirmation #1785
Changes from 4 commits
0f377f0
f5fc46d
4fbba55
7a59686
72261b1
27533fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,165 @@ | ||||||||||||||
// Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||||||||||||||
// | ||||||||||||||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||||||
// you may not use this file except in compliance with the License. | ||||||||||||||
// You may obtain a copy of the License at | ||||||||||||||
// | ||||||||||||||
// http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||
// | ||||||||||||||
// Unless required by applicable law or agreed to in writing, software | ||||||||||||||
// distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||||
// See the License for the specific language governing permissions and | ||||||||||||||
// limitations under the License. | ||||||||||||||
|
||||||||||||||
import 'dart:io'; | ||||||||||||||
|
||||||||||||||
import 'package:amplify_api/amplify_api.dart'; | ||||||||||||||
import 'package:amplify_authenticator/amplify_authenticator.dart'; | ||||||||||||||
import 'package:amplify_flutter/amplify_flutter.dart'; | ||||||||||||||
import 'package:amplify_test/amplify_test.dart'; | ||||||||||||||
import 'package:flutter/foundation.dart'; | ||||||||||||||
import 'package:flutter/material.dart'; | ||||||||||||||
import 'package:flutter_test/flutter_test.dart'; | ||||||||||||||
import 'package:integration_test/integration_test.dart'; | ||||||||||||||
|
||||||||||||||
import 'config.dart'; | ||||||||||||||
import 'pages/confirm_sign_up_page.dart'; | ||||||||||||||
import 'pages/sign_in_page.dart'; | ||||||||||||||
import 'pages/sign_up_page.dart'; | ||||||||||||||
import 'pages/test_utils.dart'; | ||||||||||||||
import 'utils/mock_data.dart'; | ||||||||||||||
|
||||||||||||||
void main() { | ||||||||||||||
final binding = IntegrationTestWidgetsFlutterBinding.ensureInitialized(); | ||||||||||||||
// resolves issue on iOS. See: https://github.com/flutter/flutter/issues/89651 | ||||||||||||||
binding.deferFirstFrame(); | ||||||||||||||
|
||||||||||||||
final isMobile = !kIsWeb && (Platform.isIOS || Platform.isAndroid); | ||||||||||||||
|
||||||||||||||
final authenticator = Authenticator( | ||||||||||||||
child: MaterialApp( | ||||||||||||||
builder: Authenticator.builder(), | ||||||||||||||
home: const Scaffold( | ||||||||||||||
body: Center( | ||||||||||||||
child: SignOutButton(), | ||||||||||||||
), | ||||||||||||||
), | ||||||||||||||
), | ||||||||||||||
); | ||||||||||||||
|
||||||||||||||
group('confirm-sign-up', () { | ||||||||||||||
// Given I'm running the example with an "Email or Phone" config | ||||||||||||||
setUpAll(() async { | ||||||||||||||
await loadConfiguration( | ||||||||||||||
'email-or-phone', | ||||||||||||||
additionalConfigs: isMobile ? [AmplifyAPI()] : null, | ||||||||||||||
); | ||||||||||||||
}); | ||||||||||||||
|
||||||||||||||
setUp(signOut); | ||||||||||||||
|
||||||||||||||
tearDown(Amplify.Auth.deleteUser); | ||||||||||||||
|
||||||||||||||
// Scenario: Sign up & confirm account with email as username | ||||||||||||||
testWidgets( | ||||||||||||||
'Sign up & confirm account with email as username', | ||||||||||||||
(tester) async { | ||||||||||||||
SignUpPage signUpPage = SignUpPage(tester: tester); | ||||||||||||||
ConfirmSignUpPage confirmSignUpPage = ConfirmSignUpPage(tester: tester); | ||||||||||||||
SignInPage signInPage = SignInPage(tester: tester); | ||||||||||||||
|
||||||||||||||
await loadAuthenticator(tester: tester, authenticator: authenticator); | ||||||||||||||
|
||||||||||||||
final email = generateEmail(); | ||||||||||||||
final phoneNumber = generateUSPhoneNumber(); | ||||||||||||||
final password = generatePassword(); | ||||||||||||||
|
||||||||||||||
final code = getOtpCode(email); | ||||||||||||||
|
||||||||||||||
await signInPage.navigateToSignUp(); | ||||||||||||||
|
||||||||||||||
// When I select email as a username | ||||||||||||||
await signUpPage.selectEmail(); | ||||||||||||||
|
||||||||||||||
// And I type my email address as a username | ||||||||||||||
await signUpPage.enterUsername(email); | ||||||||||||||
|
||||||||||||||
// And I type my password | ||||||||||||||
await signUpPage.enterPassword(password); | ||||||||||||||
|
||||||||||||||
// And I confirm my password | ||||||||||||||
await signUpPage.enterPasswordConfirmation(password); | ||||||||||||||
|
||||||||||||||
// And I enter my phone number | ||||||||||||||
await signUpPage.enterPhoneNumber(phoneNumber.withOutCountryCode()); | ||||||||||||||
|
||||||||||||||
// And I click the "Create Account" button | ||||||||||||||
await signUpPage.submitSignUp(); | ||||||||||||||
|
||||||||||||||
// And I see "Confirmation Code" | ||||||||||||||
confirmSignUpPage.expectConfirmationCodeIsPresent(); | ||||||||||||||
|
||||||||||||||
// And I type a valid confirmation code | ||||||||||||||
await confirmSignUpPage.enterCode(await code); | ||||||||||||||
|
||||||||||||||
// And I click the "Confirm" button | ||||||||||||||
await confirmSignUpPage.submitConfirmSignUp(); | ||||||||||||||
|
||||||||||||||
// Then I see "Sign out" | ||||||||||||||
await signInPage.expectAuthenticated(); | ||||||||||||||
}, | ||||||||||||||
skip: !isMobile, | ||||||||||||||
); | ||||||||||||||
|
||||||||||||||
testWidgets( | ||||||||||||||
'Sign up & confirm account with phone number as username', | ||||||||||||||
(tester) async { | ||||||||||||||
SignUpPage signUpPage = SignUpPage(tester: tester); | ||||||||||||||
ConfirmSignUpPage confirmSignUpPage = ConfirmSignUpPage(tester: tester); | ||||||||||||||
SignInPage signInPage = SignInPage(tester: tester); | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
|
||||||||||||||
await loadAuthenticator(tester: tester, authenticator: authenticator); | ||||||||||||||
|
||||||||||||||
final email = generateEmail(); | ||||||||||||||
final phoneNumber = generateUSPhoneNumber(); | ||||||||||||||
final password = generatePassword(); | ||||||||||||||
|
||||||||||||||
final code = getOtpCode(email); | ||||||||||||||
|
||||||||||||||
await signInPage.navigateToSignUp(); | ||||||||||||||
|
||||||||||||||
// When I select phone number as a username | ||||||||||||||
await signUpPage.selectPhone(); | ||||||||||||||
|
||||||||||||||
// And I type my phone number as a username | ||||||||||||||
await signUpPage.enterUsername(phoneNumber.withOutCountryCode()); | ||||||||||||||
|
||||||||||||||
// And I type my password | ||||||||||||||
await signUpPage.enterPassword(password); | ||||||||||||||
|
||||||||||||||
// And I confirm my password | ||||||||||||||
await signUpPage.enterPasswordConfirmation(password); | ||||||||||||||
|
||||||||||||||
// And I enter my email address | ||||||||||||||
await signUpPage.enterEmail(email); | ||||||||||||||
|
||||||||||||||
// And I click the "Create Account" button | ||||||||||||||
await signUpPage.submitSignUp(); | ||||||||||||||
|
||||||||||||||
// And I see "Confirmation Code" | ||||||||||||||
confirmSignUpPage.expectConfirmationCodeIsPresent(); | ||||||||||||||
|
||||||||||||||
// And I type a valid confirmation code | ||||||||||||||
await confirmSignUpPage.enterCode(await code); | ||||||||||||||
|
||||||||||||||
// And I click the "Confirm" button | ||||||||||||||
await confirmSignUpPage.submitConfirmSignUp(); | ||||||||||||||
|
||||||||||||||
// Then I see "Sign out" | ||||||||||||||
await signInPage.expectAuthenticated(); | ||||||||||||||
}, | ||||||||||||||
skip: !isMobile, | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can skip on the group |
||||||||||||||
); | ||||||||||||||
}); | ||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
const amplifyconfig = ''' { | ||
"UserAgent": "aws-amplify-cli/2.0", | ||
"Version": "1.0" | ||
}'''; |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -14,8 +14,10 @@ | |||||
*/ | ||||||
|
||||||
import 'package:amplify_auth_cognito/amplify_auth_cognito.dart'; | ||||||
import 'package:amplify_authenticator/src/keys.dart'; | ||||||
import 'package:amplify_authenticator/src/l10n/auth_strings_resolver.dart'; | ||||||
import 'package:amplify_authenticator/src/models/username_input.dart'; | ||||||
import 'package:amplify_authenticator/src/state/authenticator_state.dart'; | ||||||
import 'package:amplify_authenticator/src/utils/validators.dart'; | ||||||
import 'package:amplify_authenticator/src/widgets/component.dart'; | ||||||
import 'package:amplify_authenticator/src/widgets/form.dart'; | ||||||
|
@@ -129,23 +131,33 @@ mixin AuthenticatorUsernameField<FieldType, | |||||
return ToggleButtons( | ||||||
borderWidth: buttonBorderWidth, | ||||||
constraints: buttonConstraints, | ||||||
isSelected: [useEmail.value, !useEmail.value], | ||||||
isSelected: [ | ||||||
state.usernameSelection == UsernameSelection.email, | ||||||
state.usernameSelection == UsernameSelection.phoneNumber, | ||||||
], | ||||||
onPressed: (int index) { | ||||||
bool useEmail = index == 0; | ||||||
setState(() { | ||||||
this.useEmail.value = useEmail; | ||||||
}); | ||||||
final newUsernameSelection = index == 0 | ||||||
? UsernameSelection.email | ||||||
: UsernameSelection.phoneNumber; | ||||||
final oldUsernameSelection = state.usernameSelection; | ||||||
if (oldUsernameSelection == newUsernameSelection) { | ||||||
return; | ||||||
} | ||||||
state.usernameSelection = newUsernameSelection; | ||||||
// Reset current username value to align with the current switch state. | ||||||
String newUsername = useEmail | ||||||
String newUsername = newUsernameSelection == | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
UsernameSelection.email | ||||||
? state.getAttribute(CognitoUserAttributeKey.email) ?? '' | ||||||
: state.getAttribute( | ||||||
CognitoUserAttributeKey.phoneNumber) ?? | ||||||
''; | ||||||
state.username = newUsername; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since |
||||||
// Clear attributes on switch | ||||||
state.authAttributes.clear(); | ||||||
Jordan-Nelson marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
}, | ||||||
children: [ | ||||||
Text(emailTitle), | ||||||
Text(phoneNumberTitle), | ||||||
Text(emailTitle, key: keyEmailUsernameToggleButton), | ||||||
Text(phoneNumberTitle, key: keyPhoneUsernameToggleButton), | ||||||
], | ||||||
); | ||||||
}), | ||||||
|
@@ -219,7 +231,7 @@ mixin AuthenticatorUsernameField<FieldType, | |||||
validator: _validator, | ||||||
enabled: enabled, | ||||||
errorMaxLines: errorMaxLines, | ||||||
initialValue: state.getAttribute(CognitoUserAttributeKey.phoneNumber), | ||||||
initialValue: state.username, | ||||||
); | ||||||
} | ||||||
return TextFormField( | ||||||
|
@@ -253,9 +265,6 @@ mixin UsernameAttributes<T extends AuthenticatorForm> | |||||
return <CognitoUserAttributeKey>{...?authConfig?.usernameAttributes}; | ||||||
}(); | ||||||
|
||||||
/// Toggle value for the email or phone number case. | ||||||
final ValueNotifier<bool> useEmail = ValueNotifier(true); | ||||||
|
||||||
UsernameConfigType get usernameType { | ||||||
if (usernameAttributes.isEmpty) { | ||||||
return UsernameConfigType.username; | ||||||
|
@@ -283,7 +292,7 @@ mixin UsernameAttributes<T extends AuthenticatorForm> | |||||
case UsernameConfigType.phoneNumber: | ||||||
return UsernameType.phoneNumber; | ||||||
case UsernameConfigType.emailOrPhoneNumber: | ||||||
if (useEmail.value) { | ||||||
if (state.usernameSelection == UsernameSelection.email) { | ||||||
return UsernameType.email; | ||||||
} | ||||||
return UsernameType.phoneNumber; | ||||||
|
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.