Package Exports
- @nestjs-cognito/auth
- @nestjs-cognito/auth/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 (@nestjs-cognito/auth) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
@nestjs-cognito/auth
Description
AWS Cognito utilities module for Nest.
Installation
npm i --save @nestjs-cognito/authConfiguration
Options params
You can find more details here.
Synchronously
Use CognitoAuthModule.register method with options of CognitoModuleOptions interface
import { CognitoAuthModule } from "@nestjs-cognito/auth";
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 { CognitoAuthModule } from "@nestjs-cognito/auth";
import { Module } from "@nestjs/common";
import { ConfigModule, ConfigService } from "@nestjs/config";
@Module({
imports: [
CognitoAuthModule.registerAsync({
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => ({
region: configService.get("COGNITO_REGION"),
}),
inject: [ConfigService],
}),
],
})
export class AppModule {}Usage
You can use the built-in @nestjs-cognito/auth decorators and guards.
Built-in decorators and guards
- Decorate the controller with the
@Authenticationdecorator or with the@UseGuardsdecorator to apply theAuthenticationGuardto the controller in order to ensure that the user is authenticated. - Decorate the resolver with the
@Authorizationdecorator or with the@UseGuardsdecorator to apply theAuthorizationGuardin order to ensure that the user is authorized. - Decorate method arguments with the
@CurrentUserdecorator 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/auth";
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/auth";
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.