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, UseInterceptors } from '@nestjs/common'; | |
import { LoggingInterceptor } from './logging.interceptor'; | |
@Controller('users') | |
export class UsersController { | |
@Get() | |
@UseInterceptors(LoggingInterceptor) // Apply logging interceptor to this route | |
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 { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common'; | |
import { Observable } from 'rxjs'; | |
import { tap } from 'rxjs/operators'; | |
@Injectable() | |
export class LoggingInterceptor implements NestInterceptor { | |
intercept(context: ExecutionContext, next: CallHandler): Observable<any> { | |
console.log('Before handler execution'); | |
const startTime = new Date(); | |
// Proceed with the request and log the response |
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'; | |
import { RoleGuard } from './role.guard'; | |
@Controller('admin') | |
export class AdminController { | |
@Get() | |
@UseGuards(AuthGuard, new RoleGuard('admin')) // applying mulitple guards | |
findAll() { | |
return { message: 'Admin dashboard' }; |
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' }; |
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
// 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 { 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
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 { 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
// 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); | |
} |