Back to Skills

nestjs-guards-interceptors

verified

Use when nestJS guards and interceptors for auth, logging, and transformation. Use when implementing cross-cutting concerns.

View on GitHub

Marketplace

han

TheBushidoCollective/han

Plugin

jutsu-nestjs

Technique

Repository

TheBushidoCollective/han
60stars

jutsu/jutsu-nestjs/skills/nestjs-guards-interceptors/SKILL.md

Last Verified

January 24, 2026

Install Skill

Select agents to install to:

Scope:
npx add-skill https://github.com/TheBushidoCollective/han/blob/main/jutsu/jutsu-nestjs/skills/nestjs-guards-interceptors/SKILL.md -a claude-code --skill nestjs-guards-interceptors

Installation paths:

Claude
.claude/skills/nestjs-guards-interceptors/
Powered by add-skill CLI

Instructions

# NestJS Guards and Interceptors

Master NestJS guards and interceptors for implementing authentication,
authorization, logging, and request/response transformation.

## Guards Fundamentals

Understanding CanActivate and ExecutionContext.

```typescript
import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common';
import { Observable } from 'rxjs';

@Injectable()
export class BasicGuard implements CanActivate {
  canActivate(
    context: ExecutionContext,
  ): boolean | Promise<boolean> | Observable<boolean> {
    const request = context.switchToHttp().getRequest();
    return this.validateRequest(request);
  }

  private validateRequest(request: any): boolean {
    // Simple validation logic
    return !!request.headers.authorization;
  }
}

// ExecutionContext provides context about current request
@Injectable()
export class ContextAwareGuard implements CanActivate {
  canActivate(context: ExecutionContext): boolean {
    // Get HTTP context
    const httpContext = context.switchToHttp();
    const request = httpContext.getRequest();
    const response = httpContext.getResponse();

    // Get handler and class information
    const handler = context.getHandler();
    const controller = context.getClass();

    console.log(`Handler: ${handler.name}`);
    console.log(`Controller: ${controller.name}`);

    return true;
  }
}

// Usage in controller
import { Controller, Get, UseGuards } from '@nestjs/common';

@Controller('users')
@UseGuards(BasicGuard)
export class UserController {
  @Get()
  findAll() {
    return [];
  }

  @Get('profile')
  @UseGuards(ContextAwareGuard)  // Method-level guard
  getProfile() {
    return { name: 'John' };
  }
}
```

## Authentication Guards

JWT, session, and API key authentication patterns.

```typescript
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';

@Injectable()
export class JwtAuthGuard implements CanActivate {
  constructor(private jwtService: Jw

Validation Details

Front Matter
Required Fields
Valid Name Format
Valid Description
Has Sections
Allowed Tools
Instruction Length:
27781 chars