nestjs / awesome-nestjs

A curated list of awesome things related to NestJS 😎
10.63k stars 940 forks source link

how can I inject jwtService or other Injectable service into the param decorator ! #288

Open lizhihui888 opened 4 months ago

lizhihui888 commented 4 months ago

I want to get user info from jwt token in controller , like this:

  @Get('getUser')
  getUser(@User() user) {
    return user;
  }

so I create a custom param decorator like this:

export const User = createParamDecorator((data: string, ctx: ExecutionContext) => {
  const request = ctx.switchToHttp().getRequest<Request>();

  const arr = request.headers.authorization?.split(' ') ?? [];
  const token = arr.at(-1);
  if (!token) {
    return undefined;
  }
  // how to inject jwtService ?
  const payload = jwtService.verify(token);
  return payload;
});

how to inject jwtService into the decorator ? thanks !

stijnveeke commented 2 months ago

Have you found a solution now having the same problem with my custom decorator.

micalevisk commented 2 months ago

This has nothing to do with the awesome-nestjs repository

Try the following instead: