Nest: Error: Unknown authentication strategy "jwt"

Created on 28 Mar 2019  路  2Comments  路  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 just updated to v6.0.4 and I'm using authguard with graphql & passport-jwt. However, I'm getting Error: Unknown authentication strategy "jwt"

Expected behavior

Minimal reproduction of the problem with instructions


// gql-auth.guard.ts
import { Injectable } from '@nestjs/common';
import { GqlExecutionContext } from '@nestjs/graphql';
import { AuthGuard } from '@nestjs/passport';

@Injectable()
export class GqlAuthGuard extends AuthGuard('jwt') {
  getRequest(context: GqlExecutionContext) {
    const ctx = GqlExecutionContext.create(context);
    return ctx.getContext().req;
  }
}

//jwt.strategy.ts
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { ExtractJwt, Strategy, VerifiedCallback } from 'passport-jwt';
import { UserService} from 'src/user/user.service';

@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
  constructor(private userService: UserService) {
    super({
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      secretOrKey: process.env.JWT_SECRET,
    });
  }

  async validate(payload: any, done: VerifiedCallback) {
    const user = await this.userService.findOneByPayload(payload);
    if (!user) {
      return done(new UnauthorizedException(), false);
    }

    return done(null, { id: user.id }, payload.iat);
  }
}

// auth.module.ts
import { Module } from '@nestjs/common';
import { JwtModule } from '@nestjs/jwt';
import { PassportModule } from '@nestjs/passport';
import { UserModule } from 'src/user/user.module';
import { AuthResolver } from './auth.resolver';

@Module({
  imports: [
    PassportModule.register({ defaultStrategy: 'jwt' }),
    JwtModule.register({ secretOrPrivateKey: process.env.JWT_SECRET }),
    UserModule,
  ],
  providers: [AuthResolver],
})
export class AuthModule {}

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

Environment


Nest version: 6.0.4


For Tooling issues:
- Node version: 10.15
- Platform:  Windows

Others:

Most helpful comment

I forgot to import Strategy into Provider :D
please close it thanks

All 2 comments

I forgot to import Strategy into Provider :D
please close it thanks

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cojack picture cojack  路  3Comments

thohoh picture thohoh  路  3Comments

menme95 picture menme95  路  3Comments

tronginc picture tronginc  路  3Comments

FranciZ picture FranciZ  路  3Comments