I have integrated AWS Cognito as the authentication service for my NestJS application. However, when accessing the endpoint without a JWT (unauthenticated), the server crashes and displays the error
TypeError: applicationRef.isHeadersSent is not a function
. Strangely enough, everything functions correctly when a valid JWT is provided, resulting in the correct data being returned by the API Endpoint protected by the auth guard. Here is the setup I used for the authentication configuration and auth guard. Could someone please review this? Thank you in advance!
src/authz/authz.module.ts
import { Injectable } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { ExtractJwt, Strategy } from 'passport-jwt';
import { passportJwtSecret } from 'jwks-rsa';
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
constructor() {
super({
secretOrKeyProvider: passportJwtSecret({
cache: true,
rateLimit: true,
jwksRequestsPerMinute: 5,
jwksUri: `https://cognito-idp.us-east-1.amazonaws.com/xxxxx/.well-known/jwks.json`,
}),
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
issuer: 'https://cognito-idp.us-east-1.amazonaws.com/xxxxx',
algorithms: ['RS256'],
});
}
validate(payload: unknown): unknown {
return payload;
}
}
src/authz/jwt.strategy.ts
import { Module } from '@nestjs/common';
import { PassportModule } from '@nestjs/passport';
import { JwtStrategy } from './jwt.strategy';
@Module({
imports: [PassportModule.register({ defaultStrategy: 'jwt' })],
providers: [JwtStrategy],
exports: [PassportModule],
})
export class AuthzModule {}
src/app.module.ts
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { GraphQLModule } from '@nestjs/graphql';
import { MomentModule } from './moment/moment.module';
import { graphqlConfigOptions } from './config/graphql';
import { TypeOrmModule } from '@nestjs/typeorm';
import { typeormConfigOptions } from './config/data-source';
import { CharacterModule } from './character/character.module';
import { AuthzModule } from './authz/authz.module';
@Module({
imports: [
GraphQLModule.forRoot(graphqlConfigOptions),
TypeOrmModule.forRoot(typeormConfigOptions),
MomentModule,
CharacterModule,
AuthzModule,
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
src/app.controller.ts
import { Controller, Get, UseGuards } from '@nestjs/common';
import { AppService } from './app.service';
import { ApiBearerAuth, ApiResponse, ApiTags } from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';
@ApiTags('System')
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
@Get('health')
@ApiResponse({ status: 200, type: String })
getHello(): string {
return this.appService.getHello();
}
@UseGuards(AuthGuard('jwt'))
@Get('secure-message')
@ApiResponse({ status: 200, type: String })
@ApiResponse({ status: 401 })
@ApiBearerAuth()
getSecureMessage(): string {
return this.appService.getSecureMessage();
}
}
package.json
{
"name": "moment-share-service",
"version": "0.0.1",
"description": "",
"author": "",
[...]
Stack Trace
if (!applicationRef.isHeadersSent(response)) {
^
TypeError: applicationRef.isHeadersSent is not a function
at ExceptionsHandler.catch (D:\self-study\GitHub Repo\Social Media App Suite\social-media-app-suite\moment-share-service\node_modules\@nestjs\core\exceptions\base-exception-filter.js:27:29)
at ExceptionsHandler.next (D:\self-study\GitHub Repo\Social Media App Suite\social-media-app-suite\moment-share-service\node_modules\@nestjs\core\exceptions\exceptions-handler.js:16:20)
[...]
result of npx nest info
[System Information]
OS Version : Windows 10
NodeJS Version : v18.14.1
YARN Version : 1.22.17
[Nest CLI]
Nest CLI Version : 8.2.8
[Nest Platform Information]
platform-express version : 8.4.7
mapped-types version : 1.2.2
schematics version : 8.0.11
passport version : 9.0.3
graphql version : 11.0.5
swagger version : 6.3.0
typeorm version : 9.0.1
testing version : 8.4.7
[...]