Skip to content

Registration example

CI edited this page Apr 2, 2019 · 2 revisions

In this example we register three identities, Alice, Bob, and Charlie. Those users will be used in other examples.

Show code
var DataPeps = require("datapeps-sdk");

global.fetch = require("node-fetch");
global.Headers = global.fetch.Headers;

let aliceLogin = "aliceliddell",
    bobLogin = "bobmorane",
    charlieLogin = "charliebucket";

let alice = {
  login: aliceLogin,
  name: "Alice",
  kind: "user",
  payload: new TextEncoder().encode(
    JSON.stringify({ firstname: "Alice", lastname: "Liddell", tel: "555-0101" })
  )
};

let bob = {
  login: bobLogin,
  name: "Bob",
  kind: "user",
  payload: new TextEncoder().encode(
    JSON.stringify({ firstname: "Bob", lastname: "Morane", tel: "555-0102" })
  )
};

let charlie = {
  login: charlieLogin,
  name: "Charlie",
  kind: "user",
  payload: new TextEncoder().encode(
    JSON.stringify({ firstname: "Charlie", lastname: "Bucket", tel: "555-0103" })
  )
};

let aliceSecret = "aliceP@ssw0rd",
  bobSecret = "bobP@ssw0rd",
  charlieSecret = "charlieP@ssw0rd";

async function main() {
  Promise.all([
    registerUser(alice, aliceSecret),
    registerUser(bob, bobSecret),
    registerUser(charlie, charlieSecret)
  ]);
}

async function registerUser(user, userSecret) {
  return await DataPeps.register(user, userSecret).catch(e => {
    if (
      e instanceof DataPeps.Error &&
      e.kind == DataPeps.ServerError.IdentityAlreadyExists
    ) {
      console.log("User " + user.login + " already exists");
      return;
    }
    throw e;
  });
}

main().catch(e => console.log("An error occurred: ", e));

Running the example

Before running the example make sure you followed the steps described here.

To fetch the code run the following command in the examples directory:

git clone https://gist.github.com/946a5c21f551c60cc5f9b1f84e6d8120.git register-identity

To run this example execute the following command in the examples directory:

node register-identity/register-identity.js