Wednesday, March 12, 2014

Error: Failed to find request token in session at Strategy.OAuthStrategy.authenticate [passport-twitter on cluster]

I got the error in the title when I tried to deploy my code on Heroku or Dokku. Possible suspects were:

1) Twitter API KEY and SECRET
2) System Time

And they were correct. Then I found that if we run the module as cluster:

Authentication request was sent by passport-twitter to twitter and some data regarding this request kept in session object. Then the response came from twitter in form of (callback?oauth_token=...&oauth_verifier=...) but could not find session object since it was in another process. By the way, heroku can run 2 processes per web dyno.

So solution was :

not using cluster code and running the http server on a single process. 

Another solution: Use Redis for session storing:

RedisStore = require('connect-redis')(express.session);
var REDIS_URL = process.env.REDISCLOUD_URL || "redis://localhost";
app.use(express.session({ store: new RedisStore({'url': REDIS_URL}), secret: '2342342' })); // session secret

No comments: