Implement Auth in React using AWS Cognito and AWS Amplify (SDK)
Tags: aws, react, javascript
Published: 2020-10-19 00:29:29

3rd party authentication is not something new, it's been there for a while now and its popularity is only increasing. It takes out the hassle of managing security risks (to some extent) and allows you to reduce the time to roll out your applications.

To implement auth in React using cognito we need to do two things:
1. Setup AWS side of things (user pools, clients, etc.)
2. Hook our AWS Cognito user pool and client with the react app

##### 1. Create AWS Cognito User Pool
The user pool is like a user directory where all your users (and their attributes like name, email, password, etc.) are stored. 
To create a new user pool, login to the AWS console and go to the AWS Cognito homepage. From there, select "Manage User Pools". Select "Create a User Pool" and give some name to the user pool. Click "Review Defaults" (or "Step through Settings" if you want to configure each step) and then click "Create Pool".

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/a5ed2xc3a4bab0yp6fm2.png)

##### 2. Create AWS Cognito User Pool's Client
Once done, it will show a success notification. From the left side, select "App clients".
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/mbhuy8hqp7bjc1w2uzhk.png)

Click "Add an app client", give it any name and make sure you uncheck the box next to "Generate client secret". Leave the rest of the options to their defaults unless you want to change something. Click "Create app client".
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/sl98ywmj6fkg91d5d82g.png)


Once the client has been created, copy "App client id" (we will need it later on).
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/4e9eusqsruqv0m4ikq3j.png)


##### 3. Add Custom Attribute(s)
AWS Cognito provides you an array of attributes that you can store against a user. The default available attributes (they are based on [OpenID Connect Specifications](http://openid.net/specs/openid-connect-core-1_0.html#StandardClaims)) are:

* name	
* given name
* middle name
* family name
* nickname
* preferred username	
* email
* phone number
* birthdate
* picture
* gender
* address
* profile
* zoneinfo
* locale
* updated at
* website

ℹ️ **The default attributes can be made required/not-required only at the time of the user pool's creation.**

But if you need to add any custom attribute you can do that easily. From the left sidebar, select "Attributes". Then click on "Add custom attribute".
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/kv3uyl902ptlzmyuo2lj.png)

Select the type of the attribute (String or number) you want to add, give it any name (without spaces), define min and max length (number of characters), and check the "Mutable" box. Once done, click "Save changes".
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/9iy66tb1zzv37woj8x24.png)

Now the last step is to make new attribute(s) both readable and writable. Go back to "App clients" from the left, click "Show Details", Click "Set attribute read and write permissions" (it will be at the bottom). Check the boxes against the custom attribute(s) and click "Save app client changes"
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/pqkktg5s57hj8ansguz9.png)


##### 4. Hook React App with AWS Cognito
[Clone this repo](https://github.com/mubbashir10/aws-cognito-react-auth), install dependencies, replace the aws conf (`src/configs/aws.ts`) and start the app.

##### 5. See it in Action
###### Create a new account
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/g9ehgabydabo23zx1x2z.png)

###### Verify User (by default the code is sent to email)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/7gz072cajus6yh7ialub.png)

###### Login
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/b1i7lcun4rzb4zxoqiu9.png)

###### Dashboard (can only access if logged in)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/h9rgphpc8jolmxfvjclf.png)

###### Newly added user 🤩
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/b2zgzv638dl1f11lb2h9.png)

That's pretty much it. Instead of writing code for each component here, I thought it would be better to just link the repo. The code is pretty self-explanatory, but if you need any help please feel free to leave your queries in the comments.

Complete API reference: [Read the official documentation] (https://docs.amplify.aws/lib/auth/getting-started/q/platform/js#create-authentication-service)