This guide will help you set up AWS Cognito User Pool for authentication in your React time tracking application.
- AWS Account
- AWS CLI configured (optional but recommended)
- Basic knowledge of AWS services
-
Sign in to AWS Console
- Go to https://console.aws.amazon.com/
- Navigate to Amazon Cognito service
-
Create User Pool
- Click "Create user pool"
- Choose "Cognito user pool" (not hosted UI)
-
Configure Sign-in Experience
- User name requirements: Choose "Allow email addresses and user names"
- User name case sensitivity: Choose "Case insensitive"
- Sign-in options: Select "User name" and "Email"
- Click "Next"
-
Configure Security Requirements
- Password policy: Choose "Cognito defaults" or customize
- Multi-factor authentication: Choose "No MFA" for simplicity (or enable if needed)
- User account recovery: Enable "Self-service account recovery"
- Click "Next"
-
Configure Sign-up Experience
- Self-service sign-up: Enable
- Cognito-assisted verification and confirmation: Enable
- Verification message: Choose "Email"
- Required attributes: Select "Email" and "Name" (optional)
- Click "Next"
-
Configure Message Delivery
- Email provider: Choose "Send email with Cognito"
- From email address: Use the default or create a custom one
- Click "Next"
-
Integrate Your App
- User pool name: Enter a name (e.g., "TimeTrackingUserPool")
- Initial app client: Choose "Public client"
- App client name: Enter a name (e.g., "TimeTrackingApp")
- Client secret: Choose "Don't generate a client secret"
- Click "Next"
-
Review and Create
- Review your settings
- Click "Create user pool"
###tegin terraformiga
# Create User Pool
aws cognito-idp create-user-pool \
--pool-name "TimeTrackingUserPool" \
--policies '{
"PasswordPolicy": {
"MinimumLength": 8,
"RequireUppercase": true,
"RequireLowercase": true,
"RequireNumbers": true,
"RequireSymbols": false
}
}' \
--auto-verified-attributes email \
--username-attributes email \
--schema '[
{
"Name": "email",
"AttributeDataType": "String",
"Required": true,
"Mutable": true
}
]'
# Create App Client
aws cognito-idp create-user-pool-client \
--user-pool-id YOUR_USER_POOL_ID \
--client-name "TimeTrackingApp" \
--no-generate-secret \
--explicit-auth-flows ALLOW_USER_PASSWORD_AUTH ALLOW_REFRESH_TOKEN_AUTHAfter creating the User Pool, you'll need these values:
- User Pool ID: Found in the User Pool details (format:
region_poolid) - App Client ID: Found in the App integration tab
- Region: The AWS region where you created the User Pool
-
**Update
env_devandapp-config.jsfor production **:window.APP_CONFIG = { "aws_region": "eu-north-1", "cognito_user_pool_id": "eu-north-1_your pool", "cognito_client_id": "cognito client id" };
-
Replace the placeholder values in
env_devwith your actual values: VITE_AWS_REGION=eu-north-1
VITE_COGNITO_USER_POOL_ID=eu-north-1_XXXX
VITE_COGNITO_CLIENT_ID=XXXXXX
-
Start your development server:
npm start
-
Test the authentication flow:
- Navigate to your app
- You should see a login screen
- Try creating a new account
- Verify your email with the confirmation code
- Sign in with your credentials
For production deployment:
-
Configure CORS (if needed):
- In your User Pool settings, add your domain to the allowed origins
-
Set up custom domain (optional):
- Configure a custom domain for your Cognito hosted UI
- Update the configuration accordingly
-
"User does not exist" error:
- Make sure the user is confirmed in the User Pool
- Check that the username/email is correct
-
"Invalid client" error:
- Verify your App Client ID is correct
- Make sure the App Client is configured for your User Pool
-
"Invalid credentials" error:
- Check that your User Pool ID and region are correct
- Verify the App Client is properly configured
-
CORS errors:
- Add your domain to the allowed origins in Cognito settings
- Check that your API endpoints are properly configured
- Use HTTPS in production
- Implement proper password policies
- Enable MFA for sensitive applications
- Regularly rotate App Client secrets (if using confidential clients)
- Monitor authentication logs
- Implement proper session management
You can enhance your authentication by:
- Adding social login (Google, Facebook, etc.)
- Implementing custom authentication flows
- Adding user groups and roles
- Setting up custom attributes
- Implementing advanced security features
For more information, refer to the AWS Cognito Developer Guide.