From d28e93cfd82bcba3412f5a981dbdb92e13412425 Mon Sep 17 00:00:00 2001 From: "C.J. Winslow" Date: Tue, 26 Jan 2016 12:20:25 -0800 Subject: [PATCH] create example of how to store and use authInfo fix #22 --- .../auth-store-helpers.js | 12 +-- examples/stored-auth.js | 73 +++++++++++++++++++ 2 files changed, 79 insertions(+), 6 deletions(-) rename test/test-helpers.js => examples/auth-store-helpers.js (63%) create mode 100644 examples/stored-auth.js diff --git a/test/test-helpers.js b/examples/auth-store-helpers.js similarity index 63% rename from test/test-helpers.js rename to examples/auth-store-helpers.js index 36a03e334..a48c085c0 100644 --- a/test/test-helpers.js +++ b/examples/auth-store-helpers.js @@ -1,10 +1,10 @@ '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, @@ -12,7 +12,7 @@ module.exports = { }; function getStoredAuthInfo () { - let authInfo; + var authInfo; try { authInfo = require(authInfoPath); } catch (e) { @@ -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); } diff --git a/examples/stored-auth.js b/examples/stored-auth.js new file mode 100644 index 000000000..eabd62560 --- /dev/null +++ b/examples/stored-auth.js @@ -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(); + });