Authenticating Users with Amazon Cognito User Pools
This lesson explains how an Amazon Cognito user pool works as a managed user directory. It handles sign-up and sign-in and issues JSON Web Tokens (JWTs), answering the question of authentication: who is this user?
Learning outcomes
After completing this lesson, you will be able to:
- Describe the sign-in methods that user pools support, including federation with social and corporate identity providers.
- Distinguish the ID, access and refresh tokens and identify where each one is used.
- Describe the security features and customisation options of a user pool.
- Identify how user pools integrate with API Gateway, Application Load Balancers and other AWS services.
Signing users in
Cognito user pools support several methods of signing users in. The following list describes them.
- Username and password.
- Passwordless sign-in with passkeys or one-time codes.
- Federation with social providers and SAML or OIDC identity providers.
The managed login pages (hosted UI) and app clients handle the OAuth 2.0 flows, so you do not have to build login pages yourself.
Tokens
A successful sign-in to a Cognito user pool returns three tokens. The ID and access tokens are JSON Web Tokens (JWTs): signed documents containing claims that any party can read and verify without calling Cognito. Each token serves a distinct purpose.
The ID token describes the user. Its claims include the user's unique identifier (sub), attributes such as email and name, and group membership, and its audience is the app client. The application uses it to show who is signed in, and an API can use it to identify the caller. The access token describes what the user may do. It contains scopes and groups rather than personal attributes, and it is the token to send to APIs that authorise by OAuth scope and to Cognito's own user APIs, such as changing a password. Both are valid for one hour by default.
The refresh token is not a JWT and is never sent to APIs. The application presents it to Cognito to obtain new ID and access tokens when they expire, so the user stays signed in, by default for 30 days. Revoking the refresh token, or signing the user out globally, stops new tokens being issued. The following table summarises the three tokens.
| Token | Contains | Used for |
|---|---|---|
| ID token | The user's attributes as claims | Telling the application who the user is |
| Access token | Scopes and group membership | Authorising API calls |
| Refresh token | — | Getting new tokens without signing in again |
APIs validate the JWTs using the user pool's public keys, published as a JWKS.
Example. The app sends a token in the Authorization header; API Gateway checks its signature, expiry and audience before calling the backend. Use the access token when the method requires OAuth scopes.
curl -H "Authorization: $ID_TOKEN" https://api.example.com/prod/profile
# Claims the backend can read from the validated ID token:
# {"sub": "7b2c...", "email": "[email protected]", "cognito:groups": ["admins"], "token_use": "id", "exp": 1791370000}Which token?
Security and customisation
User pools provide features that strengthen security and allow the sign-in experience to be customised.
- MFA by TOTP, SMS or email.
- Threat protection (Plus tier) detects compromised credentials and risky sign-ins.
- Groups appear in tokens and can map to IAM roles.
- Lambda triggers customise the flow: validate data before sign-up, run code after confirmation, add claims before tokens are generated, or build custom challenges.
Integrations
Cognito user pools integrate directly with other AWS services. The following list describes the principal integrations.
- API Gateway: a Cognito user pool authorizer validates tokens with no code.
- Application Load Balancer: authenticates users before requests reach the application.
- AWS AppSync: a user pool is one of the authorisation modes of a GraphQL API.
- Identity pools: exchange user pool tokens for temporary AWS credentials, as the next lesson explains.
Customise and integrate user pools
Which token goes where?
Each Cognito token serves a distinct purpose in an application. The following table describes the contents of each token and how it should be used.
| Token | Contains | Use it to |
|---|---|---|
| ID token | Who the user is: email, name, custom attributes, groups | Show the user's profile in the app; authorise with a Cognito authorizer on API Gateway |
| Access token | What the user may do: scopes and groups | Call APIs that check OAuth scopes (HTTP API JWT authorizer) |
| Refresh token | A long-lived credential | Obtain new ID and access tokens without signing in again |
A developer's API rejects requests after exactly one hour: the app never refreshes its tokens. Using the refresh token to obtain new tokens before expiry fixes it. Storing tokens securely (not in localStorage for sensitive apps) and verifying signatures and expiry on the server complete the picture.
How does a client keep a user signed in after the ID and access tokens expire?
Show answer
It calls the token endpoint, or InitiateAuth with the REFRESH_TOKEN_AUTH flow, with the refresh token to obtain new ID and access tokens, without asking for the password again.
Exam Tips
Remember for the exam
- User pools handle authentication: proving who the user is.
- To secure an API Gateway API for app users with minimal code, use a Cognito user pool authorizer.
- To authorise API calls by scope, validate the access token.
- To get new tokens without asking for credentials again, use the refresh token.