Tự build authentication cho app mới mất hàng tuần. Cognito làm tất cả — nhưng ít người biết JWT Authorizer tích hợp API Gateway mới là killer feature thực sự. Mình thường dùng Cognito vì lý do đơn giản: không muốn build forgot password flow lần thứ ba.


  flowchart LR
    User["User"] -->|"login"| Cognito["Cognito User Pool"]
    Cognito -->|"JWT (id + access + refresh)"| User
    User -->|"Authorization: Bearer JWT"| APIG["API Gateway<br/>JWT Authorizer"]
    APIG --> Lambda["Lambda"]
import { JwtRsaVerifier } from "aws-jwt-verify";

const verifier = JwtRsaVerifier.create({
  issuer: `https://cognito-idp.ap-southeast-1.amazonaws.com/ap-southeast-1_xxx`,
  audience: "abc123clientid",
  tokenUse: "access",
});

const payload = await verifier.verify(token); // { sub, email, scope, ... }
TriggerWhenUse
Pre Sign-upBefore user creationValidate email domain, block spam
Post ConfirmationAfter email verifyCreate user profile in DB, welcome email
Pre AuthenticationBefore loginCustom rate limiting, IP check
Pre Token GenerationBefore JWT creationAdd custom claims to token

Bài sau: Phần 30: Secrets Manager & SSM Parameter Store