Created
October 9, 2020 17:46
-
-
Save mbraga-sfdc/6855f71bf53e543714f7a536ba037ec4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const jwt = require('salesforce-jwt-bearer-token-flow'); | |
const jsforce = require('jsforce'); | |
require('dotenv').config(); | |
const { SF_CONSUMER_KEY, SF_USERNAME, SF_LOGIN_URL } = process.env; | |
let SF_PRIVATE_KEY = process.env.SF_PRIVATE_KEY; | |
if (!SF_PRIVATE_KEY) { | |
SF_PRIVATE_KEY = require('fs').readFileSync('private.pem').toString('utf8'); | |
} | |
exports.getSalesforceConnection = function () { | |
return new Promise(function (resolve, reject) { | |
jwt.getToken( | |
{ | |
iss: SF_CONSUMER_KEY, | |
sub: SF_USERNAME, | |
aud: SF_LOGIN_URL, | |
privateKey: SF_PRIVATE_KEY | |
}, | |
(err, tokenResponse) => { | |
if (tokenResponse) { | |
let conn = new jsforce.Connection({ | |
instanceUrl: tokenResponse.instance_url, | |
accessToken: tokenResponse.access_token | |
}); | |
resolve(conn); | |
} else { | |
reject('Authentication to Salesforce failed'); | |
} | |
} | |
); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment