#JWT service reqires a secret...

13 messages · Page 1 of 1 (latest)

cedar jewel
#

I'm trying to configure JWT strat. using passport + nestJS I was following the official docs on authentication here: https://docs.nestjs.com/security/authentication#jwt-functionality
but its forcing me to use a secret here even if i provide it in the module, I wanted someone to clarify if the docs are outdated or am i doing something wrong? here's my ts code for the auth service signing the JWT

import { Injectable } from '@nestjs/common';
import { UsersService } from '../users/users.service';
import { JwtService } from '@nestjs/jwt';
import { jwtConstants } from './constants';

@Injectable()
export class AuthService {
  constructor(
    private readonly usersService: UsersService,
    private readonly jwtService: JwtService,
  ) {}

  async validateUser(username: string, pass: string): Promise<any> {
    const user = await this.usersService.findOne(username);
    if (user && user.password === pass) {
      const { password, ...result } = user;
      return result;
    }
    return null;
  }

  async login(user: any) {
    const payload = { email: user.email, sub: user.userId };
    return {
      access_token: this.jwtService.sign(payload, {
        secret: jwtConstants.secret,
      }),
    };
  }
}

and here is my auth.module.ts

import { Module } from '@nestjs/common';
import { AuthService } from './auth.service';
import { LocalStrategy } from './strategies/local.strategy';
import { UsersModule } from '../users/users.module';
import { PassportModule } from '@nestjs/passport';
import { JwtModule } from '@nestjs/jwt';
import { jwtConstants } from './constants';
import { JwtStrategy } from './strategies/jwt.strategy';

@Module({
  imports: [
    UsersModule,
    PassportModule,
    JwtModule.register({
      secret: jwtConstants.secret,
      signOptions: { expiresIn: '60s' },
    }),
  ],
  providers: [AuthService, LocalStrategy, JwtStrategy],
  exports: [AuthService],
})
export class AuthModule {}
mild tulip
#

Almost every time I see this, someone is adding JwtService to a providers array where it shou;dn't be, or the JwtService is being re-instantiated somewhere else and that's where it is causing the problem

cedar jewel
#

But i didnt add it to the providers here

#

let me check the other modules i guess

mild tulip
#

What;s the full error you're getting? That stack trace will be helpful here'

cedar jewel
#

yea so i did add it yesterday in the providers of the app.module.ts

#

because it was giving an error let me grab it

#
[Nest] 15052  - 10/15/2022, 6:59:35 PM   ERROR [ExceptionHandler] Nest can't resolve dependencies of the AuthService (UsersService, ?). Please make sure that the argument JwtService at index [1] is available in the AppModule context.

Potential solutions:
- If JwtService is a provider, is it part of the current AppModule?
- If JwtService is exported from a separate @Module, is that module imported within AppModule?
  @Module({
    imports: [ /* the Module containing JwtService */ ]
  })

Error: Nest can't resolve dependencies of the AuthService (UsersService, ?). Please make sure that the argument JwtService at index [1] is available in the AppModule context.

Potential solutions:
- If JwtService is a provider, is it part of the current AppModule?
- If JwtService is exported from a separate @Module, is that module imported within AppModule?
  @Module({
    imports: [ /* the Module containing JwtService */ ]
  })
#
at Injector.lookupComponentInParentModules (D:\projects\Coursuite\coursuite-backend\node_modules\@nestjs\core\injector\injector.js:241:19)
    at Injector.resolveComponentInstance (D:\projects\Coursuite\coursuite-backend\node_modules\@nestjs\core\injector\injector.js:194:33)
    at resolveParam (D:\projects\Coursuite\coursuite-backend\node_modules\@nestjs\core\injector\injector.js:116:38)
    at async Promise.all (index 1)
    at Injector.resolveConstructorParams (D:\projects\Coursuite\coursuite-backend\node_modules\@nestjs\core\injector\injector.js:131:27)
    at Injector.loadInstance (D:\projects\Coursuite\coursuite-backend\node_modules\@nestjs\core\injector\injector.js:57:13)
    at Injector.loadProvider (D:\projects\Coursuite\coursuite-backend\node_modules\@nestjs\core\injector\injector.js:84:9)
    at async Promise.all (index 4)
    at InstanceLoader.createInstancesOfProviders (D:\projects\Coursuite\coursuite-backend\node_modules\@nestjs\core\injector\instance-loader.js:47:9)
    at D:\projects\Coursuite\coursuite-backend\node_modules\@nestjs\core\injector\instance-loader.js:32:13```
#

so thats the whole error

#

when i remove it from app module providers

mild tulip
#

Why is AuthService in the providers of AppModule?

cedar jewel
#

oh