0

I have a microservice setup with an api gateway accepting http requests and passing them to miscroservices (media-service, payment-service, etc.) using message pattern.

I can access the request.user in the api-gateway which gives me the authenticated user from the decoded jwt. What i would like is, to pass this to the media microservice and access it within the functions in media service.

How do i achieve this?

controller in api-gateway

export class MediasController {
    constructor(@Inject('MEDIA_SERVICE') private client: ClientProxy) { }

    @Get()
    @UseGuards(AuthGuard('jwt'))
    async find(@Query() query: QueryArgs): Promise<any> {
        const pattern = { cmd: 'media-find' };
        return await this.client.send<any>(pattern, query)
    }
}

controller & service in media microservice

medias.controller.ts
@Controller({ path: 'medias', scope: Scope.REQUEST })
export class MediasController {
    constructor(private readonly mediasService: MediasService) { }
    
    @MessagePattern({ cmd: 'media-find' })
    async find(query: any): Promise<any> {
        return await this.mediasService.find(query)
    }
}
medias.service.ts
export class MediasService {
    constructor(@InjectRepository(Media) private readonly mediaRepository: Repository<Media>) {
    }

    async find(query): Promise<Media[]> {
        // access request.user here 
        return await this.mediaRepository.find(query);
    }
}
Community
  • 1
  • 1
mushfau
  • 63
  • 3
  • 5

0 Answers0