A Guide to Amplify CLI - Part 3 - Authentication

In this post, I will show you how to integrate Authentication for your users to register and login on your application with an username and password.



I assume you have npm installed and have little knowledge about different services in AWS.

In this tutorial, we will be using Amazon Cognito as it is Amplify's main authetication provider. Cognito handles user registration, authentication, and different account operations.

Steps:
1. Install aws-amplify by typing in CMD: npm install aws-amplify
2. The easiest way to use Authentication with Amplify is by using Amplify CLI. Type into your CMD: amplify add auth
3. Under "..the default authentication and security..", select `Default configuration`.
4. For "..users to be able to sign in", select `Username`.
5. For "..configure advanced settings", select `No, I am done`.

6. Run `amplify push` to build all your local backend resources and provision it in cloud.

Now, we have to create sign up and sign in page and integrate respective functions of Auth into it.

We need to add some lines of code in your app's entry file. It might be app.js, index.js or anything.


Existing Auth resources from Amazon Cognito UserPools can be used with Amplify libraries by calling the `Amplify.configure()` method.


Steps:
1. Add `import { Amplify, Auth } from 'aws-amplify';` in your script file. In order to have these imports, you need to also run `npm install aws-amplify`.
2. Under `Amplify.configure({ ... })`, we need the following values:
    a. identityPoolId: [This is required] If you don't know this for that you need to have AWS CLI installed on your system. Head on to `https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html` and follow the steps of installation according to your system's operating system.
    b. region: [This is required] This is the region where your Amazon Cognito is deployed. E.g. us-east-1, ap-south-1.
    c. identityPoolRegion: This is optional and is only required if Amazon Cognito Region is different.
    d. userPoolId: This is Amazon Cognito User Pool ID. If you don't know your User Pool ID, you can still get it from AWS Console >> Amazon Cognito >> Select your project's user pool.
The user pools gets created when you add auth and push your Amplify changes onto the cloud.

    e. mandatorySignIn: Accepts boolean. It enforces user authentication prior to accessing AWS resources
    f. signUpVerificationMethod: Only options are `code` and `link`. This is used when autoSignIn is enabled for Auth.signup. Here, the `code` option is used for `Auth.confirmSignUp` and `link` is used for email link verification.
    g. storage: This is optional and developers can specify customized storage object.
    h. userPoolClientId: Click on `App Integration` tab under your user pool in Amazon Cognito. Scroll down to bottom to find `App client and analytics`. The Client ID is 26 characters long string comprising of numbers and letters.

Comments

Popular

How To Create MySQL Table with SequelizeJS

How To Read CSV Files in Java as Maven Project

How To Create A Hibernate Project in Maven