Stop 11 · Serverless & Event-Driven AppsTopics · 9/10

Amazon Cognito User Pools

Part of the AWS Roadmap

In short

Writing your own login system means password hashing, reset emails, MFA and token signing, all easy to get wrong. A Cognito user pool handles that so you can focus on your app.

Cognito user pools provide sign-up, sign-in and user management for apps and issue JSON Web Tokens after login. Identity pools are a separate feature that exchanges identities for temporary AWS credentials.

The jaadu at a tech park security desk clips a fresh visitor badge onto a smiling person's lanyard as turnstiles glow behind them.

Think of it like this

The office ID card desk

At a Bengaluru tech park, the security desk checks your ID once, then hands you a visitor badge with your name and floor on it, valid until evening. Every door just checks the badge. A user pool is the desk: it checks the password and issues tokens. Your API is the door that only checks the badge.

Watch it happen

Signing in and calling the API

App
User pool
API Gateway
Lambda

Sign in (email, password)

1/5Through the managed login pages or the SDK. MFA can be added here.

The big ideas

What you need to know

User pool = user directory

Sign-up, sign-in, email or phone verification, password reset, MFA, groups and social or SAML sign-in.

Three tokens

The ID token describes the user, the access token authorises API calls, and the refresh token gets new ones without logging in again.

App clients

Each frontend gets an app client with its own allowed flows and callback URLs. Public browser apps should not use a client secret.

Identity pools are different

Identity pools swap a login for temporary AWS credentials, for example to upload straight to S3. Many apps only need a user pool.

See it in code

Walk through the code

ts
1import { CognitoJwtVerifier } from "aws-jwt-verify";2 3const verifier = CognitoJwtVerifier.create({4  userPoolId: "ap-south-1_AbCdEf123",5  tokenUse: "access",6  clientId: "5example1clientid2abc",7});8 9export async function getUserId(authHeader?: string): Promise<string | null> {10  const token = authHeader?.replace("Bearer ", "");11  if (!token) return null;12  try {13    const payload = await verifier.verify(token);14    return payload.sub;15  } catch {16    return null;17  }18}

1/4Create the verifier once. It downloads and caches the pool's public signing keys.

TypeScript with aws-jwt-verify, AWS's open-source library. Use it when your backend isn't behind a JWT authorizer, for example on ECS.

Save yourself the pain

Common mistakes

Oops

Decoding the JWT with base64 and trusting its contents.

Do this instead

Anyone can create a fake token. Always verify the signature, expiry and audience with a library or a JWT authorizer.

Oops

Using the user's email as their primary key in your database.

Do this instead

Emails change. Store the sub claim, which never changes for a user.

Your mission

Add login to an API

~40 min

Quick check

Your app needs users to log in and call your own API. Which Cognito feature do you need first?

Go deeper

  1. 1Create a user pool and sign up a test user through the managed login page.
  2. 2Protect an API Gateway route with a JWT authorizer using the user pool.
  3. 3Decode the ID token at jwt.io to see the claims it contains.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge