Overview
AWS Cognito User Pools use RS256 signing with JWKS for token verification. This guide shows you how to integrate Cognito authentication with Revstack.Prerequisites
- An AWS account with a Cognito User Pool
- Your AWS region (e.g.,
us-east-1) - Your User Pool ID (e.g.,
us-east-1_Abc123) - (Optional) Your App Client ID for audience validation
Installation
Configuration
1. Get your Cognito credentials
From the AWS Console:- Go to Amazon Cognito → User Pools
- Select your User Pool
- Note your User Pool ID (format:
region_poolId) - Go to App Integration → App clients
- Note your App Client ID (this is used for audience validation)
2. Build the auth contract
Create an auth contract using your Cognito configuration:- Constructs the issuer:
https://cognito-idp.us-east-1.amazonaws.com/us-east-1_Abc123 - Sets the JWKS URI to
<issuer>/.well-known/jwks.json - Configures RS256 verification strategy
- Sets the audience to your
clientId(if provided)
Verification
Initialize the verifier
Verify tokens
In your API routes or middleware:Token structure
Cognito ID tokens contain standard OIDC claims:Accessing Cognito attributes
Cognito includes user attributes in the ID token that you can access:ID tokens vs. Access tokens
Cognito issues two types of tokens:- ID tokens - Contain user attributes and are meant for your application
- Access tokens - Used for authorizing API calls to AWS services
token_use claim:
Custom attributes
If you’ve added custom attributes to your User Pool, they’ll be prefixed withcustom::
Using the username instead of sub
If you want to use the Cognito username as the user ID instead of thesub claim:
Complete example
Here’s a full Express.js integration:Frontend integration
When calling your API from a Cognito-enabled frontend:Environment variables
Store your Cognito configuration in environment variables:Testing
To test your integration:- Sign in via your Cognito-enabled frontend
- Obtain the ID token from the Cognito session
- Send it in the Authorization header:
Bearer <id_token> - Verify the token is validated correctly
Troubleshooting
”Issuer mismatch” error
Verify that:- Your region is correct
- Your User Pool ID is formatted correctly (e.g.,
us-east-1_Abc123) - The token was issued by the correct User Pool
”Invalid signature” error
Check that:- The token was issued by the correct Cognito User Pool
- The JWKS endpoint is accessible
- You’re using an ID token, not an access token
”Audience validation failed” error
Ensure:- Your
clientIdmatches the App Client ID that issued the token - The token’s
audclaim matches your App Client ID
Using access tokens instead of ID tokens
If you accidentally send access tokens, verification will fail. Always use ID tokens for user authentication. Check thetoken_use claim to verify.
Next steps
Auth Overview
Learn more about JWT verification in Revstack
Error Handling
Handle authentication errors gracefully