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

ParamDecorator not work correctly in LocalGuard canActivate function #10593

Closed
4 of 15 tasks
kusime opened this issue Nov 24, 2022 · 2 comments
Closed
4 of 15 tasks

ParamDecorator not work correctly in LocalGuard canActivate function #10593

kusime opened this issue Nov 24, 2022 · 2 comments
Labels
needs triage This issue has not been looked into

Comments

@kusime
Copy link

kusime commented Nov 24, 2022

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

I had use createParamDecorator to create a custom decorator call BodyChecker , and this decorator is aim to check the request body have some filed in it , if not throw the BadRequestException , and this decorator recived two parameter

export const BodyChecker = createParamDecorator(
(
data: { check: string[]; isJwt: boolean; retCTX?: true | undefined },
ctx: ExecutionContext,
) => {
console.log('BodyChecker called');

the data is the option that passed in to the BodyChecker parameter , and ctx is ExecutionContext

but this Decorator not work fine with the passport-local guard

in local.guard.ts file , I have

export class LocalGuard extends AuthGuard('local') {
// use the handleRequest method to intercept the request
canActivate(
@BodyChecker({
check: ['email', 'password'],
isJwt: false,
retCTX: true,
})
ctx,
) {
// shift to the low-level implementation of passport-local
console.log('local canActivate');
return super.canActivate(ctx);
}

// this canActivate is ref by https://docs.nestjs.com/guards#execution-context
and canActivate is hoped to received a ExecutionContext , so this BodyChecker should work fine since the ExecutionContext is
provied to the BodyChecker ,

but the final run result is the console not log the => BodyChecker called
so I don't know why this BodyChecker is not run with this canActivate function , but this BodyChecker work fine if it's not in guard ,such as in controller

async login(
@BodyChecker({ check: ['email', 'password'], isJwt: false })
body: { email: string },
): Promise {
return this.auth.assignToken(body[jwtPayloadKey]);
}

Minimum reproduction code

https://github.com/kusime

Steps to reproduce

No response

Expected behavior

the BodyChecker callback should be run , and use the context to make some check

Package

  • I don't know. Or some 3rd-party package
  • @nestjs/common
  • @nestjs/core
  • @nestjs/microservices
  • @nestjs/platform-express
  • @nestjs/platform-fastify
  • @nestjs/platform-socket.io
  • @nestjs/platform-ws
  • @nestjs/testing
  • @nestjs/websockets
  • Other (see below)

Other package

passport-local @nest/passport

NestJS version

9.1.5

Packages versions

[System Information]
OS Version     : Linux 5.15
NodeJS Version : v16.18.1
YARN Version    : 1.22.19 

[Nest CLI]
Nest CLI Version : 9.1.5 

[Nest Platform Information]
platform-express version : 9.2.0
mapped-types version     : 1.2.0
schematics version       : 9.0.3
passport version         : 9.0.0
testing version          : 9.2.0
common version           : 9.2.0
config version           : 2.2.0
core version             : 9.2.0
jwt version              : 9.0.0
cli version              : 9.1.5

Node.js version

v16.18.1

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

@kusime kusime added the needs triage This issue has not been looked into label Nov 24, 2022
@kusime
Copy link
Author

kusime commented Nov 24, 2022

How ever this problem can be easily avoid since the canActive had provied ExecutionContext , but i dont konw why this BodyChecker not work here , is there are some design purpose here ?

@kamilmysliwiec
Copy link
Member

Please, use our Discord channel (support) for such questions. We are using GitHub to track bugs, feature requests, and potential improvements.

@nestjs nestjs locked and limited conversation to collaborators Nov 24, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
needs triage This issue has not been looked into
Projects
None yet
Development

No branches or pull requests

2 participants