{"id":60670,"date":"2024-03-18T18:33:24","date_gmt":"2024-03-18T13:03:24","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=60670"},"modified":"2024-03-27T18:42:23","modified_gmt":"2024-03-27T13:12:23","slug":"basic-authentication-using-aws-cognito-for-beginners","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/basic-authentication-using-aws-cognito-for-beginners\/","title":{"rendered":"Basic Authentication using AWS Cognito for beginners"},"content":{"rendered":"<p>Traditionally, building user authentication from scratch can be time-consuming and complex. This is where Amazon Cognito comes in, offering a hassle-free solution to manage user registration, login, and authorization in your apps.<\/p>\n<p>In this tutorial, we&#8217;ll explore the powerful capabilities of AWS Cognito to create a robust authentication or login system for your applications. Additionally, we&#8217;ll cover the essential steps to route users between pages after successful authentication seamlessly.<\/p>\n<h2><strong>Understanding AWS Cognito<\/strong><\/h2>\n<p>AWS Cognito is a service that simplifies access control for your applications out of the box, saving you the hassle of building a backend and integrating it with a database. Specifically, we&#8217;ll utilize the user pool feature, which acts as a database storing user credentials such as username, email address, and password. AWS Cognito also provides a pre-built UI for login and registration, making the entire process convenient.<\/p>\n<p>Here are the key features of Cognito User Pools:<\/p>\n<ol>\n<li><strong>User Registration:<\/strong><br \/>\n&#8211;\u00a0 Simplified user registration process with customizable sign-up pages.<br \/>\n&#8211;\u00a0 Email verification for enhanced security.<\/li>\n<li><strong>Authentication Methods:<\/strong><br \/>\nSupport for various authentication methods, including:<br \/>\n&#8211;\u00a0 Email and password.<br \/>\n&#8211;\u00a0 Social sign-ins (e.g., Google, Facebook, Amazon).<br \/>\n&#8211;\u00a0 Multi-factor authentication (MFA) for an added layer of security.<\/li>\n<li><strong>Integration and Versatility:<\/strong><br \/>\n&#8211;\u00a0 Seamless integration with web, mobile, and server-side applications.<br \/>\n&#8211;\u00a0 Versatility to support different use cases and application frameworks.<\/li>\n<li><strong>Security Measures:<\/strong><br \/>\n&#8211;\u00a0 Token-based authentication to secure communication between the application and the user pool.<br \/>\n&#8211;\u00a0 Built-in protection against common security threats.<\/li>\n<li><strong>Scalability:<\/strong><br \/>\n&#8211;\u00a0 Automatic scaling to handle a large number of users without manual intervention.<br \/>\n&#8211;\u00a0 Reliable performance even as the user base grows.<\/li>\n<li><strong>Developer-Friendly Tools:<\/strong><br \/>\n&#8211;\u00a0 AWS Cognito provides a hosted UI that developers can leverage for user authentication, reducing the effort required for UI development.<\/li>\n<\/ol>\n<h2>Steps to Create AWS Cognito User Pool<\/h2>\n<p><strong>1. Log in to the AWS Management Console and search for &#8220;Cognito&#8221; using the search bar.<\/strong> Then, open the Cognito service.<\/p>\n<p><span style=\"font-weight: 400;\"><strong>2. Click on the &#8220;Create User Pool&#8221; button.<\/strong> You will choose between two ways users can log in.<\/span><\/p>\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Standard login( default)<\/span>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Users create an account with an email and password, managed by Cognito.<\/span><\/li>\n<\/ul>\n<\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">\u00a0\u00a0Social login<\/span>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Users log in with existing accounts from platforms like Facebook or Google.<\/span><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><strong>3. Since we are implementing the login with email\/username , we will be selecting the sign-in options with Username and Email.<\/strong><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-60716\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/03\/Create-user-pool-User-pools-Amazon-Cognito-us-east-2-14-1.png\" alt=\"\" width=\"3456\" height=\"1862\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/03\/Create-user-pool-User-pools-Amazon-Cognito-us-east-2-14-1.png 3456w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/Create-user-pool-User-pools-Amazon-Cognito-us-east-2-14-1-300x162.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/Create-user-pool-User-pools-Amazon-Cognito-us-east-2-14-1-1024x552.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/Create-user-pool-User-pools-Amazon-Cognito-us-east-2-14-1-768x414.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/Create-user-pool-User-pools-Amazon-Cognito-us-east-2-14-1-1536x828.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/Create-user-pool-User-pools-Amazon-Cognito-us-east-2-14-1-2048x1103.png 2048w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/Create-user-pool-User-pools-Amazon-Cognito-us-east-2-14-1-624x336.png 624w\" sizes=\"(max-width: 3456px) 100vw, 3456px\" \/><\/p>\n<p><strong>4. Configure security requirements<\/strong><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">Configure your desired settings, such as password policies, multi-factor authentication, MFA methods, and User account recovery. For the test app, we will be choosing MFA methods Authenticator apps.\u00a0<\/span><\/li>\n<li>Regarding the User account recovery option, tick the recommended option for self-service account recovery and choose the option Email. You can choose any other options too.<\/li>\n<\/ul>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-60720\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/03\/ConfigureSecurity1.png\" alt=\"AWS Cognito - Set password policy and MFA\" width=\"3456\" height=\"1862\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/03\/ConfigureSecurity1.png 3456w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/ConfigureSecurity1-300x162.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/ConfigureSecurity1-1024x552.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/ConfigureSecurity1-768x414.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/ConfigureSecurity1-1536x828.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/ConfigureSecurity1-2048x1103.png 2048w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/ConfigureSecurity1-624x336.png 624w\" sizes=\"(max-width: 3456px) 100vw, 3456px\" \/><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-60721\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/03\/ConfigureSecurity2-1.png\" alt=\"AWS Cognito - Set User account recovery \" width=\"3456\" height=\"1862\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/03\/ConfigureSecurity2-1.png 3456w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/ConfigureSecurity2-1-300x162.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/ConfigureSecurity2-1-1024x552.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/ConfigureSecurity2-1-768x414.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/ConfigureSecurity2-1-1536x828.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/ConfigureSecurity2-1-2048x1103.png 2048w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/ConfigureSecurity2-1-624x336.png 624w\" sizes=\"(max-width: 3456px) 100vw, 3456px\" \/><\/p>\n<p><strong>5. Configure sign-up experience<\/strong><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">\u00a0Customize the sign-up process to suit your requirements by setting required attributes and custom attributes. These attributes will be displayed to the user on the sign-up page and stored in the Cognito user pool.<\/span><\/li>\n<\/ul>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-60722\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/03\/ConfigureSignUp1.png\" alt=\"Configure sign-up experience \" width=\"3456\" height=\"1862\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/03\/ConfigureSignUp1.png 3456w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/ConfigureSignUp1-300x162.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/ConfigureSignUp1-1024x552.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/ConfigureSignUp1-768x414.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/ConfigureSignUp1-1536x828.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/ConfigureSignUp1-2048x1103.png 2048w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/ConfigureSignUp1-624x336.png 624w\" sizes=\"(max-width: 3456px) 100vw, 3456px\" \/><\/p>\n<p><strong>6. Configure message delivery<\/strong><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">Configure message delivery by selecting the option &#8220;Send email with Cognito,&#8221; which is recommended for the test application. For production, choose the option &#8220;Send email with Amazon SES.&#8221;<\/span><\/li>\n<\/ul>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-60725\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/03\/ConfigureMessageDelivery.png\" alt=\"Configure Message Delivery \" width=\"3456\" height=\"1862\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/03\/ConfigureMessageDelivery.png 3456w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/ConfigureMessageDelivery-300x162.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/ConfigureMessageDelivery-1024x552.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/ConfigureMessageDelivery-768x414.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/ConfigureMessageDelivery-1536x828.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/ConfigureMessageDelivery-2048x1103.png 2048w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/ConfigureMessageDelivery-624x336.png 624w\" sizes=\"(max-width: 3456px) 100vw, 3456px\" \/><\/p>\n<p><strong>7. Integrate your app<\/strong><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">User pool name<\/span>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Provide a unique name for your User pool.<\/span><\/li>\n<\/ul>\n<\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Hosted authentication pages<\/span>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">We are using Cognito-hosted UI on our test application so we need to check the option &#8220;Use the Cognito Hosted UI&#8221;.<\/span><\/li>\n<\/ul>\n<\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Domain<\/span>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Choose the option &#8220;Use a Cognito domain&#8221; and provide the cognito domain, I am using the random domain name test2080. You can choose as you want.<\/span><\/li>\n<\/ul>\n<\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Initial app client<\/span>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Choose the option &#8220;Public client&#8221;\u00a0 for the App type since we are testing this in the browser.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Enter a friendly name for your app client.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Choose the option &#8220;Generate a client secret&#8221; since we will require it to authorize API requests.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Enter the callback URL as<\/span><a href=\"http:\/\/localhost:3000\/logged-in\"> <span style=\"font-weight: 400;\">http:\/\/localhost:3000\/logged-in<\/span><\/a><span style=\"font-weight: 400;\">. After Cognito authenticates the user, it will redirect them to this callback URL<\/span><\/li>\n<li>Click on Advanced app client settings and add the signOut URL \u00a0<a href=\"http:\/\/localhost:3000\/logout\">http:\/\/localhost:3000\/logout<\/a>. The sign-out URL is a redirect page sent by Cognito when your application signs users out. This is needed only if you want Cognito to direct signed-out users to a page other than the callback URL.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-60733\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/03\/Integrate-your-app.png\" alt=\"Integrate your app\" width=\"3456\" height=\"5218\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/03\/Integrate-your-app.png 3456w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/Integrate-your-app-199x300.png 199w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/Integrate-your-app-678x1024.png 678w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/Integrate-your-app-768x1160.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/Integrate-your-app-1017x1536.png 1017w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/Integrate-your-app-1356x2048.png 1356w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/Integrate-your-app-624x942.png 624w\" sizes=\"(max-width: 3456px) 100vw, 3456px\" \/><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-60828\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/03\/signout2.png\" alt=\"add sign out url\" width=\"1339\" height=\"842\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/03\/signout2.png 1339w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/signout2-300x189.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/signout2-1024x644.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/signout2-768x483.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/signout2-624x392.png 624w\" sizes=\"(max-width: 1339px) 100vw, 1339px\" \/><\/p>\n<p><strong>8. Review and Create<\/strong><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">After reviewing your configurations on the final page, you can create your user pool by clicking &#8220;Create pool\u201d to create your user pool.<\/span><\/li>\n<li><span style=\"font-weight: 400;\">After the user pool is successfully created, you will see it listed as shown below.<\/span><\/li>\n<\/ul>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-60741\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/03\/UserPool-created.png\" alt=\"list of user pools\" width=\"3456\" height=\"1862\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/03\/UserPool-created.png 3456w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/UserPool-created-300x162.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/UserPool-created-1024x552.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/UserPool-created-768x414.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/UserPool-created-1536x828.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/UserPool-created-2048x1103.png 2048w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/UserPool-created-624x336.png 624w\" sizes=\"(max-width: 3456px) 100vw, 3456px\" \/><\/p>\n<p>Now click on the User pool name that we have just created. It will redirect us to the User Pool overview page and click on the App integration tab.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-60800\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/03\/userPoolOverview1.png\" alt=\"userPool Overview page\" width=\"3456\" height=\"4016\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/03\/userPoolOverview1.png 3456w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/userPoolOverview1-258x300.png 258w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/userPoolOverview1-881x1024.png 881w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/userPoolOverview1-768x892.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/userPoolOverview1-1322x1536.png 1322w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/userPoolOverview1-1762x2048.png 1762w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/userPoolOverview1-624x725.png 624w\" sizes=\"(max-width: 3456px) 100vw, 3456px\" \/><\/p>\n<p>Scroll to the end and click on the App client name. It will redirect us to the App Client information page.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-60801\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/03\/appClientInfo.png\" alt=\"App client information page\" width=\"3456\" height=\"1850\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/03\/appClientInfo.png 3456w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/appClientInfo-300x161.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/appClientInfo-1024x548.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/appClientInfo-768x411.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/appClientInfo-1536x822.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/appClientInfo-2048x1096.png 2048w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/appClientInfo-624x334.png 624w\" sizes=\"(max-width: 3456px) 100vw, 3456px\" \/><\/p>\n<p>We will require Client Id and Client secret value to integrate cognito on our test app.<\/p>\n<p><span style=\"font-weight: 400;\">Here&#8217;s the basic structure of our Node.js application:<\/span><\/p>\n<ul>\n<li><span style=\"font-weight: 400;\">views<\/span>\n<ul>\n<li>\u00a0index.ejs<\/li>\n<li>loggedIn.ejs<\/li>\n<li>loggedOut.ejs<\/li>\n<\/ul>\n<\/li>\n<li><span style=\"font-weight: 400;\">.env<\/span><\/li>\n<li>app.js<\/li>\n<li>package.json<\/li>\n<\/ul>\n<p><strong>.env<\/strong><\/p>\n<p>CLIENT_ID=&lt;client_id&gt;<br \/>\nCLIENT_SECRET=&lt;client_secret&gt;<br \/>\nCOGNITO_DOMAIN=&lt;cognito_domain&gt;<\/p>\n<p>app.js<\/p>\n<div>\n<pre>const express = require('express');\r\n\r\nrequire('dotenv').config();\r\n\r\n\r\nconst app = express();\r\n\r\n\r\nconst port = 3000;\r\n\r\nconst clientId = process.env.CLIENT_ID;\r\n\r\nconst clientSecret = process.env.CLIENT_SECRET;\r\n\r\napp.set('view engine', 'ejs');\r\n\r\n\r\n\r\napp.get('\/', (req, res) =&gt; {\r\n\r\nres.render('index');\r\n\r\n});\r\n\r\n\r\n\r\napp.get('\/logged-in', async (req, res) =&gt; {\r\n\r\nres.render('loggedIn');\r\n\r\n});\r\n\r\n\r\n\r\napp.get('\/logout', (req, res) =&gt; {\r\n\r\nres.render('loggedOut');\r\n\r\n});\r\n\r\n\r\n\r\n\r\napp.listen(port, () =&gt; {\r\n\r\nconsole.log(`Server running at http:\/\/localhost:${port}`);\r\n\r\n});\r\n<\/pre>\n<h3><span style=\"font-weight: 400;\">Creating the Views<\/span><\/h3>\n<p><strong>index.ejs<\/strong><\/p>\n<div>\n<pre>&lt;head&gt;\r\n\r\n    &lt;title&gt;Index Page&lt;\/title&gt;\r\n\r\n&lt;\/head&gt;\r\n\r\n&lt;body&gt;\r\n\r\n  &lt;h1&gt;Welcome to the Index Page!&lt;\/h1&gt;\r\n\r\n  &lt;a href=\"&lt;%= process.env.COGNITO_DOMAIN %&gt;\/login?client_id=&lt;%= process.env.CLIENT_ID %&gt;&amp;response_type=code&amp;scope=email+openid+phone&amp;redirect_uri=http:\/\/localhost:3000\/logged-in\"&gt;Register\/Login&lt;\/a&gt;\r\n\r\n&lt;\/body&gt;\r\n\r\n&lt;\/html&gt;\r\n\r\n<\/pre>\n<p><strong>loggedIn.ejs<\/strong><\/p>\n<div>\n<pre>&lt;html&gt;\r\n\r\n&lt;head&gt;\r\n\r\n&lt;title&gt;LoggedIn User&lt;\/title&gt;\r\n\r\n&lt;\/head&gt;\r\n\r\n&lt;body&gt;\r\n\r\n&lt;p&gt;You have successfully logged in.&lt;\/p&gt;\r\n\r\n&lt;a\r\n\r\n href=\"&lt;%= process.env.COGNITO_DOMAIN %&gt;\/logout?client_id=&lt;%= process.env.CLIENT_ID %&gt;&amp;logout_uri=http:\/\/localhost:3000\/logout\"&gt;Logout&lt;\/a&gt;\r\n\r\n&lt;\/body&gt;\r\n\r\n&lt;\/html&gt;\r\n<\/pre>\n<p><strong>loggedOut.ejs<\/strong><\/p>\n<pre>&lt;html&gt;\r\n\r\n&lt;head&gt;\r\n\r\n&lt;title&gt;Logout Page&lt;\/title&gt;\r\n\r\n&lt;\/head&gt;\r\n\r\n&lt;body&gt;\r\n\r\n&lt;h1&gt;Logout&lt;\/h1&gt;\r\n\r\n&lt;p&gt;You have been successfully logged out.&lt;\/p&gt;\r\n\r\n&lt;a href=\"&lt;%= process.env.COGNITO_DOMAIN %&gt;\/login?client_id=&lt;%= process.env.CLIENT_ID %&gt;&amp;response_type=code&amp;scope=email+openid+phone&amp;redirect_uri=http:\/\/localhost:3000\/logged-in\"&gt;Log back in&lt;\/a&gt;\r\n\r\n&lt;\/body&gt;\r\n\r\n&lt;\/html&gt;<\/pre>\n<div>\n<p>Lets&#8217;s run the test app<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-60831\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/03\/indexPage.png\" alt=\"index Page\" width=\"629\" height=\"420\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/03\/indexPage.png 629w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/indexPage-300x200.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/indexPage-624x417.png 624w\" sizes=\"(max-width: 629px) 100vw, 629px\" \/><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-60832\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/03\/redirection1.png\" alt=\"\" width=\"1222\" height=\"533\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/03\/redirection1.png 1222w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/redirection1-300x131.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/redirection1-1024x447.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/redirection1-768x335.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/redirection1-624x272.png 624w\" sizes=\"(max-width: 1222px) 100vw, 1222px\" \/><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-60833\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/03\/redirection2.png\" alt=\"\" width=\"465\" height=\"668\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/03\/redirection2.png 465w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/redirection2-209x300.png 209w\" sizes=\"(max-width: 465px) 100vw, 465px\" \/> <img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-60834\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/03\/redirection3.png\" alt=\"\" width=\"474\" height=\"447\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/03\/redirection3.png 474w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/redirection3-300x283.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/redirection3-24x24.png 24w\" sizes=\"(max-width: 474px) 100vw, 474px\" \/> <img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-60835\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/03\/redirection4.png\" alt=\"\" width=\"469\" height=\"831\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/03\/redirection4.png 469w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/redirection4-169x300.png 169w\" sizes=\"(max-width: 469px) 100vw, 469px\" \/><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-60836\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/03\/redirection5.png\" alt=\"\" width=\"869\" height=\"311\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/03\/redirection5.png 869w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/redirection5-300x107.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/redirection5-768x275.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/redirection5-624x223.png 624w\" sizes=\"(max-width: 869px) 100vw, 869px\" \/><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-60837\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/03\/redirection6.png\" alt=\"\" width=\"685\" height=\"488\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/03\/redirection6.png 685w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/redirection6-300x214.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/redirection6-624x445.png 624w\" sizes=\"(max-width: 685px) 100vw, 685px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">Let&#8217;s verify whether the user we just created exists in the Cognito user pool.<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-60839\" src=\"https:\/\/www.tothenew.com\/blog\/wp-ttn-blog\/uploads\/2024\/03\/UserCreated.png\" alt=\"User list on cognito\" width=\"1684\" height=\"784\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/03\/UserCreated.png 1684w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/UserCreated-300x140.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/UserCreated-1024x477.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/UserCreated-768x358.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/UserCreated-1536x715.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2024\/03\/UserCreated-624x291.png 624w\" sizes=\"(max-width: 1684px) 100vw, 1684px\" \/><\/p>\n<p>The user has just signed up for is successfully showing up in the Cognito dashboard.<\/p>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p><span style=\"font-weight: 400;\">Congratulations! We&#8217;ve successfully set up the authentication layer in your Node.js application using Amazon Cognito. Users can now log in securely through the Cognito-hosted UI, and upon successful login, they will be redirected to our specified endpoint. This is just the beginning of securing your application; feel free to explore more advanced features and configurations offered by Amazon Cognito based on your application&#8217;s requirements.<\/span><\/p>\n<p>&nbsp;<\/p>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>Traditionally, building user authentication from scratch can be time-consuming and complex. This is where Amazon Cognito comes in, offering a hassle-free solution to manage user registration, login, and authorization in your apps. In this tutorial, we&#8217;ll explore the powerful capabilities of AWS Cognito to create a robust authentication or login system for your applications. Additionally, [&hellip;]<\/p>\n","protected":false},"author":1739,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":278},"categories":[1174,1185],"tags":[248,5694,5704],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/60670"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1739"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=60670"}],"version-history":[{"count":31,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/60670\/revisions"}],"predecessor-version":[{"id":61002,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/60670\/revisions\/61002"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=60670"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=60670"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=60670"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}