The Amplify Framework 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.
amplify 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.
push
command:amplify push
Now that we have our authentication service deployed to AWS, it’s time to add authentication to our React app. Creating the login flow can be quite difficult and time consuming to get right. Luckily Amplify Framework has an authentication UI component we can use that will provide the entire authentication flow for us, using our configuration specified in our aws-exports.js
file.
Open src/App.js
and make the following changes:
withAuthenticator
component:import { withAuthenticator } from '@aws-amplify/ui-react'
withAuthenticator
wrapping the main component:export default withAuthenticator(App)
Changes to src/App.js
can be shown below (line 13 and 39)
npm start
Access your application at https://localhost:3000. Now you should see the app load with an authentication flow allowing users to sign up and sign in.
After creating an account and signing in, you can see that we have been brought to our landing page!
Just like that, we have incorporated user authentication with AWS Cognito via AWS Amplify into our React web application.