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) {
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',

@ -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);

@ -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);

Loading…
Cancel
Save