Skip to main content
Version: v3.x (DDN)

Hash Passwords

Introduction​

In this recipe, you'll learn how to securely hash passwords to protect user credentials in your application. Password hashing is an essential part of securing sensitive information before storing it in your database.

Prerequisites

Before continuing, ensure you have:

NB: Bcrypt is a common, well-tested library for password hashing in various languages, and it's widely supported across many systems.

Recipe​

Step 1. Write the function​

In your connector's directory, install the bcrypt package:
npm install bcryptjs
In your functions.ts file, add the following:
import bcrypt from "bcryptjs";

export async function hashPassword(password: string): Promise<string> {
const salt = await bcrypt.genSalt(10);
const hashedPassword = await bcrypt.hash(password, salt);

// Add your own logic here to hit your Hasura endpoint and perform an insertion

return hashedPassword;
}

Step 2. Track your function​

To add your function, generate the related metadata that will link together any functions in your lambda connector's source files and your API:

ddn connector introspect <connector_name>

Then, you can generate an hml file for the function using the following command:

ddn command add <connector_name> "*"

Step 3. Test your function​

Create a new build of your supergraph:

ddn supergraph build local

In your project's explorer, you should see the new function exposed as a type and you should be able to execute a mutation like this:

Wrapping up​

In this guide, you learned how to enhance your API by securely hashing passwords before storing them in your database. Your API clients can invoke this mutation and you can handle all of the logic of hashing and inserting the new record directly from your API.

Learn more about lambda connectors​

Similar recipes​