title: Nest.js Auth Guard JWT Authentication constantly returns 401 unauthorized
body: <p>Using Postman to test my endpoints, I am able to successfully &quot;login&quot; and receive a JWT token. Now, I am trying to hit an endpoint that supposedly has an <code>AuthGuard</code> to ensure that now that I am logged in, I can now access it.</p>
  <p>However, it constantly returns <code>401 Unauthorized</code> even when presented the JWT token in Postman.</p>
  <p>Here is my code:</p>
  <p><strong>user.controller.ts</strong></p>
  <pre><code>@Controller('users')
  export class UsersController {
      constructor(private readonly usersService: UsersService) {}
  
      @UseGuards(AuthGuard())
      @Get()
      getUsers() {
          return this.usersService.getUsersAsync();
      }
  }
  </code></pre>
  <p><strong>jwt.strategy.ts</strong></p>
  <pre><code>@Injectable()
  export class JwtStrategy extends PassportStrategy(Strategy) {
      constructor(
          private readonly authenticationService: AuthenticationService,
      ) {
          super({
              jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
              ignoreExpiration: false,
              secretOrKey: 'SuperSecretJWTKey',
          });
      }
  
      async validate(payload: any, done: Function) {
          console.log(&quot;I AM HERE&quot;); // this never gets called.
          const user = await this.authenticationService.validateUserToken(payload);
  
          if (!user) {
              return done(new UnauthorizedException(), false);
          }
  
          done(null, user);
      }
  }
  </code></pre>
  <p>I have tried <code>ExtractJWT.fromAuthHeaderWithScheme('JWT')</code> as well but that does not work.</p>
  <p><strong>authentication.module.ts</strong></p>
  <pre><code>@Module({
      imports: [
          ConfigModule,
          UsersModule,
          PassportModule.register({ defaultStrategy: 'jwt' }),
          JwtModule.register({
              secret: 'SuperSecretJWTKey',
              signOptions: { expiresIn: 3600 },
          }),
      ],
      controllers: [AuthenticationController],
      providers: [AuthenticationService, LocalStrategy, JwtStrategy],
      exports: [AuthenticationService, LocalStrategy, JwtStrategy],
  })
  export class AuthenticationModule {}
  </code></pre>
  <p><strong>authentication.controller.ts</strong></p>
  <pre><code>@Controller('auth')
  export class AuthenticationController {
      constructor(
          private readonly authenticationService: AuthenticationService,
          private readonly usersService: UsersService,
      ) {}
  
      @UseGuards(AuthGuard('local'))
      @Post('login')
      public async loginAsync(@Response() res, @Body() login: LoginModel) {
          const user = await this.usersService.getUserByUsernameAsync(login.username);
  
          if (!user) {
              res.status(HttpStatus.NOT_FOUND).json({
                  message: 'User Not Found',
              });
          } else {
              const token = this.authenticationService.createToken(user);
              return res.status(HttpStatus.OK).json(token);
          }
      }
  }
  </code></pre>
  <p>In Postman, I am able to use my login endpoint to successfully login with the proper credentials and receive a JWT token. Then, I add an <code>Authentication</code> header to a GET request, copy and paste in the JWT token, and I have tried both &quot;Bearer&quot; and &quot;JWT&quot; schemes and both return <code>401 Unauthorized</code> as you can see in the images below.</p>
  <p><a href="https://i.stack.imgur.com/3dbeQ.png" rel="noreferrer"><img src="https://i.stack.imgur.com/3dbeQ.png" alt="enter image description here" /></a></p>
  <p><a href="https://i.stack.imgur.com/ndnMd.png" rel="noreferrer"><img src="https://i.stack.imgur.com/ndnMd.png" alt="enter image description here" /></a></p>
  <p>I used the JWT.IO debugger, to check if there's anything wrong with my token and it appears correct:
  <a href="https://i.stack.imgur.com/k6MIf.png" rel="noreferrer"><img src="https://i.stack.imgur.com/k6MIf.png" alt="enter image description here" /></a></p>
  <p>I am at a lost as to what could be the issue here. Any help would be greatly appreciated.</p>