
Product
A Fresh Look for the Socket Dashboard
We’ve redesigned the Socket dashboard with simpler navigation, less visual clutter, and a cleaner UI that highlights what really matters.
@nestjs-cognito/core
Advanced tools
This package provides the core functionality and adapters for AWS Cognito integration in NestJS applications. It serves as the foundation for other @nestjs-cognito packages.
npm install @nestjs-cognito/core
The Core package provides essential adapters and interfaces that are used by other packages in the @nestjs-cognito ecosystem:
import { CognitoIdentityProvider } from "@aws-sdk/client-cognito-identity-provider";
import { InjectCognitoIdentityProvider } from "@nestjs-cognito/core";
export class MyService {
constructor(
@InjectCognitoIdentityProvider()
private readonly client: CognitoIdentityProvider
) {}
}
The package provides two mutually exclusive JWT verification implementations that share the same injection token. The implementation you get depends on your module configuration:
Both implementations perform comprehensive JWT claims verification according to AWS Cognito standards:
exp
claim to ensure the token hasn't expiredaud
claim matches the app's client IDclient_id
claim matches the app's client IDiss
claim matches your Cognito User Pool's issuer URL format:
https://cognito-idp.[REGION].amazonaws.com/[USER_POOL_ID]
token_use
claim:
access
for access tokensid
for ID tokensnull
to accept both token typesWhen you configure jwtVerifier
in your module, this implementation is used to verify JWTs issued by AWS Cognito:
import {
CognitoJwtVerifier,
InjectCognitoJwtVerifier
} from "@nestjs-cognito/core";
export class MyService {
constructor(
@InjectCognitoJwtVerifier()
private readonly jwtVerifier: CognitoJwtVerifier
) {}
async verifyToken(token: string) {
return this.jwtVerifier.verify(token);
}
}
When you configure jwtRsaVerifier
in your module, this implementation is used to verify JWTs using RSA public keys:
import {
JwtRsaVerifier,
InjectCognitoJwtVerifier
} from "@nestjs-cognito/core";
export class MyService {
constructor(
@InjectCognitoJwtVerifier()
private readonly jwtVerifier: JwtRsaVerifier
) {}
async verifyToken(token: string) {
return this.jwtVerifier.verify(token);
}
}
Important: While both verifier types use the same
@InjectCognitoJwtVerifier()
decorator, they are mutually exclusive in configuration. The type of verifier you get (CognitoJwtVerifier or JwtRsaVerifier) is determined by which configuration option you use in your module:
- Use
jwtVerifier
for Cognito JWT verification- Use
jwtRsaVerifier
for RSA JWT verificationYou cannot configure both types simultaneously in the same module.
Note: Choose either Cognito JWT Verification or RSA JWT Verification based on your authentication requirements. The
InjectCognitoJwtVerifier
decorator works with both verifier types, but you should configure only one type in your module.
import { CognitoModule } from '@nestjs-cognito/core';
@Module({
imports: [
CognitoModule.register({
// Required: Configure AWS Cognito Identity Provider
identityProvider: {
region: 'us-east-1',
credentials: {
accessKeyId: 'xxxxx',
secretAccessKey: 'xxxxx',
},
},
// Choose ONE of the following JWT verification methods:
// Option 1A: Configure Cognito JWT verification (Single User Pool)
jwtVerifier: {
userPoolId: 'us-east-1_xxxxx',
clientId: 'xxxxx',
tokenUse: 'access', // 'access', 'id', or null to accept both
// Optional: Configure JWK cache
additionalProperties: {
jwksCache: {
// Cache configuration
expiryTimeInHours: 24
}
}
},
// Option 1B: Configure Cognito JWT verification (Multiple User Pools)
jwtVerifier: [
{
userPoolId: 'us-east-1_pool1',
clientId: 'client1',
tokenUse: 'access' // 'access', 'id', or null to accept both
},
{
userPoolId: 'us-east-1_pool2',
clientId: 'client2',
tokenUse: 'id',
additionalProperties: {
jwksCache: {
expiryTimeInHours: 24
}
}
}
],
// Option 2A: Configure RSA JWT verification (Single Issuer)
jwtRsaVerifier: {
issuer: 'https://your-issuer.com',
jwksUri: 'https://your-jwks-uri.com/.well-known/jwks.json',
// Optional: Configure JWK cache
additionalProperties: {
jwksCache: {
expiryTimeInHours: 24
}
}
},
// Option 2B: Configure RSA JWT verification (Multiple Issuers)
jwtRsaVerifier: [
{
issuer: 'https://issuer1.com',
jwksUri: 'https://issuer1.com/.well-known/jwks.json'
},
{
issuer: 'https://issuer2.com',
jwksUri: 'https://issuer2.com/.well-known/jwks.json',
additionalProperties: {
jwksCache: {
expiryTimeInHours: 24
}
}
}
]
}),
],
})
export class AppModule {}
Note: When using multiple user pools or issuers, you can configure each one independently with its own settings, including separate JWK cache configurations. The verifier will automatically handle token verification against all configured sources.
The Core package provides the foundational adapters and interfaces that are utilized by the Auth package for implementing authentication and authorization features:
CognitoJwtVerifier
adapter for token verificationCognitoIdentityProvider
for user management operationsFor authentication features, it's recommended to use the @nestjs-cognito/auth package which builds upon these core components.
MIT
FAQs
Cognito Provider for NestJS
The npm package @nestjs-cognito/core receives a total of 7,965 weekly downloads. As such, @nestjs-cognito/core popularity was classified as popular.
We found that @nestjs-cognito/core demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
We’ve redesigned the Socket dashboard with simpler navigation, less visual clutter, and a cleaner UI that highlights what really matters.
Industry Insights
Terry O’Daniel, Head of Security at Amplitude, shares insights on building high-impact security teams, aligning with engineering, and why AI gives defenders a fighting chance.
Security News
MCP spec updated with structured tool output, stronger OAuth 2.1 security, resource indicators, and protocol cleanups for safer, more reliable AI workflows.