AWS Amplify combined with Amazon Cognito provides a robust authentication solution for web applications. In this guide, we’ll walk through setting up Cognito User Pools, integrating it with Amplify, and implementing sign-up and sign-in functionality.
Navigate to Cognito Console
Configure Sign-in Experience
Configure Security Requirements
Configure Sign-up Experience
Configure Message Delivery
Integrate Your App
Review and Create
# Create a new React application
npx create-react-app my-auth-app
cd my-auth-app
# Install Amplify dependencies
npm install aws-amplify @aws-amplify/ui-react
Create a new file src/aws-config.js
:
import { Amplify } from 'aws-amplify';
Amplify.configure({
Auth: {
region: 'us-east-1', // Replace with your region
userPoolId: 'us-east-1_xxxxxx', // Replace with your User Pool ID
userPoolWebClientId: 'xxxxxxxxxxxxxx', // Replace with your App Client ID
}
});
Update your src/App.js
:
import React from 'react';
import { Authenticator } from '@aws-amplify/ui-react';
import '@aws-amplify/ui-react/styles.css';
function App() {
return (
<Authenticator>
{({ signOut, user }) => (
<div>
<h1>Welcome {user.attributes.email}</h1>
<button onClick={signOut}>Sign out</button>
</div>
)}
</Authenticator>
);
}
export default App;
You can customize the sign-up and sign-in experience by adding custom components:
import { Authenticator } from '@aws-amplify/ui-react';
const components = {
Header() {
return (
<div>
<h1>My Application</h1>
</div>
);
},
Footer() {
return (
<div>
<p>© 2024 My Company</p>
</div>
);
}
};
function App() {
return (
<Authenticator components={components}>
{({ signOut, user }) => (
// Your authenticated app content
)}
</Authenticator>
);
}
Sign Up Flow
Sign In Flow
Password Policies
Token Handling
API Protection
Invalid Client ID
Email Verification Issues
CORS Errors
AWS Amplify with Cognito provides a secure and scalable authentication solution. This setup gives you:
Remember to update security settings and implement additional features like MFA before moving to production.
By following this guide, you’ve created a fully functional authentication system using AWS Amplify and Cognito. The solution is production-ready and can be extended based on your specific requirements.