Skip to content

Instantly share code, notes, and snippets.

@thetutlage
Last active November 30, 2023 16:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thetutlage/cf32e18d66408fd96fc8b7d3591c7a1a to your computer and use it in GitHub Desktop.
Save thetutlage/cf32e18d66408fd96fc8b7d3591c7a1a to your computer and use it in GitHub Desktop.
Creating and register custom hash driver in AdonisJS v5
{
"preloads": [
"./start/hashDriver",
"./start/routes",
"./start/kernel"
],
}
/**
* Add custom driver to the config file
*/
{
list: {
custom: {
driver: 'custom-bcrypt',
saltRounds: 10,
},
}
}
/**
* Contract source: https://git.io/Jfefs
*
* Feel free to let us know via PR, if you find something broken in this contract
* file.
*/
/**
* Replace `contracts/hash.ts` with this
*/
declare module '@ioc:Adonis/Core/Hash' {
import { HashDrivers } from '@ioc:Adonis/Core/Hash'
interface HashersList {
bcrypt: {
config: BcryptConfig,
implementation: BcryptContract,
},
custom: {
config: { saltRounds: number, driver: 'custom-bcrypt' },
implementation: HashDriverContract,
}
argon: {
config: ArgonConfig,
implementation: ArgonContract,
},
}
}
/*
|--------------------------------------------------------------------------
| Preloaded File
|--------------------------------------------------------------------------
|
| Any code written inside this file will be executed during the application
| boot.
|
*/
/**
* Write this inside `start/hashDriver.ts`
*/
import bcrypt from 'bcrypt'
import Hash, { HashDriverContract } from '@ioc:Adonis/Core/Hash'
/**
* Implementation of custom bcrypt driver
*/
class CustomBcryptDriver implements HashDriverContract {
constructor (private config: { saltRounds: number }) {
}
/**
* Ignore these. The PHC format requires it
*/
public params = {}
public ids = []
/**
* Has to be false, since bcrypt cannot find if password needs
* a re-hash or not
*/
public needsReHash () {
return false
}
/**
* Hash value
*/
public async hash (value: string) {
return bcrypt.hash(value, this.config.saltRounds)
}
/**
* Verify value
*/
public async verify (hashedValue: string, plainValue: string) {
return bcrypt.verify(plainValue, hashedValue)
}
}
Hash.extend('custom-bcrypt', function () {
return new CustomBcryptDriver(arguments[2])
})
@anthonmanix
Copy link

Thank you for this.

For anyone requiring this, the hash method is now deprecated, provide a make function as well.
And if you are using the bycript package replace bycript.verify with bycript.compare.

@dspangenberg
Copy link

Hi,

thank you for the code. In development, the driver works fine. But on compilation I get an error:

$ node ace build --production
[ info ]  cleaning up "./build" directory
[ info ]  compiling typescript source files
[ error ]  typescript compiler errors
config/hash.ts:70:7 - error TS2322: Type '"custom-bcrypt"' is not assignable to type '"bcrypt" | "argon2"'.

70       driver: 'custom-bcrypt',
         ~~~~~~

  node_modules/@adonisjs/hash/build/adonis-typings/hash.d.ts:30:9
    30         driver: 'bcrypt';
               ~~~~~~
    The expected type comes from property 'driver' which is declared here on type '(BcryptConfig & { driver: "bcrypt"; }) | (ArgonConfig & { driver: "argon2"; })'

Thank you in advance and greetings,
Danny

@JonhnyDev
Copy link

Hi,

thank you for the code. In development, the driver works fine. But on compilation I get an error:

$ node ace build --production
[ info ]  cleaning up "./build" directory
[ info ]  compiling typescript source files
[ error ]  typescript compiler errors
config/hash.ts:70:7 - error TS2322: Type '"custom-bcrypt"' is not assignable to type '"bcrypt" | "argon2"'.

70       driver: 'custom-bcrypt',
         ~~~~~~

  node_modules/@adonisjs/hash/build/adonis-typings/hash.d.ts:30:9
    30         driver: 'bcrypt';
               ~~~~~~
    The expected type comes from property 'driver' which is declared here on type '(BcryptConfig & { driver: "bcrypt"; }) | (ArgonConfig & { driver: "argon2"; })'

Thank you in advance and greetings, Danny

same here

@Ovoda
Copy link

Ovoda commented Nov 30, 2023

@dspangenberg @JonhnyDev
I managed to fix this by adding the following code to contracts/hash.ts

declare module '@ioc:Adonis/Core/Hash' {
  export interface HashDrivers {
    'no-phc-bcrypt': {
      config: { saltRounds: number; driver: 'no-phc-bcrypt' }
      implementation: HashDriverContract
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment