Implementing trustless authentication with NodeJS

2 December 2020
2 min read

In What is Trustless Authentication we learned about the driving principles of Trustless Authentication and what makes it different from the more traditional Identity and Access Management (IAM) systems available on the market.

In this post we are going to explore how to implement NUID Trustless Authentication API in NodeJS based web application. In order to following along with this tutorial you will need to sign up to the NUID Developer Portal

You may also want to take the opportunity to register an account with Fauna - the data API for client-serverless applications because we'll be using it for our back-end database.

create a project

The following steps will create a simple project folder: Essentially we will be creating a basic nodejs project, git repository, Github remote repository.

Some steps below have been further explained in the following articles:

mkdir trustless-auth && cd trustless-auth
npm init -y
gitignore node
touch README.md
git init
gh repo create Geek-I-AM/trustless-auth

Install Dependencies

In order to create our simple project, we will need to install the following dependencies.

npm i faunadb @nuid/zk node-fetch express body-parser dotenv 

Create a simple Node Webserver

We now have all the dependencies we need installed, we can now start developing the basic skeleton of our application.

We'll iniitally create a very simple node based webserver that we will use to serve our application. Add new file to our repository and name it index.js, then add the following code to create our webserver.

const bodyParser = require('body-parser');
const express = require('express');

const DEFAULT_PORT = process.env.DEFAULT_PORT || 8001;
const HOST = process.env.HOST || '0.0.0.0';

const app = express();

app.use(bodyParser.json())

app.listen(DEFAULT_PORT, HOST, err => {
    if (err) {
        return console.log(err);
    }
    console.log('\n\tStarting server...');
    console.log(`Running locally at ${HOST}:${DEFAULT_PORT}`);
});

We can now test our webserver, works by running node index.js in our terminal window, then we'll see our simple webserver will start up.

Simple Web server