6

I am integrating passport with sails.

While google and facebook is working fine in my application I struggle with twitter authentication! When clicking on the 'login with twitter' button there is an error thrown wich says: Error: OAuthStrategy requires session support. Did you forget app.use(express.session(...))?

I read here that sessions are necessary for twitter authentication to work. I made sure my app has sessions activated!

I testet passport-twitter with a simple express app (without sails) to make sure the module is working and my twitter credentials are intact.

I am assuming sails sessions are different to express sessions? Is sails changing the way sessions work? Any advice on how to solve this?


EDIT: Added some more info as requested in the comments:

Sails Version: 0.9.13

UserController.js:

...
twitter: function(res, req) {
    passport.authenticate('twitter', {failureRedirect: '/login'}, function(err, user, info) {
      return console.log(err, user, info);
    })(req, res);
  }
...

config/passport.js:

...
passport.use(new TwitterStrategy({
    consumerKey: '**************',
    consumerSecret: '********************',
    callbackURL: "http://127.0.0.1:1337/auth/twitter/callback"
  },
  function(token, tokenSecret, profile, done){
    process.nextTick(function() {
      console.log(profile);
    });
  }
));
...
Travis Webb
  • 14,688
  • 7
  • 55
  • 109
Sven
  • 6,288
  • 24
  • 74
  • 116
  • Where do you call `authenticate` from? – bredikhin Mar 24 '14 at 13:54
  • From the UserController – Sven Mar 24 '14 at 14:12
  • Could you publish a snippet? Also, which Sails version? – bredikhin Mar 24 '14 at 14:14
  • added more info.. thanks for help – Sven Mar 24 '14 at 14:23
  • Hmm, all looks good, but I have a feeling that it has something to do with the Express middleware loading order. Can you show the middleware config? – bredikhin Mar 24 '14 at 15:55
  • That sounds plausible! I am not sure how to influence the order of config files being loaded with sails! All I did was putting passport.js (where I also configured passport.session and passport.initialize) into the /config folder. Sails loads it with all the other config files. – Sven Mar 25 '14 at 10:17
  • (1) Are you serializing and deserializing the user in passport.js? Someplace else? If not at all...that's your problem. (2) Did you edit config/session.js at all to use a different session store, like redis? If so, can you post your changes? – Matthew Bakaitis Mar 25 '14 at 11:42
  • @Sven You should have something like `app.use(passport.initialize());` and `app.use(passport.session());` where `app` is, basically, Express. Could you show this part of your `config/passport.js`? – bredikhin Mar 25 '14 at 14:01
  • @bredikhin: sails is taking care of that express stuff – Sven Mar 25 '14 at 17:05
  • @Matt Bakaitis: I am serializing and deserializing in passport.js! I didn't edit session.js! Might that be the problem? Do I need some session store other than memory? – Sven Mar 25 '14 at 17:09
  • You can use the memory session store as you develop and should be fine. I guess I'd ask: are sessions working at all? How did you confirm they work? Are you using another passport strategy that's doing fine while the twitter strategy is failing? – Matthew Bakaitis Mar 26 '14 at 17:11
  • @MattBakaitis Facebook, Google and local strategies are working fine! – Sven Mar 26 '14 at 18:04
  • @AJcodez: I didn't figure it out. Sry :-/ – Sven Jul 04 '14 at 09:30

3 Answers3

2

Did you try sails-generate-auth with sails 0.10? It makes life easier from my point of view: https://www.npmjs.org/package/sails-generate-auth

Diego Pamio
  • 1,377
  • 13
  • 21
0

check out one example how to do Twitter auth (google, facebook, github are there as well): https://github.com/stefanbuck/sails-social-auth-example

ronky
  • 1
0

I recommend that you don't spend time integrating this yourself. There are a number of existing solutions. The official documentation lists several options:

  • sails-auth: Passport-based Authentication Extension, including Basic Auth
  • sails-permissions: Permissions and Entitlements system for sails.js: supports user authentication with passport.js, role-based permissioning, object ownership, and row-level security.
  • sails-generate-auth: Generate a Passport.js authentication layer for your Sails app
Travis Webb
  • 14,688
  • 7
  • 55
  • 109