Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
lizhihui888 opened this issue May 5, 2024 · 2 comments

Comments

@lizhihui888
Copy link

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
Copy link

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

@micalevisk
Copy link
Member

This has nothing to do with the awesome-nestjs repository

Try the following instead:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants