🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

nestjs-cognito

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nestjs-cognito

NestJS Cognito Authentication Module

1.0.0
latest
Source
npm
Version published
Weekly downloads
13
8.33%
Maintainers
1
Weekly downloads
 
Created
Source

NestJS-Cognito

Node.js CI Coverage Status

Description

AWS Cognito utilities module for Nest.

Installation

npm i --save @nestjs/jwt nestjs-cognito

Configuration

Options params

/**
 * @interface CognitoModuleOptions - Options for the CognitoModule
 * @property {string} region - The region
 */
export type CognitoModuleOptions = CognitoIdentityProviderClientConfig &
  Required<Pick<CognitoIdentityProviderClientConfig, 'region'>>;

/**
 * @interface CognitoModuleOptionsFactory - Metadata for the CognitoModule
 * @property {() => Promise<CognitoModuleOptions>} createCognitoModuleOptions - A factory function to create the CognitoModuleOptions
 * @property {Type<any>[]} imports - The imports to be used by the module
 * @property {Provider[]} providers - The providers to be used by the module
 * @property {(string | Provider)[]} exports - The exports to be used by the module
 * @property {string} name - The name of the module
 */
export interface CognitoModuleOptionsFactory {
  createCognitoModuleOptions():
    | Promise<CognitoModuleOptions>
    | CognitoModuleOptions;
}

/**
 * @interface CognitoModuleAsyncOptions - Options for the CognitoModule
 * @property {Function} imports - Imports the module asyncronously
 * @property {Function} inject - Injects the module asyncronously
 * @property {CognitoModuleOptions} useFactory - The factory function to create the CognitoModuleOptions
 * @property {CognitoModuleOptions} useClass - The class to create the CognitoModuleOptions
 * @property {CognitoModuleOptions} useExisting - The existing instance of the CognitoModuleOptions
 */
export interface CognitoModuleAsyncOptions
  extends Pick<ModuleMetadata, 'imports'> {
  useExisting?: Type<CognitoModuleOptionsFactory>;
  useClass?: Type<CognitoModuleOptionsFactory>;
  useFactory?: (
    ...args: any[]
  ) => Promise<CognitoModuleOptions> | CognitoModuleOptions;
  inject?: any[];
  extraProviders?: Provider[];
}

Synchronously

Use CognitoModule.register method with options of CognitoModuleOptions interface

import { CognitoModule } from 'nestjs-cognito';
import { Module } from '@nestjs/common';

@Module({
  imports: [
    CognitoModule.register({
      region: 'eu-west-X',
    }),
  ],
})
export class AppModule {}

Asynchronously

With CognitoModule.registerAsync you can import your ConfigModule and inject ConfigService to use it in useFactory method. It's also possible to use useExisting or useClass. You can find more details here.

Here's an example:

import { CognitoModule } from 'nestjs-cognito';
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';

@Module({
  imports: [
    CognitoModule.registerAsync({
      imports: [ConfigModule],
      useFactory: async (configService: ConfigService) => ({
        region: configService.get('COGNITO_REGION'),
      }),
      inject: [ConfigService],
    }),
  ],
})
export class AppModule {}

Usage

You can use the cognito identity provider injectors or the built-in nestjs-cognito decorators and guards.

Cognito Identity Provider

import {
  InjectCognitoIdentityProvider,
  InjectCognitoIdentityProviderClient,
} from 'nestjs-cognito';

export class MyService {
  constructor(
    @InjectCognitoIdentityProvider()
    private readonly client: CognitoIdentityProvider,
    @InjectCognitoIdentityProviderClient()
    private readonly cognitoIdentityProviderClient: CognitoIdentityProviderClient,
  ) {}
}

Built-in decorators and guards

  • Decorate the controller with the @Authentication decorator or with the @UseGuards decorator to apply the AuthenticationGuard to the controller in order to ensure that the user is authenticated.
  • Decorate the resolver with the @Authorization decorator or with the @UseGuards decorator to apply the AuthorizationGuard in order to ensure that the user is authorized.
  • Decorate method arguments with the @CurrentUser decorator to get the current user.

During the authorization process, we already check if the user is authenticated, so you don't need to use authentication guard or decorator.

In addition, you can find more details about @UseGuards decorator here.

Here is an example that shows how to use authentication:

import {
  Authentication,
  AuthenticationGuard,
  CurrentUser,
} from 'nestjs-cognito';
import { Controller, Get, UseGuards } from '@nestjs/common';

@Controller('dogs')
@Authentication()
export class DogsController {
  @Get()
  findAll(@CurrentUser() me: User): string {
    return 'This action returns all my dogs';
  }
}

@Controller('cats')
@UseGuards(AuthenticationGuard)
export class CatsController {
  @Get()
  findAll(@CurrentUser() me: User): string {
    return 'This action returns all my cats';
  }
}

@Controller('dogs')
export class DogsController {
  @Get()
  @UseGuards(AuthenticationGuard)
  findAll(@CurrentUser() me: User): string {
    return 'This action returns all my dogs';
  }
}

Here is an example that shows how to use authorization:

import { Authorization, AuthorizationGuard, CurrentUser } from 'nestjs-cognito';
import { Controller, Get, UseGuards } from '@nestjs/common';

@Controller('dogs')
@Authorization({
  allowedGroups: ['user', 'admin'],
  requiredGroups: ['moderator'],
  prohibitedGroups: ['visitor'],
})
export class DogsController {
  @Get()
  findAll(@CurrentUser() me: User): string {
    return 'This action returns all my dogs';
  }
}

@Controller('cats')
@Authorization(['user']) // allowedGroups by default
export class CatsController {
  @Get()
  findAll(@CurrentUser() me: User): string {
    return 'This action returns all my cats';
  }
}

@Controller('cats')
@UseGuards(
  AuthorizationGuard({
    allowedGroups: ['user', 'admin'],
    requiredGroups: ['moderator'],
    prohibitedGroups: ['visitor'],
  }),
)
export class CatsController {
  @Get()
  findAll(@CurrentUser() me: User): string {
    return 'This action returns all my cats';
  }
}

@Controller('cats')
export class CatsController {
  @Get()
  @UseGuards(AuthorizationGuard(['user', 'admin']))
  findAll(@CurrentUser() me: User): string {
    return 'This action returns all my cats';
  }
}

License

NestJS-Cognito is MIT licensed.

Keywords

nestjs

FAQs

Package last updated on 17 Jul 2022

Did you know?

Socket

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.

Install

Related posts