user2517182
user2517182

Reputation: 1299

SANE stack and Passportjs

I am using the sane stack and I would like to add passportjs to it.

I successfully implemented passportjs in a sails application using the instructions here. I am trying to use those instruction to add passportjs to a sane application. I am also considering trying to have sails serve the login, logout, ect... pages, since the those instructions use sails' views. This might not be the best decision going forward, but right now I just want something that works.

I have been getting the following error when I go to login.

error: Sending 500 ("Server Error") response:
server   |  TypeError: undefined is not a function
server   |     at Object.AuthController.login (/Users/someUser/Documents/sanestack/project/server/api/controllers/AuthController.js:50:9)
server   |     at bound (/usr/local/lib/node_modules/sails/node_modules/lodash/dist/lodash.js:729:21)
server   |     at routeTargetFnWrapper (/usr/local/lib/node_modules/sails/lib/router/bind.js:179:5)
server   |     at callbacks (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:164:37)
server   |     at param (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:138:11)
server   |     at pass (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:145:5)
server   |     at nextRoute (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:100:7)
server   |     at callbacks (/usr/local/lib/node_modules/sails/node_modules/express/lib/router/index.js:167:11)
server   |     at /usr/local/lib/node_modules/sails/lib/router/bind.js:187:7
server   |     at /Users/someUser/Documents/sanestack/project/server/api/policies/passport.js:32:7
server   |     at SessionStrategy.strategy.pass (/Users/someUser/Documents/sanestack/project/server/node_modules/passport/lib/middleware/authenticate.js:318:9)
server   |     at SessionStrategy.authenticate (/Users/someUser/Documents/sanestack/project/server/node_modules/passport/lib/strategies/session.js:67:10)
server   |     at attempt (/Users/someUser/Documents/sanestack/project/server/node_modules/passport/lib/middleware/authenticate.js:341:16)
server   |     at authenticate (/Users/someUser/Documents/sanestack/project/server/node_modules/passport/lib/middleware/authenticate.js:342:7)
server   |     at /Users/someUser/Documents/sanestack/project/server/api/policies/passport.js:28:23
server   |     at initialize (/Users/someUser/Documents/sanestack/project/server/node_modules/passport/lib/middleware/initialize.js:62:5) [TypeError: undefined is not a function]

The problem is with following code in server/api/controllers/AuthController.js

 33   login: function (req, res) {
 34     var strategies = sails.config.passport
 35       , providers  = {};
 36
 37     // Get a list of available providers for use in your templates.
 38     Object.keys(strategies).forEach(function (key) {
 39       if (key === 'local') {
 40         return;
 41       }
 42
 43       providers[key] = {
 44         name: strategies[key].name
 45       , slug: key
 46       };
 47     });
 48
 49     // Render the `auth/login.ext` view
 50     res.view({
 51       providers : providers
 52     , errors    : req.flash('error')
 53     });
 54   },

And also the following code in /server/api/policies/passport.js

 24 module.exports = function (req, res, next) {
 25   // Initialize Passport
 26   passport.initialize()(req, res, function () {
 27     // Use the built-in sessions
 28     passport.session()(req, res, function () {
 29       // Make the user available throughout the frontend
 30       res.locals.user = req.user;
 31
 32       next();
 33     });
 34   });
 35 };

Any help would greatly be appreciated.

Upvotes: 2

Views: 306

Answers (1)

mobetta
mobetta

Reputation: 417

I believe you are running into this problem, because you are trying to use Sails server views and the Sane stack expects Ember to handle all the views... If you look at https://github.com/mgenev/how-to-sane you will see the client/server auth interaction there and how the 2 play along, it's not passport based and it has some work pending, but you can get the general picture

Upvotes: 3

Related Questions