Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sails hook passport

I'm trying to implement Passport strategies into a sails hook, like this I can share on multiple project.

When I try to log I have this error :

Error: passport.initialize() middleware not in use
    at IncomingMessage.req.login.req.logIn (/Users/jaumard/IdeaProjects/HookPassportTest/node_modules/sails-hook-passport/node_modules/passport-github/node_modules/passport-oauth/node_modules/passport/lib/passport/http/request.js:30:30)
    at /Users/jaumard/IdeaProjects/HookPassportTest/node_modules/sails-hook-passport/api/controllers/AuthController.js:163:11
    at Strategy.strategy.success (/Users/jaumard/IdeaProjects/HookPassportTest/node_modules/sails-hook-passport/node_modules/passport/lib/middleware/authenticate.js:194:18)
    at verified (/Users/jaumard/IdeaProjects/HookPassportTest/node_modules/sails-hook-passport/node_modules/passport-twitter/node_modules/passport-oauth1/lib/strategy.js:169:16)
    at returnResults (/Users/jaumard/IdeaProjects/HookPassportTest/node_modules/sails/node_modules/waterline/lib/waterline/query/finders/basic.js:168:9)
    at /Users/jaumard/IdeaProjects/HookPassportTest/node_modules/sails/node_modules/waterline/lib/waterline/query/finders/basic.js:74:16
    at /Users/jaumard/IdeaProjects/HookPassportTest/node_modules/sails/node_modules/waterline/lib/waterline/query/finders/operations.js:82:7
    at Object.async.each (/Users/jaumard/IdeaProjects/HookPassportTest/node_modules/sails/node_modules/async/lib/async.js:121:20)
    at /Users/jaumard/IdeaProjects/HookPassportTest/node_modules/sails/node_modules/waterline/lib/waterline/query/finders/operations.js:425:11
    at /Users/jaumard/IdeaProjects/HookPassportTest/node_modules/sails/node_modules/waterline/lib/waterline/query/finders/operations.js:564:5
    at Object.async.each (/Users/jaumard/IdeaProjects/HookPassportTest/node_modules/sails/node_modules/async/lib/async.js:121:20)
    at _buildChildOpts (/Users/jaumard/IdeaProjects/HookPassportTest/node_modules/sails/node_modules/waterline/lib/waterline/query/finders/operations.js:453:9)
    at _execChildOpts (/Users/jaumard/IdeaProjects/HookPassportTest/node_modules/sails/node_modules/waterline/lib/waterline/query/finders/operations.js:421:8)
    at /Users/jaumard/IdeaProjects/HookPassportTest/node_modules/sails/node_modules/waterline/lib/waterline/query/finders/operations.js:80:10
    at bound (/Users/jaumard/IdeaProjects/HookPassportTest/node_modules/sails/node_modules/lodash/dist/lodash.js:957:21)
    at applyInOriginalCtx (/Users/jaumard/IdeaProjects/HookPassportTest/node_modules/sails/node_modules/waterline/lib/waterline/utils/normalize.js:421:80)

I read that I have to add some middleware in config/http.js

middleware: {

    passportInit    : require('passport').initialize(),
    passportSession : require('passport').session(),

    order: [
      'startRequestTimer',
      'cookieParser',
      'session',
      'passportInit',
      'passportSession',
      'myRequestLogger',
      'bodyParser',
      'handleBodyParserError',
      'compress',
      'methodOverride',
      'poweredBy',
      'router',
      'www',
      'favicon',
      '404',
      '500'
    ]
  }

All it's working after this but I'm under an installable hook and don't want to manually change http.js, is there a way to modify this from the hook ? Or fix the error without adding this.

like image 657
jaumard Avatar asked May 12 '15 19:05

jaumard


1 Answers

If I understand correctly, you need to init passport.js in your middleware but want to avoid having to manually edit http.js in each of your projects. To do that you'd create an installable hook like so:

module.exports = function passware(sails) {

sails.config.http.middleware = {

    passportInit    : require('passport').initialize(),
    passportSession : require('passport').session(),

    order: [
      'startRequestTimer',
      'cookieParser',
      'session',
      'passportInit',
      'passportSession',
      'myRequestLogger',
      'bodyParser',
      'handleBodyParserError',
      'compress',
      'methodOverride',
      'poweredBy',
      'router',
      'www',
      'favicon',
      '404',
      '500'
    ]
  }

  return {};
}

To use it you simply copy the hook into your node_modules or npm publish and npm install it in your projects.

like image 189
Alex Alksne Avatar answered Nov 13 '22 11:11

Alex Alksne