-
Notifications
You must be signed in to change notification settings - Fork 1
lambda
Eugene Lazutkin edited this page Jul 17, 2026
·
2 revisions
The AWS Lambda port of the middleware family: cognito-toolkit/lambda, wrapping (event, context) => result handlers — including dynamodb-toolkit's createLambdaHandler. API Gateway v1, API Gateway v2 / Function URL, and ALB event shapes are auto-detected.
import {makeAuth} from 'cognito-toolkit/lambda';
function makeAuth<Payload extends object = JwtPayload>(options: AuthOptions<Payload>): Auth<Payload>;
type LambdaEvent = APIGatewayProxyEvent | APIGatewayProxyEventV2 | ALBEvent; // from @types/aws-lambda
type LambdaResult = APIGatewayProxyResult | APIGatewayProxyStructuredResultV2 | ALBResult;
type LambdaHandler = (event: LambdaEvent, ...rest: any[]) => LambdaResult | Promise<LambdaResult>;
interface Auth<Payload> {
getUser(event: LambdaEvent): Promise<AuthUser<Payload> | null>; // memoized per event
isAuthenticated(handler: LambdaHandler): LambdaHandler;
hasGroup(group: string): (handler: LambdaHandler) => LambdaHandler;
hasScope(scope: string): (handler: LambdaHandler) => LambdaHandler;
isAllowed(validator: (event: LambdaEvent, groups: string[], scopes: string[]) => boolean | Promise<boolean>): (handler: LambdaHandler) => LambdaHandler;
setAuthCookie(event: LambdaEvent, result: LambdaResult, cookieOptions?: CookieOptions): Promise<LambdaResult>;
}
type AuthUser<Payload> = Payload & {_token: string};Options: see Middleware ports § Options — no stateUserProperty here. Event/result types come from @types/aws-lambda; the payload type flows from the verifier.
import {CognitoJwtVerifier} from 'cognito-toolkit';
import {makeAuth} from 'cognito-toolkit/lambda';
const verifier = CognitoJwtVerifier.create({userPoolId: 'us-east-1_MY_POOL', clientId: 'my-app-client', tokenUse: 'access'});
const auth = makeAuth({verifier});
export const handler = auth.hasGroup('admins')(async (event, context) => ({statusCode: 200, body: 'ok'}));Behind API Gateway proper, prefer the built-in Cognito / JWT authorizer — it rejects bad tokens without invoking your function. This port's niche:
- Function URLs — their only auth options are IAM or none, so a public URL wanting Cognito JWT auth must verify in code.
-
ALB targets — when the ALB's
authenticate-cognitoaction forwardsx-amzn-oidc-data, pair this port with aws-jwt-verify'sAlbJwtVerifieras theverifier. -
Local-debug bridges — e.g. dynamodb-toolkit's
lambda/local.jsdriving the composed, guarded handler from real HTTP traffic.
- Event shapes: ALB is detected by
requestContext.elb, v2 / Function URL byversion === '2.0', everything else is v1. Headers are read case-insensitively (v2 lowercases them, v1/ALB preserve the sender's casing); v2 cookies come fromevent.cookies; ALB multi-value mode (multiValueHeaderswith null-stampedheaders) is honored on reads and mirrored on responses. - Denials are result envelopes —
{statusCode: 401 | 403, body: ''}, withmultiValueHeaders: {}added when the trigger runs in ALB multi-value mode. -
setAuthCookieis async, shape-aware (v2cookiesarray; v1/ALB headers, lifted tomultiValueHeaderswhen required), and mutates + returns the result. Automatic refresh viasetAuthCookieOptionsapplies to results passing through guards.Domaindefaults torequestContext.domainNameor theHostheader, port stripped.
Start here
Reference
Under the hood