This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Custom Pipe used in a Controller | |
| import { Controller, Post, Body } from '@nestjs/common'; | |
| import { UppercasePipe } from './uppercase.pipe'; | |
| @Controller('users') | |
| export class UsersController { | |
| @Post() | |
| create(@Body('name', new UppercasePipe()) name: string) { | |
| return { message: 'User created', name }; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { IsString, IsInt, MinLength, Min } from 'class-validator'; | |
| export class CreateUserDto { | |
| @IsString() | |
| @MinLength(3) | |
| name: string; | |
| @IsInt() | |
| @Min(18) | |
| age: number; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Controller, Post, Body, UsePipes, ValidationPipe } from '@nestjs/common'; | |
| import { CreateUserDto } from './create-user.dto'; | |
| @Controller('users') | |
| export class UsersController { | |
| @Post() | |
| @UsePipes(new ValidationPipe()) // Apply validation | |
| create(@Body() createUserDto: CreateUserDto) { | |
| return { message: 'User created', data: createUserDto }; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // main.ts | |
| import { NestFactory } from '@nestjs/core'; | |
| import { AppModule } from './app.module'; | |
| import { ValidationPipe } from '@nestjs/common'; | |
| async function bootstrap() { | |
| const app = await NestFactory.create(AppModule); | |
| app.useGlobalPipes(new ValidationPipe()); // Apply to all routes | |
| await app.listen(3000); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common'; | |
| @Injectable() | |
| export class AuthGuard implements CanActivate { | |
| canActivate(context: ExecutionContext): boolean { | |
| const request = context.switchToHttp().getRequest(); | |
| // Check if request has a valid user (authentication logic) | |
| return Boolean(request.user); // Assume `request.user` is set after successful login | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Controller, Get, UseGuards } from '@nestjs/common'; | |
| import { AuthGuard } from './auth.guard'; | |
| @Controller('users') | |
| export class UsersController { | |
| @Get() | |
| @UseGuards(AuthGuard) // Apply guard to this method | |
| findAll() { | |
| return { message: 'List of users' }; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Controller, Get, UseGuards } from '@nestjs/common'; | |
| import { AuthGuard } from './auth.guard'; | |
| @Controller('users') | |
| @UseGuards(AuthGuard) // Apply guard to all routes in this controller | |
| export class UsersController { | |
| @Get() | |
| findAll() { | |
| return { message: 'List of users' }; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // main.ts | |
| import { NestFactory } from '@nestjs/core'; | |
| import { AppModule } from './app.module'; | |
| import { AuthGuard } from './auth.guard'; | |
| async function bootstrap() { | |
| const app = await NestFactory.create(AppModule); | |
| app.useGlobalGuards(new AuthGuard()); // Apply globally | |
| await app.listen(3000); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common'; | |
| @Injectable() | |
| export class RoleGuard implements CanActivate { | |
| constructor(private requiredRole: string) {} | |
| canActivate(context: ExecutionContext): boolean { | |
| const request = context.switchToHttp().getRequest(); | |
| const user = request.user; // User is assumed to be attached to the request | |
| return user?.role === this.requiredRole; // Check if user's role matches |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // guard implementation in Controller | |
| import { Controller, Get, UseGuards } from '@nestjs/common'; | |
| import { RoleGuard } from './role.guard'; | |
| @Controller('admin') | |
| export class AdminController { | |
| @Get() | |
| @UseGuards(new RoleGuard('admin')) // Apply the guard for 'admin' role | |
| findAll() { | |
| return { message: 'Admin dashboard' }; |