Export better-auth password's default hash and verify functions #826

Closed
opened 2026-03-13 08:05:57 -05:00 by GiteaMirror · 3 comments
Owner

Originally created by @Manethpak on GitHub (Mar 11, 2025).

Is this suited for github?

  • Yes, this is suited for github

When seeding databases with mock users for development or testing, there's currently no easy way to generate proper password hashes that are compatible with better-auth's unless I switches to custom password hashing algorithm, which may result in security concerned.

Describe the solution you'd like

Export the default password hash and verify functions from the better-auth library as public API methods. This would allow developers to directly use these functions when creating mock users or seeding databases with test data.

import { password } from "better-auth";

/**
 * Hashes a plaintext password using the same algorithm as better-auth
 * @param {string} plaintext - The plaintext password to hash
 * @returns {Promise<string>} The complete hashed password string
 */
const hashedPassword = await password.hash("WeakP@ssw0rd");

/**
 * Verifies if a plaintext password matches a stored hash
 * @param {string} plaintext - The plaintext password to check
 * @param {string} hashedPassword - The previously hashed password to compare against
 * @returns {Promise<boolean>} True if the password matches, false otherwise
 */
const isValid = await password.verify("WeakP@ssw0rd", hashedPassword);

Describe alternatives you've considered

Current workarounds that I've used:

  • Creating users through the regular registration flow and extracting hashes from the database, reusing the hashes for every mock users
  • Use better-auth client, however it request the server to be available in order to seed it.
  • Implementing custom hash functions that may not match better-auth's implementation

Additional context

This feature would improve the developer experience without compromising security

Originally created by @Manethpak on GitHub (Mar 11, 2025). ### Is this suited for github? - [x] Yes, this is suited for github ### Is your feature request related to a problem? Please describe. When seeding databases with mock users for development or testing, there's currently no easy way to generate proper password hashes that are compatible with better-auth's unless I switches to custom password hashing algorithm, which may result in security concerned. ### Describe the solution you'd like Export the default password hash and verify functions from the better-auth library as public API methods. This would allow developers to directly use these functions when creating mock users or seeding databases with test data. ```ts import { password } from "better-auth"; /** * Hashes a plaintext password using the same algorithm as better-auth * @param {string} plaintext - The plaintext password to hash * @returns {Promise<string>} The complete hashed password string */ const hashedPassword = await password.hash("WeakP@ssw0rd"); /** * Verifies if a plaintext password matches a stored hash * @param {string} plaintext - The plaintext password to check * @param {string} hashedPassword - The previously hashed password to compare against * @returns {Promise<boolean>} True if the password matches, false otherwise */ const isValid = await password.verify("WeakP@ssw0rd", hashedPassword); ``` ### Describe alternatives you've considered Current workarounds that I've used: - Creating users through the regular registration flow and extracting hashes from the database, reusing the hashes for every mock users - Use better-auth client, however it request the server to be available in order to seed it. - Implementing custom hash functions that may not match better-auth's implementation ### Additional context This feature would improve the developer experience without compromising security
Author
Owner

@namuorg commented on GitHub (Mar 11, 2025):

You can access better-auth's hash and verify functions using auth.$context (example):

const ctx = await auth.$context;
const hash = await ctx.password.hash("your-new-password");
@namuorg commented on GitHub (Mar 11, 2025): You can access `better-auth`'s hash and verify functions using `auth.$context` ([example](https://www.better-auth.com/docs/authentication/email-password#update-password)): ```typescript const ctx = await auth.$context; const hash = await ctx.password.hash("your-new-password"); ```
Author
Owner

@Manethpak commented on GitHub (Mar 12, 2025):

I was not aware of this the last time I checked the documentation, anyway thank you for the answer!

@Manethpak commented on GitHub (Mar 12, 2025): I was not aware of this the last time I checked the documentation, anyway thank you for the answer!
Author
Owner

@itisyb commented on GitHub (Apr 27, 2025):

I tried this do set a default password for my users but it returns new hash each time for example I wanted ctx.password.hash("123456");

so all my user can use that for first time login and change if they want

do you know how can I do that @namukang would be great help

@itisyb commented on GitHub (Apr 27, 2025): I tried this do set a default password for my users but it returns new hash each time for example I wanted ctx.password.hash("123456"); so all my user can use that for first time login and change if they want do you know how can I do that @namukang would be great help
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#826