From 02e81c73b5aefaf99236527584d6d867ec9b5652 Mon Sep 17 00:00:00 2001 From: Mahdi Date: Mon, 6 Jan 2025 01:20:41 +0330 Subject: [PATCH] Add protection & authorization support to our routes --- src/modules/auth/jwt.strategy.ts | 2 +- src/modules/products/products.controller.ts | 5 +++++ src/modules/users/users.controller.ts | 5 +++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/modules/auth/jwt.strategy.ts b/src/modules/auth/jwt.strategy.ts index 52ae537..c223b61 100644 --- a/src/modules/auth/jwt.strategy.ts +++ b/src/modules/auth/jwt.strategy.ts @@ -14,7 +14,7 @@ export class JwtStrategy extends PassportStrategy(Strategy) { } async validate(payload: any) { - const user = await this.userService.findOne(payload.id); + const user = await this.userService.findOne(payload.uuid); if (!user) { throw new UnauthorizedException( 'You are not authorized to perform the operation', diff --git a/src/modules/products/products.controller.ts b/src/modules/products/products.controller.ts index 6456a06..d1ff944 100644 --- a/src/modules/products/products.controller.ts +++ b/src/modules/products/products.controller.ts @@ -6,15 +6,18 @@ import { Patch, Param, Delete, + UseGuards, } from '@nestjs/common'; import { ProductsService } from './products.service'; import { CreateProductDto, UpdateProductDto } from './dto'; import { UUID } from 'crypto'; +import { AuthGuard } from '@nestjs/passport'; @Controller('products') export class ProductsController { constructor(private readonly productsService: ProductsService) {} + @UseGuards(AuthGuard('jwt')) @Post() create(@Body() createProductDto: CreateProductDto) { return this.productsService.create(createProductDto); @@ -30,11 +33,13 @@ export class ProductsController { return this.productsService.findOne(+id); } + @UseGuards(AuthGuard('jwt')) @Patch(':id') update(@Param('id') id: string, @Body() updateProductDto: UpdateProductDto) { return this.productsService.update(+id, updateProductDto); } + @UseGuards(AuthGuard('jwt')) @Delete(':id') remove(@Param('id') id: string) { return this.productsService.remove(+id); diff --git a/src/modules/users/users.controller.ts b/src/modules/users/users.controller.ts index 8ce59a8..adc6144 100644 --- a/src/modules/users/users.controller.ts +++ b/src/modules/users/users.controller.ts @@ -6,10 +6,12 @@ import { Param, Patch, Post, + UseGuards, } from '@nestjs/common'; import { UsersService } from './users.service'; import { CreateUserDto, UpdateUserDto } from './dto'; import { UUID } from 'crypto'; +import { AuthGuard } from '@nestjs/passport'; @Controller('users') export class UsersController { @@ -25,16 +27,19 @@ export class UsersController { return this.usersService.findOne(id); } + @UseGuards(AuthGuard('jwt')) @Post() create(@Body() createUserDto: CreateUserDto) { return this.usersService.create(createUserDto); } + @UseGuards(AuthGuard('jwt')) @Patch(':id') update(@Param('id') id: UUID, @Body() updateUserDto: UpdateUserDto) { return this.usersService.update(id, updateUserDto); } + @UseGuards(AuthGuard('jwt')) @Delete(':id') remove(@Param('id') id: UUID) { return this.usersService.remove(id);