Hands-On: Create a User Pool & Sign In
Set up a user pool and app client, create a user, and get tokens from the CLI.
1 min read
This uses the simple username/password flow so you can see the tokens. Production web apps typically use Cognito's managed login pages or the AWS Amplify libraries, which use the more secure SRP or OAuth flows.
Try it
Terminal
POOL=$(aws cognito-idp create-user-pool --pool-name notesnap-users \
--auto-verified-attributes email --username-attributes email \
--query UserPool.Id --output text)
CLIENT=$(aws cognito-idp create-user-pool-client --user-pool-id $POOL \
--client-name notesnap-web --no-generate-secret \
--explicit-auth-flows ALLOW_USER_PASSWORD_AUTH ALLOW_REFRESH_TOKEN_AUTH \
--query UserPoolClient.ClientId --output text)
# Sign up a user, then confirm them as an admin (skips the email code for this demo)
aws cognito-idp sign-up --client-id $CLIENT \
--username sam@example.com --password 'Correct-Horse-9!'
aws cognito-idp admin-confirm-sign-up --user-pool-id $POOL --username sam@example.com
# Sign in and get tokens
aws cognito-idp initiate-auth --client-id $CLIENT \
--auth-flow USER_PASSWORD_AUTH \
--auth-parameters USERNAME=sam@example.com,PASSWORD='Correct-Horse-9!'Paste the IdToken into a JWT viewer (or decode the middle part with base64) to see the claims.
Lambda triggers
Customize the flow with Lambda at key moments, for example:
- Pre sign-up: block disposable email domains.
- Post confirmation: create the user's profile row in DynamoDB.
- Pre token generation: add custom claims, such as the user's plan.
Key takeaways
- A user pool plus an app client is all you need to sign users in.
- initiate-auth returns ID, access, and refresh tokens.
- Lambda triggers customize sign-up, confirmation, and token contents.
Check your understanding
Pick an answer to see the explanation.
1. You want to create a profile record in DynamoDB whenever a user confirms their account. Which trigger?