Authentication with Aws Cognito, Passport and NestJs (Part I)
Tags: aws, webdev, typescript, javascript
Published: 2022-10-12 15:21:40

Authentication is one of the most complex parts of developing applications. It's also one of the most important because it's how you know who you're dealing with. When building a platform, authentication is even more crucial—you need to ensure that only people who are supposed to be accessing your service can do so.

There are many different ways to go about it, but today we'll discuss how to use AWS Cognito, Passport, and NestJs to create an authentication system for your app. This is a beginner tutorial, and it should take you a long to better understand authentication with cloud providers.

<br/>
####But first, let's look at this diagram to see how everything will be connected:

![Auth flow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7lfchr4ab54b6zk84u4k.gif)
<br/>


## Aws Cognito

Aws Cognito is an Amazon service that can provide authentication, authorization, and user management out of the box, and you can learn more about it [here](https://docs.aws.amazon.com/cognito/latest/developerguide/what-is-amazon-cognito.html)

The first step is setting up your AWS account; if you don't have one, you can sign up for one [here](https://aws.amazon.com/free/). Also, you can see the related costs with each service on that page. 

After we have our account, we will search for the Cognito service like so:
![Searching for cognito](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/48nogr2chtewbbr1dlwm.png)

Let's click `Create user pool`, on step1 let's keep the Cognito user pool checked and choose only the email to sign in:

![Step 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/snks91jfp0m7ywznfl2y.png)

On the configure security requirements, let's disabled MFA because it will add unnecessary complexity to this guide.

![Step 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a3ro8lthooci48kpy3bq.png)
 
When configuring the sign-up experience, let's add the `name` to the required attributes.

![Step 3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/npkr2ux93f1fncquvanp.png)

In the next step for configuring the message delivery, let's send emails with Cognito.

![Step 4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ukzvtrc9p9fohxk7p8g2.png)

On the app integration, we named our poll `pokemon-app`, and we created a new app client name called `node-app`, going with the default values for now.
![Step 5](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/diu97c7x4tqk2xzv5r07.png)
![Step 5_1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zkb4h8z10efb6t6sn0sd.png)

In our last step, let's verify everything is correct and create the user poll.

Next, select the user pool in the table, go for the app integration tab, and create a Cognito domain. (I've used `https://pokemon-app.auth.us-east-1.amazoncognito.com`)

![Creating a Cognito domain](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j5uyhuifn7a5lwrgcdbd.png)

We should save the Cognito Domain and the Client ID value for later usage in the guide.
![Client Id](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nqylr0uvodag96hfl1yd.png)

Let's change the content from the verification email to a link:
![Verification Email](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pqcmip5bsifqaw4dbeo0.png)

Now let's grant Cognito access to our user; let's search for IAM, the service to manage access to AWS resources:
![Searching for IAM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fxozd68fsav8y43gv0ni.png)

If you already have a user select the user and click `Add permissions` go for the `Attach existing policies directly` separator and search for `AmazonEsCognitoAccess`:
![IAM policy attribution](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vx648du2h8chjwd3gvqv.png)

Select the policy next, and assign it to the user.
![Cognito policy](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gyswdqwm02t2s75s6ooj.png)

If you don't have a user, you must set up one and add the security policy. (You can set the policy on the second step when creating a new one)

![Creating an IAM user](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k3ghkj3y9tifbyh2jtfi.png)

In the next part, we will set up our next js project and integrate it with Cognito. Please stay tuned 😊