Skip to content

Session establishment example

CI edited this page Apr 2, 2019 · 2 revisions

In this example Alice logs in, gets her public key and logs out.

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

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

function printPublicKey(publicKey) {
  let publicKeyStr = Buffer.from(publicKey).toString("hex");
  console.log("Alice's public key: " + publicKeyStr);
}

async function main() {
  // Alice establishes a session
  let aliceSession = await DataPeps.Session.login("aliceliddell", "aliceP@ssw0rd");
  // Alice gets her public key...
  let alicePublicKey = await aliceSession.getSessionPublicKey();
  // ...and prints it out
  printPublicKey(alicePublicKey.box);
  // Alice closes the session
  await aliceSession.close();
  // Alice tries to renew keys, but the exception occurs,
  // because Alice has closed the session
  await aliceSession.renewKeys().catch(_ => {
    console.log("Session is closed, so Alice cannot renew her keys");
  });
}

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/8236b508c31016386a8489c9c0deaedc.git session-establishment

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

node session-establishment/session-establishment.js