nestjs/passport

passport module provided options are not working

khawarizmus opened this issue · 6 comments

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

Registering Passport module with options doesn't seem to work

PassportModule.registerAsync({
      useFactory: () => ({
        property: 'account',
      }),
    }),

The property attached to the request is still named user instead or account

Expected behavior

property is account and not user

Minimal reproduction of the problem with instructions

EDIT:
https://codesandbox.io/s/passport-not-working-u0his?file=/src/app.controller.ts

git repo: https://github.com/gimyboya/nest-passport-not-working

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

Configure Passport to attache the result returned by the verify function from the strategy class as an object named account instead of user

Environment


Nest version: 7.4.2
 
For Tooling issues:
- Node version: v12.16.2
- Platform:  Windows

Please provide a minimum reproduction repository.

@kamilmysliwiec here is a working example for this issue: https://codesandbox.io/s/passport-not-working-u0his?file=/src/app.controller.ts

sorry for not being able to provide this earlier.

For anyone facing this in the future, here is the solution provided by @jmcdo29 :

1- start by including the optional options in the guard constructor and pass it down to the super constructor as it turns out that the super constructor was not having the options passed down to it.

constructor(
    private readonly jwtService: JwtService,
    private readonly accountService: AccountService,
    private authService: AuthService,
    @Optional() options: AuthModuleOptions,
  ) {
    super(options);
  }

2- Export the PassportModule and make sure to include the AuthModule anywhere you use the guard

pxDot commented

Seems not to work for global guards. Property setting is not getting passed to the ctor if the guard is defined globally and it ist not picking up the custom property name.

// Edit: It is working, when defining the global guards in AuthModule instead of AppModule

For anyone facing this in the future, here is the solution provided by @jmcdo29 :

1- start by including the optional options in the guard constructor and pass it down to the super constructor as it turns out that the super constructor was not having the options passed down to it.

constructor(
    private readonly jwtService: JwtService,
    private readonly accountService: AccountService,
    private authService: AuthService,
    @Optional() options: AuthModuleOptions,
  ) {
    super(options);
  }

2- Export the PassportModule and make sure to include the AuthModule anywhere you use the guard

I have encountered the same problem as you, and it doesn't work according to your method