Tutorials
React, NodeJS and Postgres
NodeJS Identity

NodeJs identity server

In this section, we will guide you through defining your first Vizzly identity config, and signing it into access tokens using the Vizzly auth package.

Basic NodeJS server

Firstly, create a new NodeJs project to build our auth server in, and install the express JS and Vizzly auth packages.

npm init
npm install express @vizzly/auth

Next, let’s create a basic express server for our React application to call. Create a file named app.js in the root directory, alongside to your vizzly-private.pem file.

touch app.js

Now paste the following code to your newly created app.js file.

const express = require("express");
const app = express();
 
// Enable CORS middleware
app.use((req, res, next) => {
  // * Is used only for demo purposes. Replace with the 
  // origin you wish to allow to keep your identity endpoint secure.
  res.setHeader("Access-Control-Allow-Origin", "*");
  res.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
  res.setHeader("Access-Control-Allow-Headers", "Content-Type");
 
  next();
});
 
app.post("/auth", (req, res) => {
  res.send("Auth endpoint working");
});
 
app.listen(7000, () => {
  console.log("Server listening on port 7000");
});

Now to run the app type node app.js into your terminal and let’s see it run. If successful, you’ll see Server listening on port 7000 in your console.

The identity config

Now, let's build and sign our identity config.

At the top of the app.js file, add the following imports

const fs = require("fs");
const { createSigner } = require("@vizzly/auth");

Replace the /auth route matcher in the app.js file, to use the Vizzly auth package to create access tokens from the identity config.

- app.post("/auth", (req, res) => {
-  res.send("Auth endpoint working");
- });
 
+ app.post("/auth", async (req, res) => {
+  const getIdentityTokens = async () => {
+    const privateKey = fs.readFileSync("./vizzly-private.pem").toString();
+
+    const signer = createSigner({ ttlInMinutes: 120, privateKey });
+
+    const identityConfig = {
+      dataSetIds: "*",
+      accessType: "standard",
+      organisationId: "<< ORGANISATION ID >>",
+      userReference: "user 12345",
+      scope: "read_write",
+      secureFilters: {},
+    };
+
+    return await signer.generateTokens(identityConfig);
+  };
 
+  res.send(await getIdentityTokens());
+ }

Replace the organisation ID

You will need to replace << ORGANISATION ID >> with your own organisation ID. You can find this value by running the CLI command

vizzly current-profile

Setting the user reference

The user reference in this example has been hardcoded as user 12345. However, when you run this in production, you will want to get this value from the current user, who has been authenticated in the same way your application authorizes and authenticates users.

The accessType

To learn more about the differences between admin and standard access types, read the identity docs.