title: How to get JWT token from headers in controller
body: <p>Please, help me to find how to optimize my code.
  I need to limit the data for logged user. To do that, I need to get UUID from JWT token from Request. But I don't like my approach because I have duplicates of code:</p>
  
  <pre class="lang-js prettyprint-override"><code>const jwt = request.headers.authorization.replace('Bearer ', '');
  const json = this.jwtService.decode(jwt, { json: true }) as { uuid: string };
  </code></pre>
  
  <p>Any one know how I can optimize that?</p>
  
  <p>Here is my controller's code.</p>
  
  <pre class="lang-js prettyprint-override"><code>import { Controller, Get, Put, Body, Param, UseGuards, Req } from '@nestjs/common';
  import { SettingService } from '../services';
  import { AuthGuard } from '@nestjs/passport';
  import { ResultInterface } from '../interfaces';
  import { Request } from 'express';
  import { JwtService } from '@nestjs/jwt';
  
  @Controller('settings')
  export class SettingController {
    /**
     * @param service
     * @param jwtService
     */
    constructor(private readonly service: SettingService,
                private readonly jwtService: JwtService) {
    }
  
    @UseGuards(AuthGuard('jwt'))
    @Get()
    async findAll(@Req() request: Request): Promise&lt;ResultInterface&gt; {
      const jwt = request.headers.authorization.replace('Bearer ', '');
      const json = this.jwtService.decode(jwt, { json: true }) as { uuid: string };
      const data = await this.service.findAll(json.uuid);
      return { rows: data };
    }
  
    @UseGuards(AuthGuard('jwt'))
    @Get(':id')
    async findOne(@Param('id') id: number, @Req() request: Request): Promise&lt;ResultInterface&gt; {
      const jwt = request.headers.authorization.replace('Bearer ', '');
      const json = this.jwtService.decode(jwt, { json: true }) as { uuid: string };
      const data = await this.service.findOneById(id, json.uuid);
      return { row: data };
    }
  
    @UseGuards(AuthGuard('jwt'))
    @Put()
    update(@Body() data: any, @Req() request: Request): Promise&lt;any&gt; {
      const jwt = request.headers.authorization.replace('Bearer ', '');
      const json = this.jwtService.decode(jwt, { json: true }) as { uuid: string };
      return this.service.update(data, json.uuid);
    }
  }
  </code></pre>