AWS Cognito

AWS Cognito React: The Basics and a Quick Tutorial

How Can AWS Cognito Be Used for React Applications? 

Amazon Cognito is a service designed for user authentication, authorization, and management. It enables developers to add user sign-up, sign-in, and access control to web and mobile apps quickly and securely. 

AWS Cognito integrates smoothly with other AWS services, enhancing security with minimal code changes required. It also supports integration with social identity providers such as Apple, Facebook, Google, and Amazon, along with enterprise identity providers via SAML 2.0.

Cognito’s main components are user pools and identity pools. User pools are user directories that provide sign-up and sign-in options for app users, acting as a complete user database. Identity pools  enable developers to grant authenticated users access to other AWS services. 

Cognito can be used for any application deployed on AWS, including React applications. We’ll provide a step-by-step tutorial showing how to build a React application using Amazon Lightsail and authenticate it with Cognito.

In this article:

What Are User Pools in Amazon Cognito? 

User pools in Amazon Cognito are directories that manage and authenticate users. They provide a secure and scalable user directory that can scale to hundreds of millions of users. Administrators can create customizable sign-up and sign-in processes, enforce strong passwords, and even verify emails and phone numbers.

User pools support a variety of user management features such as authentication, recovery of accounts, and user profile management. Through the use of Lambda triggers, Cognito offers security features like custom authentication flows, which can incorporate captcha or certain knowledge-based authentication questions. 

Tutorial: Building a React Application with Cognito for User Authentication 

Here’s an overview of how to use AWS Cognito to build a React user authentication app. The code is adapted from the official Cognito documentation.

Creating a User Pool

To get started, you need to create a user pool in Amazon Cognito:

  • Login into AWS Console and navigate to the Amazon Cognito Console.

  • Click Create user pool.

  • Choose Create a user pool in the top-right corner to start the user pool creation wizard.
  • In Configure sign-in experience, select User name for sign-in options. Keep all other options as default and click Next.

  • In Configure security requirements, ensure that Password policy mode is set to Cognito defaults. For Multi-factor authentication, choose Optional MFA with Authenticator apps and SMS message. Keep all other options as default and click Next.

  • In Configure security requirements, ensure that Password policy mode is set to Cognito defaults. For Multi-factor authentication, choose Optional MFA with Authenticator apps and SMS message. Keep all other options as default and click Next.

  • In Configure message delivery, select Send email with Cognito for the email provider. For SMS, create a new IAM role with permissions to send SMS messages. Click Next.

  • In Integrate your app, enter a User pool name. Confirm that the App type is set to Public client and Don’t generate a client secret is selected.

  • Under Advanced app client settings add ALLOW_USER_PASSWORD_AUTH to the list of authentication flows. Click Next.

  • Review your settings and click Create user pool.

Once the user pool is created, note your User pool ID and App client ID from the App integration tab.

Creating an Application

To build the React application, ensure your development environment is set up with the following:

  • Node.js installed and updated.
  • Node package manager (npm) updated to at least version 10.2.3.

1. Open your terminal and navigate to your project directory:

cd ~/path/to/project/folder/

2. Create a new React application using Vite:

npm create vite@latest frontend-client — –template react-ts

3. Clone the Cognito example project from 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

4. Navigate to the src directory of your project:

cd ~/path/to/project/folder/frontend-client/src

5. Edit config.json to replace the placeholders with your actual AWS region, user pool ID, and app client ID:

{

    “region”: “us-east-1”,

    “userPoolId”: “us-east-1_xxxxxx8”,

    “clientId”: “5p9bbbbbbbbbbbbb9”

}

6. If you need to access your application from an IP other than localhost, edit package.json:

“scripts”: {

    “dev”: “vite –host 0.0.0.0”

}

7. Install the application dependencies:

npm install

8. Launch the application:

npm run dev

9. Open your web browser and access the application at http://localhost:5173 (or the appropriate IP address).

Building a React Developer Environment Using Amazon Lightsail

Amazon Lightsail provides an easy way to create a virtual server for your development environment:

  • Click Create instance.

  • Choose Linux/Unix as the platform and Node.js as the blueprint.
  • Name your instance and click Create instance.

You should see something like this:

  • Once the instance is created, select it and use the Connect using SSH option to open an SSH session.
  • Verify the Node.js and npm installations:

node -v

npm -v

7. Configure your React application as described in the previous section.

By following these steps, you can create a fully functional React application integrated with AWS Cognito for user authentication.

Related content: Read our guide to AWS Cognito tutorial (coming soon)

Amazon Cognito Limitations 

When using Cognito for your React applications, you should be aware of the following limitations, reported by users on the G2 platform.

Complexity of Configuration and Setup

The initial setup can be complicated, especially for those with limited technical expertise. Customizing user flows and configuring advanced functionalities often require a deep understanding of AWS services and Cognito’s architecture. Users must navigate various components, configurations, and options. 

Token Expiration Management

The default token expiration settings might not be suitable for all use cases, leading to frequent token renewals and potential user frustration. Although it is possible to adjust token expiration times, the process is not straightforward and requires a good understanding of Cognito’s configuration options. 

Limited Customization of Login Screens

While Cognito provides basic templates for login screens, these templates are not highly customizable, restricting the ability of developers to fully brand and tailor the user experience. The SDK allows for some degree of customization, but it falls short in offering complete control over the authentication process, such as defining specific scopes during login. 

Documentation Quality

Many users find that the documentation lacks sufficient detail, particularly when dealing with advanced features and integrations. For example, integrating multiple identity providers with a single Cognito account can be confusing due to the sparse and sometimes unclear instructions. This often forces users to rely on community forums or external resources for help. 

Pricing

Pricing for Cognito starts at $0.01 per monthly active user (MAU) and can quickly become costly. Some users have complained about Amazon’s price hike, suggesting that the service doesn’t justify the price, or that other solutions may offer a better ROI. Amazon doesn’t offer a free trial for Cognito.

User Authentication and Authorization with Frontegg

The industry standard today is to use Authentication providers to “build the door”, but what about Authorization (the door knob)? Most authentication vendors don’t go that extra mile, forcing SaaS vendors to invest in expensive in-house development. This often delays investment in core technology development, which negatively impacts innovation and time-to-market (TTM) metrics. 

Frontegg’s end-to-end user management platform allows you to authenticate and authorize users with just a few clicks. Integration takes just a few minutes, thanks to it’s plug-and-play nature. It’s also multi-tenant by design. 

Start for Free

Looking to take your User Management to the next level?

Sign up. It's free