@nestjs-cognito is a powerful, type-safe library that provides seamless integration of AWS Cognito authentication into NestJS applications. Designed to simplify authentication workflows, this library offers a comprehensive solution for developers working with AWS Cognito and NestJS.
- π Robust Authentication: Seamless NestJS integration with AWS Cognito
- π‘οΈ Built-in Protection: Advanced authentication guards and decorators
- π GraphQL Support: First-class support for GraphQL applications
- π§ͺ Testing Utilities: Comprehensive testing tools for authentication flows
- π Type Safety: Fully typed implementation for enhanced developer experience
- βοΈ Flexible Configuration: Easy synchronous and asynchronous module registration
Install the core packages with npm:
# Core and authentication modules
npm install @nestjs-cognito/core @nestjs-cognito/auth
# Optional: GraphQL support
npm install @nestjs-cognito/graphql
# Optional: Testing utilities (dev dependency)
npm install -D @nestjs-cognito/testing
import { CognitoAuthModule } from '@nestjs-cognito/auth';
@Module({
imports: [
CognitoAuthModule.register({
region: 'us-east-1',
userPoolId: 'your-user-pool-id',
clientId: 'your-client-id',
}),
],
})
export class AppModule {}
import { CognitoAuthModule } from '@nestjs-cognito/auth';
import { ConfigService } from '@nestjs/config';
@Module({
imports: [
CognitoAuthModule.registerAsync({
useFactory: (configService: ConfigService) => ({
region: configService.get('AWS_REGION'),
userPoolId: configService.get('AWS_USER_POOL_ID'),
clientId: configService.get('AWS_CLIENT_ID'),
}),
inject: [ConfigService],
}),
],
})
export class AppModule {}
import { GqlCognitoUser } from '@nestjs-cognito/graphql';
@Resolver()
export class UserResolver {
@Query()
async getProfile(@GqlCognitoUser() user) {
return user;
}
}
The testing module provides comprehensive end-to-end (e2e) testing utilities to validate Cognito authentication behavior in real-world scenarios:
import { CognitoTestingModule } from '@nestjs-cognito/testing';
import { Test } from '@nestjs/testing';
describe('Cognito Authentication E2E Tests', () => {
let app: INestApplication;
beforeAll(async () => {
const moduleRef = await Test.createTestingModule({
imports: [
CognitoTestingModule.register({
// Configure with actual AWS Cognito credentials
region: process.env.AWS_REGION,
userPoolId: process.env.AWS_USER_POOL_ID,
clientId: process.env.AWS_CLIENT_ID,
}),
AppModule, // Your application module
],
}).compile();
app = moduleRef.createNestApplication();
await app.init();
});
it('should perform real-world authentication flow', async () => {
// Conduct end-to-end tests with actual Cognito service
// Test user sign-up, sign-in, token validation, etc.
});
afterAll(async () => {
await app.close();
});
});
Key Features of E2E Testing Module:
- Validate actual Cognito authentication behaviors
- Test complete authentication flows against real AWS Cognito services
- Verify integration of Cognito with NestJS application
- Ensure proper token generation, validation, and authorization
- Simulate real-world authentication scenarios
Contributions are welcome! Please follow these steps:
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Distributed under the MIT License. See LICENSE
for more information.
Project Link: https://github.com/Lokicoule/nestjs-cognito