import { genSalt , hash } from 'bcryptjs' ; import { compare } from 'bcryptjs' ; import { inject } from '@loopback/core' ; import { PasswordHasherBindings } from '../keys' ; /**
 * Service HashPassword using module 'bcryptjs'.
 * It takes in a plain password, generates a salt with given
 * round and returns the hashed password as a string
 */ export type HashPassword = ( password : string , rounds : number ) => Promise < string > ; export async function hashPassword ( password , rounds ) { const salt = await genSalt ( rounds ) ; return hash ( password , salt ) ; } export interface PasswordHasher < T = string > { hashPassword ( password ) : Promise < T > ; comparePassword ( providedPass , storedPass ) : Promise < boolean > ; } export class BcryptHasher implements PasswordHasher < string > { constructor ( @ inject ( PasswordHasherBindings . ROUNDS ) private readonly rounds ) { } async hashPassword ( password ) { const salt = await genSalt ( this . rounds ) ; return hash ( password , salt ) ; } async comparePassword ( providedPass , storedPass ) { const passwordIsMatched = await compare ( providedPass , storedPass ) ; return passwordIsMatched ; } }