Skip to content

On demand authentication

Sergei Sergeev edited this page Aug 8, 2017 · 5 revisions

On demand authentication using interactive browser session.

Credentials options:

  • ondemand - required boolean, should be always equal to true
  • electron - optional string, path to your electron executable. If empty, global electron will be used, i.e. electron. Read Configuration required section below for details
  • force - optional boolean, default false, when true forces electron to show a site with credentials dialog
  • persist - optional boolean, default true, when true, saves authentication data on the disk in a user folder in an encrypted manner

Example:

{
    ondemand: true,
    electron: require('electron'),
    force: false,
    persist: true
}

Resolving object:

{
  headers: {
    'Cookie': 'FedAuth=77u/PD94bWwgdm....'
  }
}

Configuration required:

On demand option uses electron in order to open SharePoint site url and let you enter credentials. node-sp-auth further reads and optionally (persist option) stores authentication cookies.
If you are building an app with electron, you can require it using credential options: electron: require('electron'). Otherwise you need to explicitly install it globally via npm:

npm install electron -g

Sample using:

var spauth = require('node-sp-auth');
var request = require('request-promise');

spauth
  .getAuth('https://sp2013dev/sites/dev/', {
    ondemand: true
  })
  .then(function (data) {
    var headers = data.headers;
    headers['Accept'] = 'application/json;odata=verbose';

    request.get({
      url: 'https://sp2013dev/sites/dev/_api/web',
      headers: headers,
      json: true,
      rejectUnauthorized: false
    }).then(function (response) {
      console.log(response.d.Title);
    });
  });