Apps Development, AWS, Cloud Computing

3 Mins Read

AWS Cognito with React JS (Session and Logout) – Part 4

Voiced by Amazon Polly

Overview

In my previous blog, we learned how to verify the user in the Cognito user pool and how to log in the user in our application using ‘amazon-cognito-identity-js’. In this blog, we will learn how to manage the state or session of a logged-in user and how to log out the user from the application using the ‘amazon-cognito-identity-js’ library.

Freedom Month Sale — Upgrade Your Skills, Save Big!

  • Up to 80% OFF AWS Courses
  • Up to 30% OFF Microsoft Certs
Act Fast!

Introduction to 'amazon-cognito-identity-js'

‘amazon-cognito-identity-js’ is the SDK that allows a JavaScript-enabled application to authenticate the users, view user details, delete users, update users and register the users. It also allows password change for authenticated users and forgot password functionality.

Steps to Create a Component

Step 1: Create an Account.js file in src/Components directory and follow the code snippet in the file.

 This code snippet is for Central session management. We can use it in every component. In this component, we will get the current session information using getCurrentUser method in getSession function. And in authenticating function, we will authenticate the user. And Logout function is to log out the user from the application.

Step 2: Create a ‘Status.js’ file in src/components directory

This code snippet is to get the user’s current session using the getSession function which we created in the last step. And on the UI, we are displaying the current status of the user using HTML code.

Step 3: Modify the App.js

 This code snippet is for wrapping the whole application with the Account component to use all the function of the Account component in every component of the application.

<Status /> is to display the current stage of the user whether it is logged in or not.

Step 4: Modify the Login.js file

In this step, we are modifying the login page and will call authenticate method of the Account component to log in.

Conclusion

In this blog, we learn how to manage the state or session of logged in users and how to log out the user from the application using the ‘amazon-cognito-identity-js’ library.

Source Code

In my next blog, we will see how to implement change password functionality in our application. 

 

Freedom Month Sale — Discounts That Set You Free!

  • Up to 80% OFF AWS Courses
  • Up to 30% OFF Microsoft Certs
Act Fast!

About CloudThat

CloudThat is an award-winning company and the first in India to offer cloud training and consulting services worldwide. As a Microsoft Solutions Partner, AWS Advanced Tier Training Partner, and Google Cloud Platform Partner, CloudThat has empowered over 850,000 professionals through 600+ cloud certifications winning global recognition for its training excellence including 20 MCT Trainers in Microsoft’s Global Top 100 and an impressive 12 awards in the last 8 years. CloudThat specializes in Cloud Migration, Data Platforms, DevOps, IoT, and cutting-edge technologies like Gen AI & AI/ML. It has delivered over 500 consulting projects for 250+ organizations in 30+ countries as it continues to empower professionals and enterprises to thrive in the digital-first world.

FAQs

1. Can we display the logged-in user’s username or email?

ANS: – Yes, we can display the user information on the page. Once the user is logged in, we will get the user information as a JSON object from the AWS Cognito User Pool. by parsing the JSON object, we can display the user information on the page.

2. Can we redirect the user to login page after logout?

ANS: – Yes, we can redirect the user to the login page once the user is logged out from the application. We can use navigate or normal javascript method (window.location.href) 

WRITTEN BY Mayur Patel

Mayur Patel works as a Lead Full Stack Developer at CloudThat. With solid experience in frontend, backend, database management, and AWS Cloud, he is a versatile and reliable developer. Having hands-on expertise across the entire technology stack, Mayur focuses on building applications that are robust, scalable, and efficient. Passionate about continuous learning, he enjoys exploring new technologies daily and actively shares his knowledge to foster growth within his team and the broader community. Mayur’s practical approach, strong teamwork, and drive for innovation make him an invaluable member of every project he undertakes.

Share

Comments

    Click to Comment

Get The Most Out Of Us

Our support doesn't end here. We have monthly newsletters, study guides, practice questions, and more to assist you in upgrading your cloud career. Subscribe to get them all!