Skip to content

Commit

Permalink
Merge pull request #41 from docusign/stored-auth-example
Browse files Browse the repository at this point in the history
create example of how to store and use authInfo
  • Loading branch information
Whoaa512 committed Jan 26, 2016
2 parents ea07f00 + d28e93c commit 5a2e32c
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 6 deletions.
12 changes: 6 additions & 6 deletions test/test-helpers.js → examples/auth-store-helpers.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
'use strict';

const Bluebird = require('bluebird');
const path = require('path');
const writeJson = require('write-json-file');
var Bluebird = require('bluebird');
var path = require('path');
var writeJson = require('write-json-file');

const authInfoPath = path.resolve('./auth-info.json');
var authInfoPath = path.resolve(__dirname, 'auth-info.json');

module.exports = {
getStoredAuthInfo,
storeAuthInfo
};

function getStoredAuthInfo () {
let authInfo;
var authInfo;
try {
authInfo = require(authInfoPath);
} catch (e) {
Expand All @@ -23,7 +23,7 @@ function getStoredAuthInfo () {

function storeAuthInfo (freshInfo) {
// make sure we don't overwrite
let storedInfo = getStoredAuthInfo();
var storedInfo = getStoredAuthInfo();
if (storedInfo == null) {
return writeJson(authInfoPath, freshInfo);
}
Expand Down
73 changes: 73 additions & 0 deletions examples/stored-auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
var docusign = require('docusign-node');
var authStoreHelpers = require('./auth-store-helpers');
var DocuSignError = docusign.DocuSignError;
var storeAuthInfo = authStoreHelpers.storeAuthInfo;
var getStoredAuthInfo = authStoreHelpers.getStoredAuthInfo;

var integratorKey = '***'; // Integrator Key associated with your DocuSign Integration
var email = 'YOUR_EMAIL'; // Email for your DocuSign Account
var password = 'YOUR_PASSWORD'; // Password for your DocuSign Account
var docusignEnv = 'demo'; // DocuSign Environment generally demo for testing purposes
var fullName = 'Joan Jett'; // Recipient's Full Name
var templateId = '***'; // ID of the Template you want to create the Envelope with
var templateRoleName = '***'; // Role Name of the Template
var debug = false; // Enable debug logging and debug responses from API

var templateRoles = [{
email: email,
name: fullName,
roleName: templateRoleName
}];

// **********************************************************************************
// Step 1 - Initialize DocuSign Object with Integrator Key and Desired Environment
// **********************************************************************************
docusign.init(integratorKey, docusignEnv, debug)
// **********************************************************************************
// Step 2 - Create a DocuSign Client Object
// **********************************************************************************
.then(function (response) {
var existingAuthInfo = getStoredAuthInfo();
if (response.message === 'succesfully initialized') {
if (existingAuthInfo != null) {
return docusign.createClientFromAuth(existingAuthInfo);
} else {
return docusign.createClient(email, password);
}
} else {
throw new Error('Did not initialize');
}
})
// **********************************************************************************
// Step 3 - Store the `authInfo` for later usage
// **********************************************************************************
.then(function (client) {
return [client, storeAuthInfo(client.authInfo)];
})
// **********************************************************************************
// Step 4 - Request Signature via Template
// **********************************************************************************
.spread(function (client) {
return [client, client.envelopes.sendTemplate('Sent from a Template', templateId, templateRoles)];
})
.spread(function (client, response) {
console.log('The envelope information of the created envelope is: \n' + JSON.stringify(response));
return client;
})
// **********************************************************************************
// Step 5 - Revoke OAuth Token for Logout
// **********************************************************************************
.then(function (client) {
return client.logOut();
})
.catch(DocuSignError, function (dsError) {
console.log('DocuSignError: ', dsError.stack || dsError);
process.exit(1);
})
.catch(function (error) {
console.log('Error: ', error.stack || error);
process.exit(1);
})
.finally(function () {
process.exit();
});

0 comments on commit 5a2e32c

Please sign in to comment.