fastify-xauth-local
password.hash
Hash a plaintext password with bcrypt using the configured salt rounds.
password.hash
Hashes a plaintext password with bcrypt. Use this to hash passwords before storing them, or anywhere you need to generate a bcrypt hash outside the built-in registration route.
Signature
fastify.xauthlocal.password.hash(
password: string,
rounds?: number // default: 10
): Promise<string>
Params
| Name | Type | Required | Description |
|---|---|---|---|
password | string | Yes | Plaintext password to hash |
rounds | number | No | bcrypt salt rounds (default: 10). Higher values are slower but more secure. |
Returns
A Promise<string> that resolves to the bcrypt hash.
Throws
Rejects if password is not a string or if bcrypt encounters an error.
Examples
Basic: hash before storing in a database
fastify.post("/admin/users", async (request) => {
const { email, password } = request.body;
const hash = await fastify.xauthlocal.password.hash(password);
await db.users.create({ email, password: hash });
return { created: true };
});
Advanced: custom salt rounds for high-security contexts
fastify.post("/api/change-password", async (request) => {
const { newPassword } = request.body;
// Use 12 rounds for password changes (slightly slower, more secure)
const hash = await fastify.xauthlocal.password.hash(newPassword, 12);
await db.users.updatePassword(request.auth.id, hash);
return { updated: true };
});
See Also
- password.compare — verify a plaintext password against a stored hash
- local.login — built-in login route that hashes/compares automatically
AI Context
package: "@xenterprises/fastify-xauth-local"
method: fastify.xauthlocal.password.hash(plaintext, saltRounds?)
use-when: Hash a plaintext password with bcrypt before storing
returns: Promise<string> (bcrypt hash)
