In this article, We will show you how to create AWS Cognito User Pool as well as AWS Cognito Federated Identities and How to use it with Node.JS

Image for post
AWS Cognito Node.JS

Video Tutorial

AWS Cognito Node.JS

What is AWS Cognito

Amazon Cognito lets you add user sign-up, sign-in, and access control to your web and mobile apps quickly and easily. Amazon Cognito scales to millions of users and supports sign-in with social identity providers, such as Facebook, Google, and Amazon, and enterprise identity providers via SAML 2.0.

Let’s understand what is AWS Cognito User Pool and AWS Cognito Federal Identity

  1. AWS Cognito User Pool
  2. AWS Cognito Federated Identities

AWS Cognito User Pool

A User Pool is the user directory that you can configure for your web and mobile apps. A User Pool securely stores your users’ profile attributes. You can create and manage a User Pool using the AWS console, AWS CLI, or AWS SDK.

Create Cognito User Pool

Sign in/up to your AWS account from the Services drop-down menu select Cognito then you will see the following screen.

Click to the Manage User Pools Button

Image for post

Then click on the Create user pool button.

Image for post

Enter Pool name

Image for post

Then select Step through settings

Image for post

Then select choose Email address or phone number

  • Allow email addresses

From the attribute section select email then click on to Next step button

Image for post

Then keep the default settings as shown below in the screenshot and click on the Next step button

Image for post

Then keep the default settings as shown below in the screenshot and click on the Next step button

Image for post

Then keep the default settings as shown below in the screenshot and click on the Next step button

Image for post

Then keep the default settings as shown below in the screenshot and click on the Next step button

Image for post

Then keep the default settings as shown below in the screenshot and click on the Next step button

Image for post

Then enter the app client name as shown below in the screenshot.

Image for post

Then uncheck Generate client secret checkbox shown below in the screenshot and click on the Next step button.

Image for post

It will show the App client preview screen as shown below and click on the Next step button.

Image for post

Then keep the default settings as shown below in the screenshot and click on the Next step button

Image for post

From the Review screen, keep the default settings as shown below in the screenshot and click on the Create pool button.

Image for post

You will see the user pool has been created as shown below in the screenshot. you need to copy Pool Id

Image for post
/* Just for demo */
Pool Id: ap-southeast-1_OYnSpz7Zo

above credential, we will use it in the .env file in Node.JS

Then go to App client settings copy the client ID shown below in the screenshot

Image for post

AWS Cognito Federated Identities

Identity pools are the containers that Cognito Identity uses to keep your apps’ federated identities organized. Identity Pool associates federated identities from social identity providers with a unique user-specific identifier. Identity Pools do not store any user profiles. An identity pool can be associated with one or many apps. If you use two different identity pools for two apps then the same end user will have a different unique identifier in each Identity Pool.

Create Cognito Federated Identities

Click on the Federated identities as shown in the below screenshot

Image for post

Then enter the Identity pool name as shown in the below screenshot

Image for post

Then enter the User Pool Id and App client id as shown in the below screenshot then click on the Create Pool button

Image for post

Then you will see the IAM role screen as shown in the below screenshot then click on the Allow button

Image for post

You need to copy the Identity pool Id as shown in the below screenshot

Image for post

We are done with AWS Cognito User Pool, Cognito Federated Identities setup 🚀🚀🚀🚀🚀🚀

Cognito Node.JS Sign Up

We will use the Node.JS AWS SDK for the Sign Up, Verify, and Sign In

Hope you have already set up the Node.JS environment on your system. If not, Then use the following tutorial to setup

We have created an AWS config file

we will use environment variable from the .env file in Node.JS

Here are the 3 methods we have created

  1. signUp
  2. verify
  3. signIn

Example of main index file for all 3 methods

If you run the following command

node index.js

It will create a new user as shown in the following screenshot

Image for post

6 Digit OTP For Email Verification

Then you will receive the 6 Digit OTP for email verification as shown below in the screenshot

Image for post

Then you need to verify the email using the verify method as shown below

Image for post

Cognito Node.JS Sign In

We have used Sign in method to get the access token, id token, etc. as shown in the following screenshot

Image for post

Written by

We teach & develop for startups

Get the Medium app