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]) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@dspangenberg @JonhnyDev
I managed to fix this by adding the following code to
contracts/hash.ts