Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

167
Visualizações
Getting current logged in user - NestJS

So. I set up a DELETEmethod for deleting Rents (a Rent happens when a User rents out an Item in my database).

I would like to make sure to check that the only user that can cancel the rent is the one that actually rented the item. I already implemented a check that prevents the user to rent an item from himself.

So basically before executing the delete query on that row in Rents table, i want to check that the ID of the current logged in user matches with the userID of that specific row in Rents table, only then will the query be executed

Rents table entry example enter image description here

userId is the ID of the User that rented an Item (itemId).

I set up a custom GetUser() decorator

import { User } from './../entities/user.entity';
import { createParamDecorator, ExecutionContext } from '@nestjs/common';

export const GetUser = createParamDecorator(
  (data, ctx: ExecutionContext): User => {
    const req = ctx.switchToHttp().getRequest();
    return req.user;
  },
);

Now, when i call my getMyItemsmethod to get all the items that the current user owns, the GetUser()decorator works fine as i can access the user on the request object.

GetMyItems controller

  @Get('/my')
  @ApiBearerAuth()
  async getMyItems(@GetUser() user: User): Promise<Item[]> {
    return this.itemsService.getMyItems(user);
  }

When I try to do the same for the mentioned cancelRent method it doesn't seem to work even though everything is setup the same way.

cancelRent controller

@Delete('/:id/cancel')
  async cancelRent(
    @Param('id') id: number,
    @GetUser() user: User,
  ): Promise<void> {
    return this.rentsService.cancelRent(id, user);
  }

When i run console.log(user) in the cancelRent controller i get undefined, but when I run it in the getMyItems method it returns the current user normally. Is it because its a DELETE request and not a GET request?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

cancelRent() is missing @ApiBearerAuth()

about 4 years ago · Juan Pablo Isaza Relatório

0

I forgot to include AuthGuard() in my Rents Controller.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda