title: NestJs JWT Authentication returns 401
body: <p>I have implemented a jwt authentication in nestJs. However whenever I attempt to authenticate using the following authorization headers:</p>
  
  <pre><code>Bearer &lt;token&gt; or JWT &lt;token&gt;
  </code></pre>
  
  <p>I got 401. These are my authentication files</p>
  
  <pre><code>@Injectable()
  export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') {
    constructor(private readonly authService: AuthService) {
      super({
        jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
        secretOrKey: `${process.env.SECRET}`,
      });
    }
  
    async validate(payload: Credentials) {
      const user: Account = await this.authService.validateAccount(payload);
      if (!user) {
        throw new UnauthorizedException();
      }
      return user;
    }
  }
  
  
  @Injectable()
  export class JwtAuthGuard extends AuthGuard('jwt') {
    canActivate(context: ExecutionContext) {
      return super.canActivate(context);
    }
  
    handleRequest(err, user, info) {
      if (err || !user) {
        throw err || new UnauthorizedException();
      }
      return user;
    }
  }
  </code></pre>
  
  <p>and this my auth module</p>
  
  <pre><code>@Module({
    imports: [
      PassportModule.register({ defaultStrategy: 'jwt' }),
      JwtModule.register({
        secretOrPrivateKey: `${process.env.SECRET}`,
      }),
      AccountModule,
    ],
    providers: [AuthService, JwtStrategy],
    controllers: [AuthController],
    exports: [PassportModule, AuthService],
  })
  export class AuthModule {
  
  }
  </code></pre>