R
R
Raey2021-12-16 13:17:20
JavaScript
Raey, 2021-12-16 13:17:20

Nest.js Do I need to export PassportModule and JwtStrategy?

I saw a lot of examples, someone did it, but someone did not. I can't come to a consensus.
Without exporting PassportModule and JwtStrategy, everything works like that.
Example:

AuthModule
import { Module } from '@nestjs/common';
import { AuthService } from './auth.service';
import { AuthController } from './auth.controller';
import { TypeOrmModule } from '@nestjs/typeorm';
import { UsersRepository } from './users.repository';
import { PassportModule } from '@nestjs/passport';
import { JwtModule } from '@nestjs/jwt';
import { JwtStrategy } from './jwt.strategy';
import { ConfigModule, ConfigService } from '@nestjs/config';

@Module({
  imports: [
    ConfigModule,
    PassportModule.register(),
    JwtModule.registerAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: async (configService: ConfigService) => ({
        secret: configService.get('JWT_SECRET'),
        signOptions: {
          expiresIn: 3600,
        },
      }),
    }),
    TypeOrmModule.forFeature([UsersRepository]),
  ],
  providers: [AuthService, JwtStrategy],
  controllers: [AuthController],
  exports: [JwtStrategy, PassportModule],
})
export class AuthModule {}

TaskModule
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { TasksController } from './tasks.controller';
import { TasksRepository } from './tasks.repository';
import { TasksService } from './tasks.service';

@Module({
  imports: [TypeOrmModule.forFeature([TasksRepository])],
  controllers: [TasksController],
  providers: [TasksService],
})
export class TasksModule {}


In Task Controller I use @UseGuards(JwtAuthGuard)
import { Injectable } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';

@Injectable()
export class JwtAuthGuard extends AuthGuard('jwt') {}


AppModule
import { Module } from '@nestjs/common';
import { TasksModule } from './tasks/tasks.module';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AuthModule } from './auth/auth.module';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { configValidationSchema } from './config.schema';

@Module({
  imports: [
    ConfigModule.forRoot({
      validationSchema: configValidationSchema,
    }),
    TasksModule,
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: async (configService: ConfigService) => {

        return {
          type: 'postgres',
          autoLoadEntities: true,
          synchronize: true,
          host: configService.get('DB_HOST'),
          port: configService.get('DB_PORT'),
          username: configService.get('DB_USERNAME'),
          password: configService.get('DB_PASSWORD'),
          database: configService.get('DB_DATABASE'),
        };
      },
    }),
    AuthModule,
  ],
})
export class AppModule {}


So, if I remove exports: [JwtStrategy, PassportModule] in AuthModule, everything will work, no varns in the console. Is it allowed to do so?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lssssssssssl, 2021-12-16
@Raey

Permissible.
But in general it depends on the logic of your application.
If you want to use JwtService in other modules then you need to register JwtModule in one of your modules and expose it.
The point of this is to call the registerAsync() method once in the application and reuse the same module with this executed method in other modules.
But personally, I do not advise you to export specifically from AuthModule, because AuthModule, on the contrary, should import other modules into itself, and not be an import for someone.
Same with PassportModule. But personally, I always store it in the AuthModule and don't export it anywhere, and don't use it anywhere else.
There is no point in exporting JwtStrategy since the only place you use it is in JwtAuthGuard.
Summing up: It is possible to export, but most often it is not necessary (I have not met such a need in 2 years)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question