Last active
November 30, 2023 16:57
-
-
Save thetutlage/cf32e18d66408fd96fc8b7d3591c7a1a to your computer and use it in GitHub Desktop.
Creating and register custom hash driver in AdonisJS v5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"preloads": [ | |
"./start/hashDriver", | |
"./start/routes", | |
"./start/kernel" | |
], | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Add custom driver to the config file | |
*/ | |
{ | |
list: { | |
custom: { | |
driver: 'custom-bcrypt', | |
saltRounds: 10, | |
}, | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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, | |
}, | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
|-------------------------------------------------------------------------- | |
| 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]) | |
}) |
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
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
@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
Thank you for this.
For anyone requiring this, the
hash
method is now deprecated, provide amake
function as well.And if you are using the
bycript
package replacebycript.verify
withbycript.compare
.