Nest: @nestjs/jwt - Cannot read property 'challenge' of undefined

Created on 1 Sep 2018  Â·  11Comments  Â·  Source: nestjs/nest

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'm getting the following error:


TypeError: Cannot read property 'challenge' of undefined
    at allFailed (/Users/fpachecoibz/Documents/git/fpachecoibz-service/node_modules/passport/lib/middleware/authenticate.js:107:52)
    at attempt (/Users/fpachecoibz/Documents/git/fpachecoibz-service/node_modules/passport/lib/middleware/authenticate.js:180:28)
    at authenticate (/Users/fpachecoibz/Documents/git/fpachecoibz-service/node_modules/passport/lib/middleware/authenticate.js:362:7)
    at Promise (/Users/fpachecoibz/Documents/git/fpachecoibz-service/node_modules/@nestjs/passport/dist/auth.guard.js:77:3)
    at new Promise ()
    at /Users/fpachecoibz/Documents/git/fpachecoibz-service/node_modules/@nestjs/passport/dist/auth.guard.js:69:83
    at MixinAuthGuard. (/Users/fpachecoibz/Documents/git/fpachecoibz-service/node_modules/@nestjs/passport/dist/auth.guard.js:44:36)
    at Generator.next ()
    at /Users/fpachecoibz/Documents/git/fpachecoibz-service/node_modules/@nestjs/passport/dist/auth.guard.js:19:71
    at new Promise ()

Expected behavior


Guard should block the request when I try to call the /user/all route

Minimal reproduction of the problem with instructions


The project repository is this

Environment


Nest version: 5.3.0


For Tooling issues:
- Node version: 10.9.0
- Platform:  MacOS

Others:
- @nestjs/jwt: 0.1.3
- @nestjs/passport: 5.0.0
- passport: 0.4.0
- passport-jwt: 4.0.0

Most helpful comment

PassportModule has to be injected everywhere where you want to take advantage of defaultStrategy option. You should at least re-export module by putting it inside the exports array. I just published 5.0.1 that includes more descriptive errors to anticipate such issues in the future.

All 11 comments

The error appears when I protect a route using:

@UseGuards(AuthGuard())

But if we add 'jwt' inside AuthGuard(), works:

@UseGuards(AuthGuard('jwt'))

I only have one strategy, jwt.

PassportModule has to be injected everywhere where you want to take advantage of defaultStrategy option. You should at least re-export module by putting it inside the exports array. I just published 5.0.1 that includes more descriptive errors to anticipate such issues in the future.

Hello I am having the same issue. But I cannot figure out the solutions provided here. I have to resort to

@UseGuards(AuthGuard('jwt'))

instead of

@UseGuards(AuthGuard())

@tochemey The token in the Authorization header has to start with jwt. Full example would be
Authorization: jwt <some token here> note there is a space between jwt and the token. Not sure if this is your issue but I would check that first.

Hello,

I did that but not successful.

@Tochemey,

```import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { PassportModule } from '@nestjs/passport';

import { UserService } from './user.service';
import { UserController } from './user.controller';
import { User } from './user.entity';

export const passportModule = PassportModule.register({ defaultStrategy: 'jwt' });

@Module({
imports: [TypeOrmModule.forFeature([User]), passportModule],
controllers: [UserController],
providers: [UserService],
exports: [UserService]
})

export class UserModule {}

You do not need to add jwt before the authorization bearer 

Postman example
```GET /user HTTP/1.1
Host: localhost:3000
Content-Type: application/x-www-form-urlencoded
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjEiLCJlbWFpbCI6Im5pZWxzQHNlaXRvbi5pbyIsImlhdCI6MTUxNjIzOTAyMn0.WoE9v_A46Bn5EHC_diNOTbEohKclvK4dwnWbrhkfsfo
cache-control: no-cache
Postman-Token: 5b5f48e0-a9ae-4371-816a-42e3f05907a0

This is still not working when I follow this example.

you have to import PassportModule as

  imports: [
    TypeOrmModule.forFeature([UserRepository]),
    HttpModule,
    ConfigModule,
    PassportModule.register({ defaultStrategy: 'jwt' }),
  ],

into every module where you want to use default strategy.

Great, thank you.

On Jun 10, 2019, at 5:20 AM, goranjviv notifications@github.com wrote:

you have to import PassportModule as

imports: [
TypeOrmModule.forFeature([UserRepository]),
HttpModule,
ConfigModule,
PassportModule.register({ defaultStrategy: 'jwt' }),
],```
into every module where you want to use default strategy.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub https://github.com/nestjs/nest/issues/1031?email_source=notifications&email_token=AFL5DPWBSI5ZB7JK5OC3EPLPZYMGXA5CNFSM4FSZNF42YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODXJL2PQ#issuecomment-500350270, or mute the thread https://github.com/notifications/unsubscribe-auth/AFL5DPRD3LYW6LVUDYX5WFTPZYMGXANCNFSM4FSZNF4Q.

Not working for me. Still asks for an import, but I have a single AuthModule that uses it and imports is using the defaultStrategy opt.

PassportModule is not a global module. Please, read the docs https://docs.nestjs.com/modules

Was this page helpful?
0 / 5 - 0 ratings