Using BcryptJS with Better-Auth

Hi everyone, I’m looking for an example of how to use BcryptJS with Better-Auth. I have existing users with bcrypt-hashed passwords, so I need to configure the password option properly for both hashing and verifying passwords. The documentation mentions: "You can customize the password hashing function by setting the password option in the configuration. This option should include a hash function to hash passwords and a verify function to verify them." However, I haven’t found any examples or further details, and this option isn’t listed in the options reference. If anyone can provide an example or point me in the right direction, I’d really appreciate it! Thanks!
5 Replies
lonelyplanet
lonelyplanet3d ago
check the docs here
DragonCoder99
DragonCoder99OP3d ago
it doesn't give a full example... but it got me closer but not sure how to write those functions for bcryptjs as they need input and so on.
import { betterAuth } from "better-auth"
import bcrypt from "bcryptjs";

export const auth = betterAuth({
//...rest of the options
emailAndPassword: {
password: {
hash: // your custom password hashing function
verify: // your custom password verification function
}
}
})
import { betterAuth } from "better-auth"
import bcrypt from "bcryptjs";

export const auth = betterAuth({
//...rest of the options
emailAndPassword: {
password: {
hash: // your custom password hashing function
verify: // your custom password verification function
}
}
})
import bcrypt from "bcryptjs";

// To hash a password:
const salt = bcrypt.genSaltSync(10);
const hash = bcrypt.hashSync("pass123", salt);

// To verify a password:
bcrypt.compareSync("pass123", hash); // true
bcrypt.compareSync("wrong_pass", hash); // false
import bcrypt from "bcryptjs";

// To hash a password:
const salt = bcrypt.genSaltSync(10);
const hash = bcrypt.hashSync("pass123", salt);

// To verify a password:
bcrypt.compareSync("pass123", hash); // true
bcrypt.compareSync("wrong_pass", hash); // false
lonelyplanet
lonelyplanet3d ago
/**
* Password hashing and verification
*
* By default Scrypt is used for password hashing and
* verification. You can provide your own hashing and
* verification function. if you want to use a
* different algorithm.
*/
password?: {
hash?: (password: string) => Promise<string>;
verify?: (data: { hash: string; password: string }) => Promise<boolean>;
};
/**
* Password hashing and verification
*
* By default Scrypt is used for password hashing and
* verification. You can provide your own hashing and
* verification function. if you want to use a
* different algorithm.
*/
password?: {
hash?: (password: string) => Promise<string>;
verify?: (data: { hash: string; password: string }) => Promise<boolean>;
};
straight from the source code types which you can find here
GitHub
better-auth/packages/better-auth/src/types/options.ts at main · bet...
The most comprehensive authentication framework for TypeScript - better-auth/better-auth
lonelyplanet
lonelyplanet3d ago
@DragonCoder99 You would simply do
import { betterAuth } from "better-auth"
import {hash,verify} from "my-hashing-library"; // choose whatever library you wish

export const auth = betterAuth({
//...rest of the options
emailAndPassword: {
password: {
hash: async (password) => {
// the password after hashing e.g. const passwordHash = await hash(password);
const passwordHash = await hash(password);

return hashedPassword; // this is what will be stored as the password hash
}
verify: async ({ hash, password }) => {
// verify that hash of password matches the stored hash eg: const isCorrectPassword = await verify(hash, password);
const isCorrectPassword = await verify(hash, password);

return isCorrectPassword
}
}
}
})
import { betterAuth } from "better-auth"
import {hash,verify} from "my-hashing-library"; // choose whatever library you wish

export const auth = betterAuth({
//...rest of the options
emailAndPassword: {
password: {
hash: async (password) => {
// the password after hashing e.g. const passwordHash = await hash(password);
const passwordHash = await hash(password);

return hashedPassword; // this is what will be stored as the password hash
}
verify: async ({ hash, password }) => {
// verify that hash of password matches the stored hash eg: const isCorrectPassword = await verify(hash, password);
const isCorrectPassword = await verify(hash, password);

return isCorrectPassword
}
}
}
})
DragonCoder99
DragonCoder99OP3d ago
thanks! 🙂

Did you find this page helpful?