Cognito JWT Node.JS
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

Video Tutorial
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
- AWS Cognito User Pool
- 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

Then click on the Create user pool button.

Enter Pool name

Then select Step through settings

Then select choose Email address or phone number
- Allow email addresses
From the attribute section select email then click on to Next step button

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

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

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

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

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

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

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

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

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

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

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

/* 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

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

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

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

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

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

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
- signUp
- verify
- 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

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

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

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
