title: Howto get req.user in services in Nest JS
body: <p>In a controller, I add the user object with a guard, inject some service and call that service to get some response. I have removed a lot of code for brevity.</p>
  <pre><code>@Controller()
  @UseGuards(AuthGuard())
  export class UserController() {
     constructor(private readonly userService: UsersService) {
     }
  
     @Get(':id')
     async findOne(@Param('id') id) {
        return await this.userService.findOne(id);
     }
  }
  </code></pre>
  <p>Since I have the <code>AuthGuard</code>, I now know the user is logged in before entering <code>:id</code> route.</p>
  <p>In the service I would do something like</p>
  <pre><code>@Injectable()
  export class UsersService {
     async findOne(id: number): Promise&lt;User&gt; {
        return await this.usersRepository.findOne({where: {id: id}});
     }
  }
  </code></pre>
  <p>But of course we want to have some checks that the logged in user has access to the user it is querying. The question is now how do I get the current logged in user. I can send it as a parameter from the controller, but since a lot of the backend would need security checked on the current user, I'm not sure that is a good idea.</p>
  <pre><code>@Get(':id')
  async findOne(@Param('id') id, @Req() req: any) {
     return await this.userService.findOne(id, req.user);
  }
  </code></pre>
  <p>Ideally, which doesn't work, I would be able to get it in the UserService:</p>
  <pre><code>async findOne(id: number, @Req req: any): Promise&lt;User&gt; {
     if (id === req.user.id || req.user.roles.contains('ADMIN')) {
        return await this.userRepository.findOne({where: {id: id}});
     }
  }
  </code></pre>
  <p>Or perhaps through injection in the <code>UserService</code> constructor</p>
  <pre><code>constructor(@Inject(REQUEST_OBJECT) private readonly req: any) {}
  </code></pre>
  <p>So, is there a better way to send the user object through the backend than always sending the request object in each function call?</p>