Set up an example Android app with Flutter
In this tutorial, you'll create a mobile application in Android Studio where you can
emulate a device and test user sign-up, confirmation, and sign-in. This example application
creates a basic Amazon Cognito user pools mobile client for Android in Flutter. If you're already experienced in
mobile app development with Flutter, download the example app from GitHub
The following screenshot shows the app running on a virtual Android device.
The Create a user pool procedure sets you up with a user pool that works with the example application. You can skip this step if you have a user pool that meets the following requirements:
-
Users can sign in with their email address. Cognito user pool sign-in options: Email.
-
Usernames are case insensitive. User name requirements: Make user name case sensitive is not selected.
-
Multi-factor authentication (MFA) isn't required. MFA enforcement: Optional MFA.
-
Your user pool verifies attributes for user-profile confirmation with an email message. Attributes to verify: Send email message, verify email address.
-
Email is the only required attribute. Required attributes: email.
-
Users can sign themselves up in your user pool. Self-registration: Enable self-registration is selected.
-
Your initial app client is a public client that permits sign-in with username and password. App type: Public client, Authentication flows:
ALLOW_USER_PASSWORD_AUTH
.
Create a new user pool
-
Go to the Amazon Cognito console
. If prompted, enter your AWS credentials. -
Choose the Create user pool button. You might be required to select User Pools from the left navigation pane to reveal this option.
-
In the top-right corner of the page, choose Create a user pool to start the user pool creation wizard.
-
In Configure sign-in experience, you can choose the identity providers (IdPs) that you will use with this user pool. For more information, see Adding user pool sign-in through a third party.
-
Under Authentication providers, for Provider types, ensure that only Cognito user pool is selected.
-
For Cognito user pool sign-in options, choose User name. Don't select any additional User name requirements.
-
Keep all other options as the default and choose Next.
-
-
In Configure security requirements, you can choose your password policy, multi-factor authentication (MFA) requirements, and user account recovery options. For more information, see Using Amazon Cognito user pools security features.
-
For Password policy, confirm that Password policy mode is set to Cognito defaults.
-
Under Multi-factor authentication, for MFA enforcement, choose Optional MFA.
-
For MFA methods, choose Authenticator apps and SMS message.
-
For User account recovery, confirm that Enable self-service account recovery is selected, and that the user account recovery message delivery method is set to Email only.
-
Keep all other options as the default and choose Next.
-
-
In Configure sign-up experience, you can determine how new users will verify their identities when signing up as a new user, and which attributes should be required or optional during the user sign-up flow. For more information, see Managing users in your user pool.
-
Confirm that Enable self-registration is selected. This setting opens your user pool to sign-up from anyone on the internet. This is intended for the purposes of the example application, but apply this setting with caution in production environments.
-
Under Cognito-assisted verification and confirmation, verify that the Allow Cognito to automatically send messages to verify and confirm check box is selected.
-
Confirm that Attributes to verify is set to Send email message, verify email address.
-
Under Verifying attribute changes, confirm that the default options are selected: Keep original attribute value when an update is pending is selected, and Active attribute values when an update is pending is set to Email address.
-
Under Required attributes, confirm that Required attributes based on previous selections displays email.
Important
For this example application, your user pool must not set phone_number as a required attribute. If phone_number is shown as a required attribute, review and update your earlier choices:
-
Optional MFA, Email only for Delivery method for user account recovery messages
-
Send email message, verify email address for Attributes to verify
-
-
Keep all other options as the default and choose Next.
-
-
In Configure message delivery, you can configure integration with Amazon Simple Email Service and Amazon Simple Notification Service to send email and SMS messages to your users for sign-up, account confirmation, MFA, and account recovery. For more information, see Email settings for Amazon Cognito user pools and SMS message settings for Amazon Cognito user pools.
-
For Email provider, choose Send email with Cognito, and use the default email sender provided by Amazon Cognito. This setting for low email volume is sufficient for application testing. You can return after you verify an email address with Amazon Simple Email Service (Amazon SES) and choose Send email with Amazon SES.
-
For SMS, select Create a new IAM role and enter an IAM role name. This creates a role that grants permissions to Amazon Cognito to send SMS messages.
-
Keep all other options as the default and choose Next.
-
-
In Integrate your app, you can name your user pool, configure the hosted UI, and create an app client. For more information, see Add an app client with the hosted UI. The example applications don't use the hosted UI.
-
Under User pool name, enter a User pool name.
-
Don’t select Use the Cognito hosted UI.
-
Under Initial app client, confirm that App type is set to Public client.
-
Under Client secret, confirm that Don’t generate a client secret is selected.
-
Enter an App client name.
-
Expand Advanced app client settings. Add
ALLOW_USER_PASSWORD_AUTH
to the list of Authentication flows. -
Keep all other options as the default and choose Next.
-
-
Review your choices in the Review and create screen and modify any selections as needed. When you're satisfied with your user pool configuration, choose Create user pool to proceed.
-
From the User pools page, choose your new user pool.
-
Under User pool overview, note your User pool ID. You'll provide this string when you create your example application.
-
Choose the App integration tab and locate the App clients and analytics section. Select your new app client. Note your Client ID.
Related resources
Create an application
To create an example Android app
-
Install Android studio
and command-line tools . -
In Android Studio, install the Flutter plugin
. -
Create a new Android Studio project from the contents of the
cognito_flutter_mobile_app
directory in this example app. -
Edit
assets/config.json
and replace<<YOUR USER POOL ID>>
and<< YOUR CLIENT ID>>
with the IDs of the user pool and app client that you created earlier.
-
-
Install Flutter
. -
Add Flutter to your PATH variable.
-
Accept licenses with the following command.
flutter doctor --android-licenses
-
Verify your Flutter environment and install any missing components.
flutter doctor
-
If any components are missing, run
flutter doctor -v
to learn how to fix the issue.
-
-
Change to the directory of your new Flutter project and install dependencies.
-
Run
flutter pub add amazon_cognito_identity_dart_2
.
-
-
Run
flutter pub add flutter_secure_storage
.
-
-
Create a virtual Android device.
-
In the Android studio GUI, create a new device with the device manager
. -
In the CLI, run
flutter emulators --create --name android-device
.
-
-
Launch your virtual Android device.
-
In the Android Studio GUI, select the start icon next to your virtual device.
-
In the CLI, run
flutter emulators --launch android-device
.
-
-
Launch your app on your virtual device.
-
In the Android Studio GUI, select the deploy icon.
-
In the CLI, run
flutter run
.
-
-
Navigate to your running virtual device in Android Studio.
-
Sign up a new user with a valid email address.
-
Retrieve the confirmation code from your email message. Enter the confirmation code into the application.
-
Sign in with your username and password.