Understanding Tokens (JWTs)
What Cognito hands back after sign-in, and how your backend checks it.
After a successful sign-in, Cognito returns three tokens:
| Token | Contains | Used for | Default lifetime |
|---|---|---|---|
| ID token | Who the user is: email, name, custom attributes | Showing user info in your app | 1 hour |
| Access token | What the user may do: scopes, groups | Calling your API | 1 hour |
| Refresh token | Nothing readable | Getting new ID and access tokens without re-login | 30 days |
Lifetimes are configurable per app client.
What's a JWT?
A JSON Web Token is three Base64 parts joined by dots: header.payload.signature. The payload holds
claims, like:
{
"sub": "8f3c2a1e-5b7d-4e2a-9c1f-1234567890ab",
"email": "sam@example.com",
"cognito:groups": ["premium"],
"exp": 1795012345
}The signature proves Cognito issued it and nobody changed it. Your backend verifies the signature with
Cognito's public keys (published at the pool's JWKS URL), then checks exp (expiry), iss (issuer), and the
audience or client ID.
Anyone can read a JWT's payload, because it's only encoded, not encrypted. Never put secrets in custom attributes. And never trust a token you haven't verified.
Use sub (a stable unique ID) as the user's key in your database, not the email, which can change.
Key takeaways
- ID token = who the user is, access token = what they can do, refresh token = get new tokens.
- JWTs are signed, not encrypted: verify the signature and claims, and don't store secrets in them.
- Key your data on the `sub` claim.
Check your understanding
Pick an answer to see the explanation.
1. Which token should your frontend send to your API?