- React Authentication App with AWS Cognito
This project is a React-based web application that demonstrates user authentication using Amplify Auth backend js library. It includes features such as user sign-up, sign-in, and profile management. The app showcases a modern, responsive design using Tailwind CSS and implements protected routes to secure user-specific content.
The primary purpose of this application is to serve as a boilerplate or learning resource for developers who want to implement AWS Cognito authentication in a React application. It demonstrates best practices for handling user authentication flows, managing protected routes, and structuring a React application with multiple components.
- Sign Up: Users can create a new account by providing a username, email, and password.
- Confirmation: After sign-up, users receive a confirmation code via email.
- Sign In: Registered users can sign in using their username and password.
- Protected Routes: Certain routes (like Home and Profile) are protected and only accessible to authenticated users.
- Sign Out: Users can sign out, which clears their session and redirects them to the landing page.
- Node.js (v14 or later)
- npm (v6 or later)
- An AWS account with Cognito set up
- Clone the repository:
git clone https://github.com/labeveryday-cloud-projects/amazon-cognito-react-app.git - Navigate to the project directory:
cd amazon-cognito-react-app - Install dependencies:
npm install
- Sign in to the AWS Management Console and navigate to Cognito.
- Create a new User Pool.
- Configure the user pool settings (e.g., required attributes, password policies).
- Create an app client for your React application.
- Note down the User Pool ID and App Client ID.
- Create a file named
aws-exports.jsin thesrcdirectory. - Add your Cognito configuration:
const awsconfig = { region: 'your-region', userPoolId: 'your-user-pool-id', userPoolWebClientId: 'your-app-client-id' }; export default awsconfig;
- Start the development server:
npm start - Open your browser and navigate to
http://localhost:3000.
src/components/: Contains React components for different pages.App.js: Main component that sets up routing.App.css: Global styles.index.js: Entry point of the React app.aws-exports.js: AWS Cognito configuration.
LandingPage.js: The initial page users see.SignUp.js: Handles user registration.Login.js: Manages user sign-in.Home.js: A protected route showing user-specific content.Profile.js: Displays and manages user profile information.
This is the main component that sets up the routing for the application. It uses React Router to define routes and implements a ProtectedRoute component to secure certain pages.
SignUp.js: Manages the sign-up process, including sending and verifying confirmation codes.Login.js: Handles user authentication and redirects to the home page upon successful login.
Home.jsandProfile.js: These components are only accessible to authenticated users. They demonstrate how to fetch and display user-specific data.
The app uses AWS Amplify libraries to interact with Cognito. Key functions like signUp, signIn, and getCurrentUser are used to manage the authentication flow.
The application uses Tailwind CSS for styling, providing a responsive and modern UI. Tailwind classes are used directly in the JSX for rapid styling.
- This application is for demonstration purposes and may need additional security measures for production use.
- Always follow AWS best practices for managing Cognito user pools and app clients.
- Consider implementing additional features like password reset and email verification for a more robust authentication system.
If you encounter issues:
- Ensure all dependencies are correctly installed.
- Verify that your AWS Cognito configuration is correct.
- Check the browser console for any error messages.
My passions lie in building cool stuff and impacting people's lives. I'm fortunate to weave all these elements together in my role as a Developer Advocate. On GitHub, I share my ongoing learning journey and the projects I'm building. Don't hesitate to reach out for a friendly hello or to ask any questions!
My hangouts: