developer239/nest-rest-api

View on GitHub
src/modules/auth/crypto/crypto.service.ts

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
import { Injectable } from '@nestjs/common'
import { InjectRepository } from '@nestjs/typeorm'
import * as bcrypt from 'bcryptjs'
import { UserRepository } from 'src/modules/auth/user/user.repository'

@Injectable()
export class CryptoService {
  constructor(
    @InjectRepository(UserRepository)
    private readonly userRepository: UserRepository
  ) {}

  async validatePassword(
    testPassword: string,
    hashedPassword: string,
    salt: string
  ): Promise<boolean> {
    const hash = await bcrypt.hash(testPassword, salt)

    return hash === hashedPassword
  }

  hashPassword(password: string, salt: string): Promise<string> {
    return bcrypt.hash(password, salt)
  }
}