routes/login.js
function loginCallback(req, res, test) {
  var state = JSON.parse(req.query.state);
  console.dir( state );
  var protocol = "https://";
  if(test === true || req.test) protocol = "http://";  //for testing

  var url = protocol + state.loginServer + "/services/oauth2/token";

  var options = {
    code: req.query.code,
    grant_type: 'authorization_code',
    client_id: process.env.SALESFORCE_CLIENT_ID,
    redirect_uri: process.env.SALESFORCE_REDIRECT_URL,
    client_secret: process.env.SALESFORCE_CLIENT_SECRET
  }
  
  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);
    console.dir( sfRes.body )
    req.session.salesforce = JSON.stringify( sfRes.body );
    res.redirect(state.appUrl)
  })
}
function loginCallback(req, res, test) {
  var state = JSON.parse(req.query.state);
  console.dir( state );
  var protocol = "https://";
  if(test === true || req.test) protocol = "http://";  //for testing

  var url = protocol + state.loginServer + "/services/oauth2/token";

  var options = {
    code: req.query.code,
    grant_type: 'authorization_code',
    client_id: process.env.SALESFORCE_CLIENT_ID,
    redirect_uri: process.env.SALESFORCE_REDIRECT_URL,
    client_secret: process.env.SALESFORCE_CLIENT_SECRET
  }
  
  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);
    console.dir( sfRes.body )
    req.session.salesforce = JSON.stringify( sfRes.body );
    res.redirect(state.appUrl)
  })
}
function loginCallback ( req , res , test ) { var state = JSON . parse ( req . query . state ) ; console . dir ( state ) ; var protocol = "https://" ; if ( test === true || req . test ) protocol = "http://" ; //for testing var url = protocol + state . loginServer + "/services/oauth2/token" ; var options = { code : req . query . code , grant_type : 'authorization_code' , client_id : process . env . SALESFORCE_CLIENT_ID , redirect_uri : process . env . SALESFORCE_REDIRECT_URL , client_secret : process . env . SALESFORCE_CLIENT_SECRET } 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 ) ; console . dir ( sfRes . body ) req . session . salesforce = JSON . stringify ( sfRes . body ) ; res . redirect ( state . appUrl ) } ) }
Callback for the Salesforce Server Dance Auth Leg 2, adds the auth token to session
Callback for the Salesforce Server Dance Auth Leg 2 adds the auth token to session