Example #1
0
function getConnection(req, res){
  if( !req.session.salesforce && !req.get("salesforce") ){
    onError(res, "Session is not valid, please login");
    return false;
  }
  var session = JSON.parse(req.session.salesforce || req.get("salesforce"));

  var credentials = { 
    oauth2 : {
      clientId : process.env.SALESFORCE_CLIENT_ID,
      clientSecret : process.env.SALESFORCE_CLIENT_SECRET,
      redirectUri : process.env.SALESFORCE_REDIRECT_URL
    },
    instanceUrl : session.instance_url,
    accessToken : session.access_token,
    refreshToken : session.refresh_token
  }

  var conn = new jsforce.Connection( credentials );
    
  conn.on("refresh", function(accessToken, res) {
    req.session.salesforce.access_token = accessToken;
  });

  req.conn = conn;
  return true;
}