Skip to main content
Version: v3.x

Validate Credentials

Introduction

In this recipe, you'll learn how to compare a raw text value, such as a user password, with a stored hashed password. This is critical when authenticating users securely in your application.

Prerequisites

Before continuing, ensure you have:

NB: The bcrypt library is a secure and widely supported method for password handling across various 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";

/**
* @readonly
*/
export async function comparePassword(password: string, hashedPassword: string): Promise<boolean> {
return await bcrypt.compare(password, 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. Create a relationship (optional)

It's a safe assumption that the argument's input type matches that of a password field belonging to a User model; you can create a relationship from the type to the command. This will enable you to make nested queries that will invoke your custom business logic using the value of the field from the related model!

Create a relationship in the corresponding model's HML file.

For example, if we have a Users model:
---
kind: Relationship
version: v1
definition:
name: comparePassword
sourceType: Users
target:
command:
name: ComparePassword
mapping:
- source:
fieldPath:
- fieldName: password
target:
argument:
argumentName: hashedPassword

Step 4. 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 should be able to make a query like this:

If you created a relationship, you can make a query like this, too:

Wrapping up

In this guide, you learned how to securely compare raw text values to hashed passwords to authenticate users in your API. By leveraging lambda connectors with relationships, you can add custom business logic to your authentication flows.

Learn more about lambda connectors

Similar recipes

Loading...