All files / lib/jwt JWTService.ts

71.42% Statements 5/7
0% Branches 0/2
0% Functions 0/2
60% Lines 3/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 292x 2x             2x                                        
import { Service } from '../di/di';
import jwt from 'jsonwebtoken';
 
/**
 * @class JWTService
 * @description A service for generating JWT tokens.
 */
@Service()
export class JWTService {
  private readonly secret: string;
 
  /**
   * @constructor
   * @description Initializes the secret from environment variables or a default value.
   */
  constructor() {
    this.secret = process.env.JWT_SECRET || 'default-secret';
  }
 
  /**
   * @method generateToken
   * @description Generates a JWT token.
   * @param {object} payload - The payload to include in the token.
   * @returns {string} The generated token.
   */
  public generateToken(payload: object): string {
    return jwt.sign(payload, this.secret, { expiresIn: '1h' });
  }
}