#Cant bind user to fastify request in middleware

1 messages · Page 1 of 1 (latest)

weak temple
#

Can anyone tell me if a fastify request is immutable? I have a middleware where I bind a user object to the request

 async use(
        req: FastifyRequest['raw'],
        _res: FastifyReply['raw'],
        next: () => void,
    ): Promise<void> {
...
req['user'] = user;
this.log.debug({message: 'Success', data: req['user']});
return next();
}

and in the logs I can see that the req contains the user here as expected, but when I try to access it in the controller as

async foo(
        @Body() body: OnboardRequestDto,
        @Req() req: FastifyRequest,
    ): Promise<FooResponseDto> {
console.log(req['user'])

its undefined. Any ideas what am I doing wrong?

ocean echo
#

Does this exist at req.raw.user in the controller?

weak temple
#

Wow

#

works as
@Req() req: FastifyRequest
console.log(req.raw['user'])

Interesting because if I capture the request like this:

@Req() req: FastifyRequest['raw']

then console.log(req['user']) is undefined

ocean echo
#

The type doesn't change the request object passed by Nest

#

@Req() with the fastify adapter will always be FastifyRequest