Home > Software engineering >  NestJs Authentication Circular Dependency
NestJs Authentication Circular Dependency

Time:01-13

I am fairly new to nestJs. I am trying to implement an authentication process according to nestjs documentation, but I don't want to route the login in the app.controller I want to put it in the users.controller, so the route will be /users/login. However, since the AuthModule already uses the UsersService in my code and the login logic with JWT is in the AuthService. Importing it back to the UsersService create a circular dependency. I need help.

CodePudding user response:

To avoid circular dependancy, in your UsersModule, you need to import your AuthModule with ForwardRef like that :

import ...

@Module( {
  imports: [
    forwardRef( () => AuthModule ),
  ],
  controllers: [],
  providers: [],
  exports: []
} )
export class UsersModule {
}

If you haven't already done so, you'll need to export the AuthService in AuthModule :

import ...

@Module( {
  imports: [],
  controllers: [],
  providers: [],
  exports: [
    AuthService
  ]
} )
export class AuthModule {
}

  •  Tags:  
  • Related