Exploring AWS Cognito: User Authentication and Authorization Made Easy

Christopher Adamson
7 min readNov 9, 2023

--

AWS Cognito is a service that makes it easy to add user sign-up, sign-in, and access control to web and mobile apps. With Cognito, you don’t have to write any backend code to handle user authentication. Instead, Cognito uses federated identities from social providers like Facebook, Google, and Amazon to authenticate users. Here’s an overview of how AWS Cognito works and its key benefits:

Setting Up Cognito

To get started with Cognito, you first create a user pool, which is a secure user directory that stores user data and handles sign-up, sign-in, and recovery functions. You can then create app clients that make requests to the user pool to authenticate users. Cognito handles all the authentication workflows for you behind the scenes.

Cognito also supports using identity providers like Facebook and Google to authenticate users through federated identities. This means users can sign in with their existing social accounts instead of creating a new account just for your app.

Managing Users

Once a user pool is configured, you can sign up users either within your app UI or directly with the AWS CLI/SDK. Cognito handles password storage using secure hashing. For added security, Cognito also supports multi-factor authentication with SMS and TOTP.

To manage users, you can use the Cognito dashboard or AWS CLI to view, search, and delete user accounts. You can also integrate Cognito with AWS Lambda triggers to customize user workflows like sign-up verification.

Accessing User Data

To access user data from your backend, you can use the AWS SDKs to call Cognito and retrieve user attributes, statuses, and group information. You can store custom app data in Cognito’s user attributes and keep it in sync across devices.

Controlling Access

A key aspect of Cognito is providing role-based access control for your app’s backend resources in AWS. Cognito lets you define IAM roles that you can associate with users as they are authenticated.

For example, you can assign pay-per-use customers an “Unauthenticated” role by default but authenticated users a “User” role that grants additional privileges. Cognito seamlessly handles providing temporary AWS credentials to your client apps.

Recap

Here are some of the major benefits of using AWS Cognito:

  • Simple to set up and deploy — no backend coding required
  • Secure storage and encryption of user data
  • Support for federated identities and social sign-in
  • User management capabilities like multi-factor authentication
  • Role-based access control for AWS resources
  • SDKs available for easy integration with apps
  • Scales to millions of users automatically

AWS Cognito eliminates a lot of the hassle of handling user management tasks like authentication, account recovery, monitoring, and data security. It’s a robust but flexible solution for adding login functionality to mobile and web apps on AWS.

Tutorial

AWS Cognito provides a simple way to add user sign-up, sign-in, and access control to your web or mobile app. With Cognito, you don’t need to write backend code to authenticate users. Instead, Cognito handles these tasks using identity providers like Facebook or your own user directory.

In this tutorial, we’ll walk through how to set up Cognito for a simple web app. We’ll perform these steps:

  • Create a Cognito user pool
  • Add sign-up and sign-in pages
  • Verify email addresses and phone numbers
  • Assign IAM roles to authenticated users
  • Access AWS resources from the app

Prerequisites

  • An AWS account
  • A sample web app codebase

Step 1 — Create a User Pool

First, log into the AWS Management Console and go to the Cognito dashboard. Click “Manage User Pools” and create a new user pool.

Give your pool a name like `mywebapp-user-pool` and uncheck “Email address or phone number” under Attributes. We’ll add those later.

Under Policies, choose a password strength requirement. Enable multi-factor authentication (MFA).

Step 2 — Add App Client

Next, go to App clients and add a new app client. Give it a name like `mywebapp`. Under Authentication Flows, enable USERNAME-PASSWORD-OTP. This allows username/password login plus SMS MFA.

Take note of the App client id — we’ll need this later.

Step 3 — Sign-up and Sign-in Pages

Now let’s add sign-up and sign-in pages to our web app. We’ll use the AWS Amplify library which integrates nicely with Cognito.

First, install Amplify:

Import the Authenticator component in your React app:

Configure Amplify with our user pool details:

Add the Authenticator component which will handle signup/signin:

That’s it! The Authenticator component will render the appropriate sign-in and sign-up forms.

Step 4 — Verify Email and Phone Numbers

For additional security, you can require users to verify their email and phone number before they can sign in.

In Cognito, under Message customizations, enable email and SMS verification.

Users will now receive a verification code when they sign up. You can further customize these emails and SMS messages in Cognito.

Step 5 — Assign IAM Roles

To grant users access to backend AWS resources, we need to define IAM roles.

In the AWS IAM console, create a role called `CognitoAuthenticatedUser` with basic access.

Attach a policy to this role like AmazonS3ReadOnlyAccess to grant object viewing permissions.

Back in Cognito, under App integration > App client settings, select the role you created for authenticated users.

When users are authenticated by Cognito, they will be assigned this role and gain the permissions granted by it.

Step 6 — Access AWS Resources

With IAM roles configured, we can now access AWS resources from our authenticated web app.

The Amplify API category makes it easy to call AWS services like API Gateway and S3.

For example, to list S3 buckets:

The user’s short-lived IAM credentials will be automatically used to authorize access to these resources.

That’s it! With these steps, you have user authentication set up for your web app using AWS Cognito and can securely access AWS resources.

Recap

  • Cognito handles user signup, authentication, account recovery, and security.
  • Use the Amplify libraries to easily integrate Cognito into your apps.
  • Verify emails and phone numbers for better security.
  • Use IAM roles to control access to AWS resources from your app.

For more details on working with AWS Cognito, check out the full documentation: https://docs.aws.amazon.com/cognito/latest/developerguide/what-is-amazon-cognito.html.

AWS CLI commands for Cognito

Here are some common AWS CLI commands for working with AWS Cognito:

Create a Cognito user pool

List existing user pools

Create a Cognito app client

List app clients

Sign up a user

Confirm user signup

Admin get user details

List groups

Admin add user to group

Conclusion

AWS Cognito provides a robust and fully-managed authentication service that makes it easy to add sign-up, sign-in, and access control to your web and mobile apps. With Cognito, you can avoid having to build and maintain your own user directory and authentication logic. Users can sign in through social identity providers like Facebook and Google, or with their own username and password. You can also customize authentication workflows with multi-factor authentication, email and SMS verification, and more.

Once users are authenticated, Cognito seamlessly handles providing temporary credentials to access other AWS resources like S3 or DynamoDB. Overall, AWS Cognito is an essential tool for handling identity management tasks and enabling secure access to cloud or mobile apps for your users. With the AWS Amplify libraries, you can easily integrate Cognito into your frontend apps and start building full-stack solutions on AWS.

--

--