[ ] Regression
[ x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
I get :
Error: Nest can't resolve dependencies of the AuthService (?). Please make sure that the argument at index [0] is available in the AuthModule context.
The app should run and the AuthService dependancies can be injected in AuthModule
app.module.ts
@Module({
imports: [
TypeOrmModule.forRoot(),
UserModule,
AuthModule
],
controllers: [AppController, UserController],
providers: [AppService, UserService],
})
export class AppModule {
constructor(private readonly connection: Connection) {
}
}
auth.module.ts :
@Module({
imports: [UserModule],
providers: [AuthService, HttpStrategy]
})
export class AuthModule {
}
http.strategy.ts :
@Injectable()
export class HttpStrategy extends PassportStrategy(Strategy) {
constructor(private readonly authService: AuthService) {
super();
}
async validate(token: string) {
const user = await this.authService.validateUser(token);
if (!user) {
throw new UnauthorizedException();
}
return user;
}
}
auth.service.ts :
@Injectable()
export class AuthService {
constructor(private readonly userService: UserService) {}
async validateUser(token: string): Promise<any> {
// Validate if token passed along with HTTP request
// is associated with any registered account in the database
return await this.userService.findOneByToken(token);
}
}
I want to add an Authentication system in my app.
Nest version: 5.4.0
For Tooling issues:
- Node version: 10.15.0
- Platform: Windows
Please, provide a minimal repository which reproduces your issue.
https://github.com/frenchqwerty/nest-js-auth-issues there are the repo
Hi, i think you are missing the export of the provider (you are trying to use the service in a different module)
I have ran into the same issue before
My issues is solved !
The solution is :
auth.module.ts :
@Module({
imports: [
UserModule,
PassportModule.register({defaultStrategy: 'bearer'})
],
providers: [AuthService, HttpStrategy, UserService]
})
export class AuthModule {
}
Just add the UserService in provider.
Glad to hear ;)
Instead of adding UserService
twice (in UserModule
and AuthModule
), simply export it from UserModule
(add UserService
to exports array)
Instead of adding
UserService
twice (inUserModule
andAuthModule
), simply export it fromUserModule
(addUserService
to exports array)
exuse me why does this answer ?
wondering if anyone has insight into why this isnt working
Nest can't resolve dependencies of the UsersService (?). Please make sure that the argument at index [0] is available in the AppModule context.
UserSerivice is being imported via the module into the AuthModule. I can use TopicModule fine
https://github.com/ricardosaracino/collab-apiapp/blob/master/src/users/users.service.ts
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Most helpful comment
Hi, i think you are missing the export of the provider (you are trying to use the service in a different module)
I have ran into the same issue before