JSPM

passport-cognito-jwt

1.0.3
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • 0
  • Score
    100M100P100Q26615F
  • License MIT

Passport authentication strategy for Cognito JWT token

Package Exports

  • passport-cognito-jwt
  • passport-cognito-jwt/dist/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (passport-cognito-jwt) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

passport-cognito-jwt

Based on passport-jwt

A very simple Passport strategy to authenticate with AWS Cognito.

This module lets you authenticate endpoints when using Cognito Auth in a NestJS application.

Install

npm install passport-cognito-jwt

Usage

NestJS TypeScript usage example:

Strategy name is: cognito-jwt.

auth.strategy.ts

import { PassportStrategy } from '@nestjs/passport';
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { CognitoJwtStrategy } from 'passport-cognito-jwt';

@Injectable()
export class AuthStrategy extends PassportStrategy(Strategy) {
  private verifier: CognitoJwtVerifierSingleUserPool<{
    userPoolId: string
    tokenUse: 'access'
    clientId: string
  }>

  constructor(
    private configService: AppConfigService,
  ) {
    super()

    this.verifier = CognitoJwtVerifier.create({
      userPoolId: this.configService.userPoolId,
      tokenUse: 'access',
      clientId: this.configService.clientId,
    })
  }

  validate = async (token: string): Promise<AuthenticatedUser> => {
    const payload = await this.verifier.verify(token)

    // Access cognitoId using `payload.sub`
  }
}

auth.module.ts

import { Module } from '@nestjs/common'
import { PassportModule } from '@nestjs/passport'
import { AuthStrategy } from './auth.strategy'

@Module({
  imports: [
    PassportModule.register({ defaultStrategy: 'cognito-jwt' }),
  ],
  providers: [AuthStrategy],
})
export class AuthModule {}