routes/login.js
function password(req, res, test) {
  var options = {
    grant_type: "password",
    client_id:  process.env.SALESFORCE_CLIENT_ID,
    client_secret: process.env.SALESFORCE_CLIENT_SECRET,
    username: req.query.username || req.body.username || process.env.SALESFORCE_USERNAME,
    password: req.query.password || req.body.password || process.env.SALESFORCE_PASSWORD,
  }

  var protocol = "https://";
  if(test === true || req.test) protocol = "http://"; //for testing

  var loginServer = req.body.login_server || req.query.login_server || "login.salesforce.com";
  var url = protocol + loginServer + "/services/oauth2/token";

  var r = request.post(url)
  .type("application/x-www-form-urlencoded")
  .send(options)
  .on("error", function(err){ return onError(res, err); })
  .end(function(sfRes){
    if(sfRes.error) return onError(res, sfRes.error + " " + JSON.stringify(sfRes.body) );
    req.session.salesforce = JSON.stringify( sfRes.body );
    if (process.env.NODE_ENV === 'development') router.session = req.session.salesforce;
    res.redirect( req.query.app_url || process.env.SALESFORCE_FINAL_URL || "/login/whoami" )
  })
}
function password(req, res, test) {
  var options = {
    grant_type: "password",
    client_id:  process.env.SALESFORCE_CLIENT_ID,
    client_secret: process.env.SALESFORCE_CLIENT_SECRET,
    username: req.query.username || req.body.username || process.env.SALESFORCE_USERNAME,
    password: req.query.password || req.body.password || process.env.SALESFORCE_PASSWORD,
  }

  var protocol = "https://";
  if(test === true || req.test) protocol = "http://"; //for testing

  var loginServer = req.body.login_server || req.query.login_server || "login.salesforce.com";
  var url = protocol + loginServer + "/services/oauth2/token";

  var r = request.post(url)
  .type("application/x-www-form-urlencoded")
  .send(options)
  .on("error", function(err){ return onError(res, err); })
  .end(function(sfRes){
    if(sfRes.error) return onError(res, sfRes.error + " " + JSON.stringify(sfRes.body) );
    req.session.salesforce = JSON.stringify( sfRes.body );
    if (process.env.NODE_ENV === 'development') router.session = req.session.salesforce;
    res.redirect( req.query.app_url || process.env.SALESFORCE_FINAL_URL || "/login/whoami" )
  })
}
function password ( req , res , test ) { var options = { grant_type : "password" , client_id : process . env . SALESFORCE_CLIENT_ID , client_secret : process . env . SALESFORCE_CLIENT_SECRET , username : req . query . username || req . body . username || process . env . SALESFORCE_USERNAME , password : req . query . password || req . body . password || process . env . SALESFORCE_PASSWORD , } var protocol = "https://" ; if ( test === true || req . test ) protocol = "http://" ; //for testing var loginServer = req . body . login_server || req . query . login_server || "login.salesforce.com" ; var url = protocol + loginServer + "/services/oauth2/token" ; var r = request . post ( url ) . type ( "application/x-www-form-urlencoded" ) . send ( options ) . on ( "error" , function ( err ) { return onError ( res , err ) ; } ) . end ( function ( sfRes ) { if ( sfRes . error ) return onError ( res , sfRes . error + " " + JSON . stringify ( sfRes . body ) ) ; req . session . salesforce = JSON . stringify ( sfRes . body ) ; if ( process . env . NODE_ENV === 'development' ) router . session = req . session . salesforce ; res . redirect ( req . query . app_url || process . env . SALESFORCE_FINAL_URL || "/login/whoami" ) } ) }
Retrieves the Salesforce Auth and adds it to the session
Retrieves the Salesforce Auth and adds it to the session