Understanding Tokens (JWTs)

What Cognito hands back after sign-in, and how your backend checks it.

1 min read

After a successful sign-in, Cognito returns three tokens:

TokenContainsUsed forDefault lifetime
ID tokenWho the user is: email, name, custom attributesShowing user info in your app1 hour
Access tokenWhat the user may do: scopes, groupsCalling your API1 hour
Refresh tokenNothing readableGetting new ID and access tokens without re-login30 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:

JSON
{
  "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.

Watch out

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.

Tip

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. 1. Which token should your frontend send to your API?