AdonisJS 5: Solution for use (adonis 4 and laravel) compatible bcrypt #1549
Replies: 4 comments 9 replies
-
@reg2005 That's great. Can you create a PR for the same in the hash module? We can basically enable this using the legacy mode. For example: The config will accept an additional param |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
@lucaswx2 maybe i'm late, but if you have the problem with this.$container, the correct syntax is this.app.container. |
Beta Was this translation helpful? Give feedback.
-
modified version of @amitappaspect and @reg2005 code for adonis js v6.
import {
HashDriverContract,
ManagerDriverFactory
} from '@adonisjs/core/types/hash'
/**
* need to polyfill the require function
*/
import { createRequire } from "module";
const require = createRequire(import.meta.url);
const bcrypt = require('bcrypt');
const previousAlgorithm = '$2y$12$' // bcrypt algorithm from the few characters on the start of the previously encrypted password
const currentAlgorithm = '$2b$12$' // this is for the current encrypted password
const saltRounds = 12 // match the salt rounds of the previous algorithm, it's on the second $ sign
export class LaravelHashDriver implements HashDriverContract {
/**
* Convert raw value to Hash,
* used when creating new password for user
*/
async make(value: string): Promise<string> {
const _hashedValue = bcrypt.hashSync(value, saltRounds);
return _hashedValue;
}
/**
* Verify if the plain value matches the provided hash,
* used on login
*/
async verify( hashedValue: string, plainValue: string ): Promise<boolean> {
let currentHash = hashedValue;
if (hashedValue.includes(previousAlgorithm)) {
currentHash = hashedValue.replace(previousAlgorithm, currentAlgorithm);
}
return await bcrypt.compareSync(plainValue, currentHash);
}
/**
* idk the logic for this, so i just put it to default value
*/
needsReHash(): boolean {
return false;
}
isValidHash(): boolean {
return true
}
}
/**
* Factory function so you dont need to intiate it before using it
*/
export function LaravelHashFunction (): ManagerDriverFactory {
return () => {
return new LaravelHashDriver()
}
}
import { LaravelHashFunction } from '../driver/LaravelHashDriver.js'
// ... other code ...
const hashConfig = defineConfig({
default: 'laravel_hash',
list: {
laravel_hash: LaravelHashFunction()
},
})
// ... other code ...
const AuthFinder = withAuthFinder(() => hash.use('laravel_hash'), {
uids: ['email'],
passwordColumnName: 'password',
}) credit : adonisjs/auth#144 (comment) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
When I migrated my old website to Adonis 5. I found what I can't sign in my site, because Adonis 5 use another bcrypt library than Adonis 4
I got error:
Compare one password in between versions
AdonisJS 4
AdonisJS 5:
It's not compatible.
My solution:
Add file to libs/Bcrypt.ts:
And make provider like this:
That's all 👋
Beta Was this translation helpful? Give feedback.
All reactions