Example file

06-signed-identity.html

Using a signed identity pass, and what happens when the firm id is edited.

To try this page, download it, replace the placeholders and put it on a test page of your website. Opening the file from your computer won't work, because the embeds check the website address they're on.

06-signed-identity.html · HTML Download
<!doctype html>
<!--
  Signed identity: your server creates a pass for the firm and adviser each
  time it renders the page, and the page hands it to the widget, which sends
  it with every request. Subcontext refuses any request whose firm id or
  adviser id differs from the pass. With the edited firm id, Subcontext
  answers identity_mismatch, the widget shows "Not enabled yet", and it fires
  subcontext:nurture:refused (shown in the log).

  Replace YOUR_WIDGET_KEY, YOUR_FIRM_ID and SIGNED_PASS_FROM_YOUR_SERVER. The
  pass is made on your server, never in the page. See "Creating the pass"
  at https://subcontext.com/developers/embeds/nurture/signed-identity#creating-the-pass
  (Node.js, Ruby and C#). It uses a key
  id (nhk_...) and a secret (nhs_...) that Subcontext provides; keep the
  secret on your server.

  In a single-page portal, if the adviser changes without a reload, fetch a
  new pass for them from your server and pass it along:
    SubcontextNurture.setConsumer(client, { name: 'Alex Example', externalId: 'ADVISER-9' },
                                  { identityToken: newPass })
-->
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width,initial-scale=1">
  <title>Nurture: signed identity</title>
</head>
<body>
  <h1>Signed identity</h1>

  <p>
    <button id="genuine">Start with the signed firm and adviser</button>
    <button id="tampered">Start with an edited firm id</button>
    <button id="open">open()</button>
  </p>
  <p>Firm id in use: <code id="firm"></code></p>

  <h2>Log</h2>
  <ul id="log"></ul>

  <script id="nurture-embed-script" src="https://js.subcontext.com/nurture-latest.js"></script>
  <script>
    // A firm id Subcontext has set up for you. It must be the one the pass
    // was created for.
    var firmId = 'YOUR_FIRM_ID'

    function log(label, detail) {
      var item = document.createElement('li')
      item.textContent = label + (detail === undefined ? '' : '  ' + JSON.stringify(detail))
      document.getElementById('log').prepend(item)
    }

    window.addEventListener('subcontext:nurture:refused', function (event) { log('event refused', event.detail) })

    function start(firm) {
      document.getElementById('firm').textContent = firm
      log('init() for firm ' + firm, SubcontextNurture.init({
        // Your widget key (starts nwk_), provided by Subcontext.
        integrationKey: 'YOUR_WIDGET_KEY',
        firmId: firm,
        // The pass your server created for firmId and ADVISER-7, using the
        // signing key Subcontext provides. See "Creating the pass" at
        // https://subcontext.com/developers/embeds/nurture/signed-identity#creating-the-pass
        identityToken: 'SIGNED_PASS_FROM_YOUR_SERVER',
        // Must be the adviser the pass was created for.
        advisor: { name: 'Sam Example', externalId: 'ADVISER-7' },
        // Test messages are sent for real. Put your own email address here.
        consumer: { firstName: 'Jane', lastName: 'Example', email: 'you@your-firm.example' }
      }))
      // Open straight away so Subcontext's answer shows.
      SubcontextNurture.open()
    }

    document.getElementById('genuine').addEventListener('click', function () { start(firmId) })
    document.getElementById('tampered').addEventListener('click', function () { start(firmId + '-EDITED') })
    document.getElementById('open').addEventListener('click', function () { SubcontextNurture.open() })
    start(firmId)
  </script>
</body>
</html>

Stuck, or something missing?

Get in touch and a person on the team will help.

Contact us