krakenjs / kraken-example-with-passport

An example integrating kraken with passport authentication
53 stars 33 forks source link

Receiving Object <IncomingMessage> has no method 'flash' #10

Closed joshbedo closed 9 years ago

joshbedo commented 9 years ago

I was just going through the repo and recreating the setup for passport-local on one of my projects and when i go to my /login route i'm getting the message below. Inside of the cli i also get undefined for req.flash. Wondering if there is anything i'm missing. I did the following

  1. Added connect-flash to the config.json
  2. Installed the npm package connect-flash
  3. Setup all the lib files exactly the same
  4. Included the LocalStrategy within my login controller

controllers/login/index.js

'use strict';

var LoginModel = require('../../models/login');
var passport = require('passport');

module.exports = function(router) {
  var model = new LoginModel();

  router.get('/', function(req, res) {
    console.log(req.flash); // undefined
    model.messages = req.flash('error');
    res.render('login', model);
  });

  router.post('/', function(req, res) {
    console.log(req, res);

    passport.authenticate('local', {
      successRedirect: req.session.goingTo || '/',
      failureRedirect: '/login',
      failureFlash: true
    })(req, res);  // seems to be erroring here.
  });

};

At this point i've went through most of the files and can't seem to trace why the req object wouldn't include flash. Let me know if you need to see anything else specifically, Thanks!

aredridel commented 9 years ago

What priority did you give it in config.json?

joshbedo commented 9 years ago

So i just solved this by changing spec.js to include flash like this. Not sure if this is needed? but it works.

// Used for serializing and deserializing passport users
'use strict';

var express = require('express');
var passport = require('passport');
var flash = require('connect-flash');
var auth = require('../lib/auth');
var userLib = require('./user')();
var db = require('../lib/database');
var crypto = require('../lib/crypto');

module.exports = function spec(app) {
  app.on('middleware:after:session', function configPassport(eventargs) {
    // Tell passport to use our newly created local strategy for authentication
    passport.use(auth.localStrategy());
    // Give passport a way to serialize and deserialize a user. In this case by the user's id.
    passport.serializeUser(userLib.serialize);
    passport.deserializeUser(userLib.deserialize);
    app.use(passport.initialize());
    app.use(passport.session());
    app.use(flash());
  });

  return {
    onconfig: function(config, next) {
      var dbConfig = config.get('databaseConfig')
      var cryptConfig = config.get('bcrypt');

      crypto.setCryptLevel(cryptConfig.difficulty);
      db.config(dbConfig);
      userLib.addUsers();
      next(null, config);
    }
  };
};

Also here is my config.json

{

    "express": {
      "view engine": "jsx",
      "view": "require:react-engine/lib/expressView",
      "views": "path:./public/js/views"
    }

    ,"view engines": {
      "jsx": {
        "module": "react-engine/lib/server",
        "renderer": {
          "method": "create"
        }
      }
    }

    ,"databaseConfig": {
      "host": "localhost",
      "database": "imstillreallybored"
    }

    ,"bcrypt": {
      "difficulty":8
    }

    // flash messaging middleware
    ,"flash": {
      "priority": 91,
      "enabled": true,
      "module": {
        "name": "connect-flash",
        "method": "flash"
      }
    }

    // test is user session required and if user has session
    ,"isAuthenticated": {
      "enabled": true,
      "priority": 111,
      "module": {
        "name":"path:./lib/auth",
        "method": "isAuthenticated"
      }
    }

    // inject user into the response object
    ,"injectUser": {
      "enabled": true,
      "priority": 112,
      "module": {
        "name":"path:./lib/auth",
        "method":"injectUser"
      }
    }

    ,"middleware": {

        "static": {
            "module": {
                "arguments": [ "path:./.build" ]
            }
        }

        ,"router": {
            "module": {
                "arguments": [{ "directory": "path:./controllers" }]
            }
        }

    }
}
aredridel commented 9 years ago

You'd have to put the flash in the middleware configuration rather than the root of the config.

joshbedo commented 9 years ago

Within spec.js? or which file?

aredridel commented 9 years ago

Within config.json.

Generally, kraken apps move as much middleware configuration as possible in there.

joshbedo commented 9 years ago

I do have flash set within the config.json though?

    // flash messaging middleware
    ,"flash": {
      "priority": 91,
      "enabled": true,
      "module": {
        "name": "connect-flash",
        "method": "flash"
      }
    }
aredridel commented 9 years ago

Yeah, outside the middleware configuration section. It won't do anything where you have it.

joshbedo commented 9 years ago

do'h i feel really dumb now, Thanks for the help!

aredridel commented 9 years ago

Sure thing! It takes a bit to get used to the json configs!