Page updated Nov 15, 2023

Set up Amplify Auth

Authentication with Amplify

Amplify uses Amazon Cognito as the main authentication provider. Amazon Cognito is a robust user directory service that handles user registration, authentication, account recovery & other operations. In this tutorial, you'll learn how to add authentication to your application using Amazon Cognito and username/password login.

Install Amplify Libraries

Install the necessary dependencies by running the following command:

npm install aws-amplify amazon-cognito-identity-js @react-native-community/netinfo @react-native-async-storage/async-storage
1npm install aws-amplify amazon-cognito-identity-js @react-native-community/netinfo @react-native-async-storage/async-storage

You will also need to install the pod dependencies for iOS:

npx pod-install
1npx pod-install

Set Up Backend Resources

The most common way to use Authentication with Amplify is via the Amplify CLI, which allows you to create new Amazon Cognito resources or import existing ones. However, you can also use the Amplify Studio console to configure authentication or use the Amplify.configure() method to set up authentication with existing resources.

Prerequisites: Install and configure the Amplify CLI in addition to the Amplify libraries and necessary dependencies.

To start provisioning auth resources in the backend, go to your project directory and execute the command:

amplify add auth
1amplify add auth
? Do you want to use the default authentication and security configuration? Default configuration ? How do you want users to be able to sign in? Username ? Do you want to configure advanced settings? No, I am done.
1? Do you want to use the default authentication and security configuration? Default configuration
2? How do you want users to be able to sign in? Username
3? Do you want to configure advanced settings? No, I am done.

If you have previously enabled an Amplify category that uses Auth behind the scenes (e.g. API category), you can run the amplify update auth command to edit your configuration if needed.

The CLI prompts will help you to customize your auth flow for your app. With the provided options, you can:

  • Customize sign-in/registration flow
  • Customize email and SMS messages for Multi-Factor Authentication
  • Customize attributes for your users, e.g. name, email
  • Enable 3rd party social providers, e.g. Facebook, Twitter, Google and Amazon

If you wish to federate with social providers you will need to configure them first.

After configuring your Authentication options, update your backend and deploy the service by running the push command:

amplify push
1amplify push

Now, the authentication service has been deployed and you can start using it. To view the deployed services in your project at any time, go to Amplify Console by running the following command:

amplify console
1amplify console

In your app's entry point (i.e. App.js, index.js, _app.js, or main.js), import and load the configuration file:

import { Amplify, Auth } from 'aws-amplify'; import awsconfig from './aws-exports'; Amplify.configure(awsconfig);
1import { Amplify, Auth } from 'aws-amplify';
2import awsconfig from './aws-exports';
3Amplify.configure(awsconfig);

Enable sign-up, sign-in, and sign-out

Follow the instructions in the Sign in, Sign up and Sign out to learn about how to integrate these authentication flows in your application with the Auth APIs.

Summary

To implement authentication flows using Amplify you can either use the Amplify UI libraries or call authentication methods directly on the Auth class.

Auth has over 30 methods including signUp, signIn, forgotPassword, and signOut that allow you full control over all aspects of the user authentication flow.

Check out the complete API here.