1

I am trying to do salseforce aouth in nodejs using passportjs butt getting error.

Cannot GET /callback?code=aPrxaSyVmC8fBbcSNDQ8G8UtoR.YZip2hdfAGpMSc2hf0798gD_UoDle1dqqC0HnPyewycgKMw%3D%3D

Here is my code

'use strict';
var express = require('express'),
  passport = require('passport'),
  util = require('util'),
  ForceDotComStrategy = require('passport-forcedotcom').Strategy;


//----------------------------------------------------------------------------
// REPLACE THE BELOW SETTING TO MATCH YOUR SALESFORCE CONNECTED-APP'S SETTINGS
//----------------------------------------------------------------------------
var CF_CLIENT_ID = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
var CF_CLIENT_SECRET = 'xxxxxxxxxxxxxxxxxxxxx';
var CF_CALLBACK_URL = 'http://localhost:3000/callback';
var SF_AUTHORIZE_URL = 'https://login.salesforce.com/services/oauth2/authorize';
var SF_TOKEN_URL = 'https://login.salesforce.com/services/oauth2/token';

passport.serializeUser(function(user, done) {
  done(null, user);
});

passport.deserializeUser(function(obj, done) {
  done(null, obj);
});

var sfStrategy = new ForceDotComStrategy({
  clientID: CF_CLIENT_ID,
  clientSecret: CF_CLIENT_SECRET,
  callbackURL: CF_CALLBACK_URL,
  authorizationURL: SF_AUTHORIZE_URL,
  tokenURL: SF_TOKEN_URL
}, function(accessToken, refreshToken, profile, done) {
  process.nextTick(function() {
    delete profile._raw;
    return done(null, profile);
  });
});

passport.use(sfStrategy);


var app = express();

// configure Express
app.configure(function() {
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.logger());
  app.use(express.cookieParser());
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(express.session({
    secret: 'keyboard cat'
  }));
  // Initialize Passport!  Also use passport.session() middleware, to support
  // persistent login sessions (recommended).
  app.use(passport.initialize());
  app.use(passport.session());
  app.use(app.router);
  app.use(express.static(__dirname + '/public'));
});


app.get('/', function(req, res) {
  console.log(req.user);
  if(!req.user) {
    req.session.destroy();
    req.logout();
    return res.redirect('/login');
  }
  res.render('index', {
    user: req.user
  });
});


app.get('/login', function(req, res) {
  req.logout();
  req.session.destroy();

  res.render('login', {
    user: req.user
  });
});
app.get('/auth/forcedotcom', passport.authenticate('forcedotcom'), function(req, res) {
});
app.get('/auth/forcedotcom/callback', passport.authenticate('forcedotcom', {
  successRedirect: '/',
  failureRedirect: '/login'
}), function(req, res) {
  res.redirect('/');
});

app.get('/logout', function(req, res) {
  res.redirect('/login');
});

app.listen(3000);
console.log('localhost run in 3000');

function ensureAuthenticated(req, res, next) {
  if(req.isAuthenticated()) {
    return next();
  }
  res.redirect('/login');
}

I am trying to fix problem with already post a query in at there but that cannot resole Cannot GET /auth/twitter/callback while using twitter oauth in nodejs

Community
  • 1
  • 1
Mohammed Javed
  • 866
  • 2
  • 9
  • 24
  • I'm personally using [Grant](https://github.com/simov/grant) for such cases and it works flawlessly so far. It supports authentication with SalesForce as well. – simo Jul 17 '15 at 13:58

1 Answers1

0

looks like you've set the callback URL to http://yourdomain.com/callback in your salesforce oauth app, but have not define a handler for it correctly

either change the callback URL of your salesforce oauth app to

http://yourdomain.com/auth/forcedotcom/callback

or change the following in your code

app.get('/auth/forcedotcom/callback', passport.authenticate('...

to

app.get('/callback', passport.authenticate('forcedotcom', {
  successRedirect: '/',
  failureRedirect: '/login'
}), function(req, res) {
  res.redirect('/');
});
mynawaz
  • 1,599
  • 1
  • 9
  • 16