AWS Amplify + React Native / Authentication ๐Ÿ” FULL SETUP

Play Ra
ITNEXT
Published in
7 min readOct 20, 2019

--

One of the most requested topics among my channel subscribers is authentication and authorization in the React Native application. Therefore, I decided to devote a separate post to this issue, but before we start coding, it is necessary to deal with the definition of Authentication/Authorization.

Authentication is a verification of the conformity of the subject and the one he is trying to impersonate using some unique information (fingerprints, iris color, voice, etc.), in the simplest case โ€” with the help of mail and password.

Authorization is the verification and determination of the authority to perform certain actions in accordance with previously performed authentication

At the end of this article, we will make this mobile application:

Authentication is an integral part of almost any application. Knowing who the user is, the unique identifier of the user, what permissions the user has, and whether they are logged in, allows your application to display the correct views and return the correct data for the current logged in user.

Most applications require mechanisms for registering users, logging in, processing encryption and updating passwords, as well as many other tasks related to identity management. Modern applications often require things like OAUTH (open authentication), MFA (multi-factor authentication) and TOTP (time-based time passwords).

In the past, developers had to manually spin all of these authentication features from scratch. This task alone can take weeks or even months from the development team to do everything right and make it safe. In this article, youโ€™ll learn how to correctly and securely implement authentication in a React Native application using Amazon Cognito with AWS Amplify.

Amazon Cognito is AWSโ€™s fully managed identity service. Cognito provides easy and secure user registration, logon, access control, token updating, and user identity management. Cognito scales to millions of users and also supports logging in with social network providers such as Facebook, Google and Amazon.

Cognito consists of two main parts: user pools and identity pools.

User Pools โ€” User pools provide a secure user directory that stores all of your users and scales to hundreds of millions of users. This is a fully managed service. Like serverless technology, user pools are easy to configure, without having to worry about supporting any infrastructure. User pools are what manage all the users who register and log in to the account, and is the main part that we will focus on in this article.

Identity pools โ€” Identity pools allow you to authorize users who are logged into your application to access various other AWS services. Suppose you want to give a user access to a lambda function so that he can receive data from another API. You can specify this when creating the identity pool. User pools include the fact that Cognito or even Facebook or Google user pools can be the source of these identifiers.

A scenario where an Amazon Cognito user pool and an identity pool are used together.

See the diagram for the general Amazon Cognito script. The goal here is to authenticate your user and then give him access to another AWS service.

1. At the first stage, the user of your application enters the system through the user pool and receives the tokens of the user pool after successful authentication.

2. Your application then exchanges user pool tokens for AWS credentials through the identity pool.

3. Finally, your application user can then use these AWS credentials to access other AWS services such as Amazon S3 or DynamoDB.

Cognito User Pools allows your application to call various methods for a service to manage all aspects of user authentication, including things like:

  • User registration
  • User Login
  • User Logout
  • Change user password
  • Reset User Password
  • MFA Code Verification
  • Amazon Cognito Integration with AWS Amplify

AWS Amplify supports Amazon Cognito in a variety of ways. First of all, you can create and configure Amazon Cognito services directly from the AWS Amplify command-line interface. By creating an authentication service through the CLI, you can call various methods (for example, signUp, signIn and signOut) from a JavaScript application using the Amplify JavaScript client library.
Amplify also has pre-configured user interface components that allow you to build entire authentication flows in just a couple of lines of code for environments such as React, React Native, Vue, and Angular.

You ask how much does it all cost?

Pay only for what you use. No minimum fees.

Using Amazon Cognito Identity to create a user pool, you pay only for the number of active users per month (MAU). MAUs are users who have performed at least one authentication operation during a calendar month: registration, authorization, token renewal, or password change. Subsequent sessions of active users and inactive users in this calendar month are not paid.

CODING TIME ๐Ÿ‘จ๐Ÿผโ€๐Ÿ’ป๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป

PART 1

All the code for this part can be found on Github.

Step 1

Create a new project โš›๏ธ

react-native init messaga

We start the project ๐Ÿš€

iOS

cd messaga && react-native run-ios

Android

cd messaga && react-native run-android

Step 2

We connect icons๐Ÿ‘พ

Since the icons are used by the AWS Amplify framework, we therefore connect them according to this instruction ๐Ÿ“ƒ.
Check for errors. Add to App.js

import Icon from 'react-native-vector-icons/FontAwesome5'const App = () => {
return (
<>
<Icon name="comments" size={30} color="#900" />
</>
)
}

Step 3

Register your AWS account

We register according to this instruction ๐Ÿ“ƒ and check all 5 steps according to the video tutorial..
Attention!!! You will need a bank card ๐Ÿ’ณ, where should be more than 1 $ ๐Ÿ’ต
There we look and put the Amplify Command Line Interface (CLI)

Step 4

Initializing AWS Amplify in a React Native Project

Initialize our backend in the root directory of the React Native project.

amplify init

We answer the questions:

Next is the initialization of the project. ๐Ÿš€
โ ง Initializing project in the cloudโ€ฆ
Your project has been successfully initialized and connected to the cloud!

Step 5

Connecting Authentication Plugin โ€” Auth ๐Ÿ”

Now that the application is in the cloud, you can add some features, such as allowing users to register with our application and log in.

We connect the authentication plugin.

amplify add auth

Select the default configuration. This adds auth resource configurations locally to your amplify/backend/auth directory.

Select the profile we want to use. default. Enter and how users will log in. Email (write off money for SMS).

Successfully added resource yourname locally

Send changes to the cloud ๐Ÿ’ญ

amplify push

โœ” All resources are updated in the cloud

Step 6

Connect AWS Amplify to React Native Project โš›๏ธ

Details in this manual ๐Ÿ“ƒ, and briefly and in a straight line like this:

yarn add aws-amplify @aws-amplify/core aws-amplify-react-native amazon-cognito-identity-js @react-native-community/netinfo

After installation, be sure to go to the ios folder and set the pods

cd ios && pod install && cd ..

Step 7

Editing the project structure

Create the /src directory and transfer the App.js file there, renaming it to index.js with this content
Edit import in root /yourname/index.js

- import App from './App'
+ import App from './src'

Step 8

Minimal project configuration and Authenticator module

Amplify.configure โ€” project configuration

Authenticator โ€” The AWS Amplify Authentication Module provides authentication APIs and building blocks for developers who want to create user authentication capabilities.

Run the simulator

Step 9

Edit Inputs in App.js

Step 10

Change the theme of UI๐Ÿ–Œ

We create an export point for our future components /src/components/index.js with the content

export * from './AmplifyTheme'

and accordingly create the /src/components/AmplifyTheme/index.js file itself with the content

And plug the theme into the Authenticator src/index.js component

Step 11

Connecting language support

Add export to /src/components/index.js

export * from './Localei18n'

Create the file /src/components/Localei18n/index.js with the contents

And we connect the Localei18n component in src/index.js

Part 1 done โœ…

PART 2

All the code for this part can be found on Github.

Firstly, in the official documentation Amplify says

Data is stored unencrypted when using standard storage adapters (localStorage in the browser and AsyncStorage on React Native). Amplify gives you the option to use your own storage object to persist data. With this, you could write a thin wrapper around libraries like:

react-native-keychain

react-native-secure-storage

Expoโ€™s secure store

that the data is stored in an unencrypted form, and this is a risk ๐Ÿ•ท information security with possible negative consequences ๐Ÿ•ธ.

Secondly, the standard UI from Amplify does not always satisfy the UX coming from the customer, so we will solve these two problems in this part.

๐Ÿ‘‰๐Ÿป To be continued

--

--