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

Remove optional dependency metadata inherited from base class #2581

Open
gipde opened this issue Jun 17, 2019 · 6 comments
Open

Remove optional dependency metadata inherited from base class #2581

gipde opened this issue Jun 17, 2019 · 6 comments
Labels
effort1: hours priority: low (4) Low-priority issue that needs to be resolved scope: core

Comments

@gipde
Copy link

gipde commented Jun 17, 2019

I'm submitting a...


[ ] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

i extend AuthGuard with a custom Implementation that depends on a provider of an other module (e.g. config).
if i try to use that custom AuthGuard in an other module, i have to specify a dependency to the config module, although this is already done in the module of the custom guard.

After a long debug session i found out, that AuthGuard use an Optional Parameter. https://github.com/nestjs/passport/blob/8acdc04e6210aefab7a6e2ac4009e788bc8de5b8/lib/auth.guard.ts#L29
Which means the injector does nothing, because it can not resolve the dependency (config) and the parameter of the base-class (AuthGuard) has an Optional Parameter

return undefined;

Expected behavior

Print out a clear Error message

Minimal reproduction of the problem with instructions

import {
  ExecutionContext,
  Injectable,
  Logger,
  UnauthorizedException,
} from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { Configuration } from '../configuration/configuration';

@Injectable()
export class JwtAuthGuard extends AuthGuard('jwt') {
  constructor(private readonly config: Configuration) {
    super();
  }

  canActivate(context: ExecutionContext) {
    // Add your custom authentication logic here
    // for example, call super.logIn(request) to establish a session.
    // return true;
    // super.logIn(context.switchToHttp().getRequest());

    return super.canActivate(context);
  }

  handleRequest(err, user, _info) {
    if (err || !user) {
      if (this.config.withoutLogin()) {
        Logger.log('WITHOUT_LOGIN ==> NO LOGIN REQUIRED !!!');
        user = 'autologin';
        return user;
      }

      throw err || new UnauthorizedException();
    }
    return user;
  }
}

What is the motivation / use case for changing the behavior?

In General i find that nestjs should be more verbose on handling dependencies. i spend a lot time debugging in the past

Environment


Nest version: 6.2.4

 
For Tooling issues:
- Node version: v11.10.0 
- Platform: Mac

Others:

@gipde gipde changed the title Extending AuthGuard Extending AuthGuard leads to problems Jun 17, 2019
@adamhathcock
Copy link

Thanks for this issue, I just got bit by this myself.

@kamilmysliwiec kamilmysliwiec transferred this issue from nestjs/passport Jul 14, 2019
@kamilmysliwiec kamilmysliwiec changed the title Extending AuthGuard leads to problems Remove optional dependency metadata inherited from base class Jul 14, 2019
@kamilmysliwiec kamilmysliwiec added effort1: hours priority: low (4) Low-priority issue that needs to be resolved and removed type: potential issue 💔 labels Feb 2, 2021
@Enkosz
Copy link

Enkosz commented Feb 4, 2022

Is someone working on this bug? I would like to take a look

@kamilmysliwiec
Copy link
Member

PRs are more than welcome!

@eabasir
Copy link

eabasir commented Mar 22, 2022

Still no update on this?

@micalevisk
Copy link
Member

micalevisk commented Mar 22, 2022

@jmcdo29 wasn't this fixed by your PR nestjs/passport#824

@jmcdo29
Copy link
Member

jmcdo29 commented Mar 22, 2022

Specifically for the AuthGuard yes, but if that was just an example and we wanted to take this to a higher level (all child classes have separate DI metadata) then this would still be an issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
effort1: hours priority: low (4) Low-priority issue that needs to be resolved scope: core
Projects
None yet
Development

No branches or pull requests

7 participants