0

I'm following the official NestJS documentation. Currently, I'm trying to implement the authentication step with Passport strategy. I did every step, as the documentation says, but I got stuck where I need to generate the JWT with the jwtService.sign() method. The error, that I'm getting is:

ERROR [ExceptionsHandler] secretOrPrivateKey must have a value`.

Here are the code snippets:

AuthModule:

@Module({
  imports: [
    UserModule, 
    PassportModule, 
    User, 
    TypeOrmModule.forFeature([User]), 
    JwtModule.register({
      secret: 'somerandomsecret',
      signOptions: { expiresIn: '60s' }
    })
],
  providers: [AuthService, LocalStrategy, UserService],
  exports: [AuthService]
})

export class AuthModule {}

AuthService

@Injectable()
export class AuthService {
  constructor(
    private userService: UserService,
    private jwtService: JwtService
    ){}

  async validateUser(email: string, pass: string): Promise<any> {
    const user = await this.userService.findByEmail(email);
    const isMatch = await comparePasswords(pass, user.password);

    if( user && isMatch) {
      const { password, ...result } = user;
      return result;
    }

    return null;
  }

  async signIn(user: any) {
    const payload = { username: user.email, sub: user.id };

    return this.jwtService.sign(payload)
  }
}

And inside the User controller, I'm calling the method signIn from the AuthService.

UserController

import {
  Body,
  Controller,
  Post,
  HttpException,
  HttpStatus,
  Request,
  UseGuards,
  Bind,
} from "@nestjs/common";
import { UserService } from "./user.service";
import { SignUpDataValidation } from "./user.validation";
import { hashPassword } from "../../utils/hash-password";
import { AuthGuard } from '@nestjs/passport';
import { AuthService } from '../auth/auth.servers';
import { LocalAuthGuard } from '../auth/local-auth.guard';

@Controller("user")
export class UserController {
  constructor(
    private userService: UserService,
    private authService: AuthService
    ) {}

  @UseGuards(LocalAuthGuard)
  @Post("/signin")
  @Bind(Request())
  async signIn(req) {
    return this.authService.signIn(req.user)
  }
}

UserModule

import { Module } from "@nestjs/common";
import { UserController } from "./user.controller";
import { UserService } from "./user.service";
import { TypeOrmModule } from "@nestjs/typeorm";
import { User } from "src/modules/user/user.entity";
import { AuthService } from '../auth/auth.servers';
import { JwtService } from '@nestjs/jwt';

@Module({
  imports: [TypeOrmModule.forFeature([User])],
  controllers: [UserController],
  providers: [UserService, AuthService, JwtService],
})
export class UserModule {}

LocalStrategy

import { Injectable, UnauthorizedException } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { Strategy } from 'passport-local';
import { AuthService } from './auth.servers';

@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
  constructor(private authService: AuthService) {
    super({usernameField: 'email'});
  }

  async validate(email: string, password: string): Promise<any> {
    const user = await this.authService.validateUser(email, 
password);
    if(!user) {
      throw new UnauthorizedException()
    }

    return user;
  }
}

As you can see, I'm also using Guards, but I'm not going to send you that code to avoid confusion. So, can anybody tell me, why I'm getting this ERROR? Am I missing something? Here is the link to the particular NestJS documentation page with a feature that I'm trying to implement: https://docs.nestjs.com/security/authentication

Rakonjac
  • 87
  • 9

0 Answers0