lujakob/nestjs-realworld-example-app

i have a problem about the controllers

yoohan-dex opened this issue · 3 comments

  @Post(':slug/favorite')
  async favorite(@Headers('authorization') authorization: string, @Param('slug') slug) {
    return await this.articleService.favorite(this.getUserIdFromToken(authorization), slug);
  }

  @Delete(':slug/favorite')
  async unFavorite(@Headers('authorization') authorization: string, @Param('slug') slug) {
    return await this.articleService.unFavorite(this.getUserIdFromToken(authorization), slug);
  }

like this section is it feels like have a tiny redundance.

I mean that can auth be taken in middleware like koa, and we don't need to pay attention to it?

@yoohan-dex Good point. I've refactored the code. Now I add the userId in the authentication middleware and get it in the controller from the request object. Thanks.
https://github.com/lujakob/nestjs-realworld-example-app/blob/master/src/user/auth.middleware.ts#L25

Yeah, even better. I didn't see the section custom decorators yet.

https://docs.nestjs.com/custom-decorators