Add protection & authorization support to our routes

master
Mahdi 2 weeks ago
parent 1ad73af848
commit 02e81c73b5
  1. 2
      src/modules/auth/jwt.strategy.ts
  2. 5
      src/modules/products/products.controller.ts
  3. 5
      src/modules/users/users.controller.ts

@ -14,7 +14,7 @@ export class JwtStrategy extends PassportStrategy(Strategy) {
} }
async validate(payload: any) { async validate(payload: any) {
const user = await this.userService.findOne(payload.id); const user = await this.userService.findOne(payload.uuid);
if (!user) { if (!user) {
throw new UnauthorizedException( throw new UnauthorizedException(
'You are not authorized to perform the operation', 'You are not authorized to perform the operation',

@ -6,15 +6,18 @@ import {
Patch, Patch,
Param, Param,
Delete, Delete,
UseGuards,
} from '@nestjs/common'; } from '@nestjs/common';
import { ProductsService } from './products.service'; import { ProductsService } from './products.service';
import { CreateProductDto, UpdateProductDto } from './dto'; import { CreateProductDto, UpdateProductDto } from './dto';
import { UUID } from 'crypto'; import { UUID } from 'crypto';
import { AuthGuard } from '@nestjs/passport';
@Controller('products') @Controller('products')
export class ProductsController { export class ProductsController {
constructor(private readonly productsService: ProductsService) {} constructor(private readonly productsService: ProductsService) {}
@UseGuards(AuthGuard('jwt'))
@Post() @Post()
create(@Body() createProductDto: CreateProductDto) { create(@Body() createProductDto: CreateProductDto) {
return this.productsService.create(createProductDto); return this.productsService.create(createProductDto);
@ -30,11 +33,13 @@ export class ProductsController {
return this.productsService.findOne(+id); return this.productsService.findOne(+id);
} }
@UseGuards(AuthGuard('jwt'))
@Patch(':id') @Patch(':id')
update(@Param('id') id: string, @Body() updateProductDto: UpdateProductDto) { update(@Param('id') id: string, @Body() updateProductDto: UpdateProductDto) {
return this.productsService.update(+id, updateProductDto); return this.productsService.update(+id, updateProductDto);
} }
@UseGuards(AuthGuard('jwt'))
@Delete(':id') @Delete(':id')
remove(@Param('id') id: string) { remove(@Param('id') id: string) {
return this.productsService.remove(+id); return this.productsService.remove(+id);

@ -6,10 +6,12 @@ import {
Param, Param,
Patch, Patch,
Post, Post,
UseGuards,
} from '@nestjs/common'; } from '@nestjs/common';
import { UsersService } from './users.service'; import { UsersService } from './users.service';
import { CreateUserDto, UpdateUserDto } from './dto'; import { CreateUserDto, UpdateUserDto } from './dto';
import { UUID } from 'crypto'; import { UUID } from 'crypto';
import { AuthGuard } from '@nestjs/passport';
@Controller('users') @Controller('users')
export class UsersController { export class UsersController {
@ -25,16 +27,19 @@ export class UsersController {
return this.usersService.findOne(id); return this.usersService.findOne(id);
} }
@UseGuards(AuthGuard('jwt'))
@Post() @Post()
create(@Body() createUserDto: CreateUserDto) { create(@Body() createUserDto: CreateUserDto) {
return this.usersService.create(createUserDto); return this.usersService.create(createUserDto);
} }
@UseGuards(AuthGuard('jwt'))
@Patch(':id') @Patch(':id')
update(@Param('id') id: UUID, @Body() updateUserDto: UpdateUserDto) { update(@Param('id') id: UUID, @Body() updateUserDto: UpdateUserDto) {
return this.usersService.update(id, updateUserDto); return this.usersService.update(id, updateUserDto);
} }
@UseGuards(AuthGuard('jwt'))
@Delete(':id') @Delete(':id')
remove(@Param('id') id: UUID) { remove(@Param('id') id: UUID) {
return this.usersService.remove(id); return this.usersService.remove(id);

Loading…
Cancel
Save