Set up an example React single page application
In this tutorial, you'll create a React single page application where you can test user
sign-up, confirmation, and sign-in. React is a JavaScript-based library for web and mobile
apps, with a focus on the user interface (UI). This example application demonstrates some
basic functions of Amazon Cognito user pools. If you're already experienced in web app development with React,
download the example app from GitHub
The following screenshot is of the initial authentication page in the application that you'll create.
![A screenshot of the sign-up page for a React-based example web application.](/images/cognito/latest/developerguide/images/cognito-getting-started-react-app-running.png)
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 build this application, you must set up a developer environment. The developer environment requirements are:
-
Node.js is installed and updated.
-
Node package manager (npm) is installed and updated to at least version 10.2.3.
-
The environment is accessible on TCP port 5173 in a web browser.
To create an example React web application
-
Sign in to your developer environment and navigate to the parent directory for your application.
cd
~/path/to/project/folder/
-
Create a new React service.
npm create vite@latest frontend-client -- --template react-ts
-
Clone the
cognito-developer-guide-react-example
project folderfrom the AWS code examples repository on GitHub. cd
~/some/other/path
git clone https://github.com/awsdocs/aws-doc-sdk-examples.git
cp -r ./aws-doc-sdk-examples/javascriptv3/example_code/cognito-identity-provider/scenarios/cognito-developer-guide-react-example/frontend-client
~/path/to/project/folder/
frontend-client -
Navigate to the
src
directory in your project.cd
~/path/to/project/folder/
frontend-client/src -
Edit
config.json
and replace the following values:-
Replace
YOUR_AWS_REGION
with an AWS Region code. For example:us-east-1
. -
Replace
YOUR_COGNITO_USER_POOL_ID
with the ID of the user pool that you have designated for testing. For example:us-east-1_EXAMPLE
. The user pool must be in the AWS Region that you entered in the previous step. -
Replace
YOUR_COGNITO_APP_CLIENT_ID
with the ID of the app client that you have designated for testing. For example:1example23456789
. The app client must be in the user pool from the previous step.
-
-
If you want to access your example application from an IP other than
localhost
, editpackage.json
and change the line"dev": "vite",
to"dev": "vite --host 0.0.0.0",
. -
Install your application.
npm install
-
Launch the application.
npm run dev
-
Access the application in a web browser at
http://localhost:5173
orhttp://[IP address]:5173
. -
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.
Creating a React developer environment with Amazon Lightsail
A quick way to get started with this application is to create a virtual cloud server with Amazon Lightsail.
With Lightsail, you can quickly create a small server instance that comes preconfigured with the prerequisites for this example application. You can SSH to your instance with a browser-based client, and connect to the web server at a public or private IP address.
To create a Lightsail instance for this example application
-
Go to the Lightsail console
. If prompted, enter your AWS credentials. -
Choose Create instance.
-
For Select a platform, choose Linux/Unix.
-
For Select a blueprint, choose Node.js.
-
Under Identify your instance, give your development environment a friendly name.
-
Choose Create instance.
-
After Lightsail has created your instance, select it and from the Connect tab, choose Connect using SSH.
-
An SSH session opens in a browser window. Run
node -v
andnpm -v
to confirm that your instance was provisioned with Node.js and the minimum npm version of 10.2.3. -
Proceed to configure your React application.