compound-passport issue

902 views
Skip to first unread message

Ark R

unread,
Jan 31, 2013, 7:37:57 PM1/31/13
to compo...@googlegroups.com
I'm new to node and so far I'm digging on compound. However, I'm trying to integrate a local strategy using compound-passport and I must be missing something. Any ideas?

environment.js
=============================
module.exports = function (compound) {

    var passport = require('passport'), 
    LocalStrategy = require('passport-local').Strategy;
    passport.use(new LocalStrategy(
      function(username, password, done) {
        Member = compound.models.Member;
        Member.findOne({ username: username }, function(err, member) {
          if (err) { return done(err); }
          if (!member) {
            return done(null, false, { message: 'Incorrect username.' });
          }
          if (!member.validPassword(password)) {
            return done(null, false, { message: 'Incorrect password.' });
          }
          return done(null, member);
        });
      }
    ));

    var express = require('express');
    var app = compound.app;

    app.configure(function(){
        app.use(compound.assetsCompiler.init());
        app.use(express.static(app.root + '/public', { maxAge: 86400000 }));
        app.set('jsDirectory', '/javascripts/');
        app.set('cssDirectory', '/stylesheets/');
        app.set('cssEngine', 'stylus');
        // make sure you run `npm install browserify uglify-js`
        // app.enable('clientside');
        app.use(express.bodyParser());
        app.use(express.cookieParser('secret'));
        app.use(express.session({secret: 'secret'}));
        app.use(express.methodOverride());

        app.post('/login',
          passport.authenticate('local', { successRedirect: '/app',
                                           failureRedirect: '/',
                                           failureFlash: true })
        );
        app.use(app.router);
    });
};


passport.yml
=============================
development:
  local: yes


ERROR I GET
==============================

500 TypeError: Object #<IncomingMessage> has no method 'flash'

  • at allFailed (/Users/Ark/Sites/backlogger/node_modules/passport/lib/passport/middleware/authenticate.js:110:15)
  • at attempt (/Users/Ark/Sites/backlogger/node_modules/passport/lib/passport/middleware/authenticate.js:231:28)
  • at Context.module.exports.delegate.fail (/Users/Ark/Sites/backlogger/node_modules/passport/lib/passport/middleware/authenticate.js:226:9)
  • at Context.actions.fail (/Users/Ark/Sites/backlogger/node_modules/passport/lib/passport/context/http/actions.js:35:22)
  • at Strategy.authenticate (/Users/Ark/Sites/backlogger/node_modules/passport-local/lib/passport-local/strategy.js:75:17)
  • at attempt (/Users/Ark/Sites/backlogger/node_modules/passport/lib/passport/middleware/authenticate.js:243:16)
  • at Passport.authenticate (/Users/Ark/Sites/backlogger/node_modules/passport/lib/passport/middleware/authenticate.js:244:7)
  • at callbacks (/Users/Ark/Sites/backlogger/node_modules/compound/node_modules/express/lib/router/index.js:161:37)
  • at param (/Users/Ark/Sites/backlogger/node_modules/compound/node_modules/express/lib/router/index.js:135:11)
  • at pass (/Users/Ark/Sites/backlogger/node_modules/compound/node_modules/express/lib/router/index.js:142:5)

Any ideas?

Michael Hayes

unread,
Jan 31, 2013, 7:50:16 PM1/31/13
to compo...@googlegroups.com
Here is how I have it working:
enviroment.js:
module.exports = function (compound) {

    var express = require('express')
      , app = compound.app
      , passport = require('passport');

    app.configure(function(){
        app.use(compound.assetsCompiler.init());
        app.use(express.static(app.root + '/public', { maxAge: 86400000 }));
        app.set('view engine', 'ejs');
        app.set('view options', { complexNames: true });
        app.set('jsDirectory', '/javascripts/');
        app.set('cssDirectory', '/stylesheets/');
        app.set('cssEngine', 'stylus');
        app.use(express.bodyParser());
        app.use(express.cookieParser('secret'));
        app.use(express.session({secret: 'secret'}));
        app.use(passport.initialize());
        app.use(passport.session());
        app.use(express.methodOverride());
        app.use(app.router);
    });
};
initializers/passport.js:

var passport = require('passport')
  , LocalStrategy = require('passport-local').Strategy;
module.exports = function(compound) {
    compound.passport = passport;
    
    passport.use(new LocalStrategy({
        usernameField: 'email',
        passwordField: 'password'
    }, function(email, password, done) {
        compound.models.User.findOne({where: { email: email }}, function(err, user) {
          if (err) { return done(err); }
          if (!user) {
            return done(null, false, { message: 'Incorrect username.' });
          }
          if (!user.authenticate(password)) {
            return done(null, false, { message: 'Incorrect password.' });
          }
          return done(null, user);
        });
      }
    ));
    passport.serializeUser(function(user, done) {
      done(null, user.id);
    });

    passport.deserializeUser(function(id, done) {
      compound.models.User.find(id, function(err, user) {
        done(err, user);
      });
    });
}



--
The official community of CompoundJS.
 
website: compoundjs.com
source: https://github.com/1602/compound
bug reports: https://github.com/1602/compound/issues?state=open
---
You received this message because you are subscribed to the Google Groups "CompoundJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to compoundjs+...@googlegroups.com.
To post to this group, send email to compo...@googlegroups.com.
Visit this group at http://groups.google.com/group/compoundjs?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Ark R

unread,
Jan 31, 2013, 8:01:08 PM1/31/13
to compo...@googlegroups.com
I moved my code around to mirror yours. And I'm still getting the same error about the flash. 

Also, where are you including your authenticate method.

Michael Hayes

unread,
Jan 31, 2013, 8:08:50 PM1/31/13
to compo...@googlegroups.com
so in the router I have map.post('login', 'user#signin');
then in the user controller I have 
action('signin', function signin() {
    compound.passport.authenticate('local', function(err, user, info) {
    if (err) { return next(err); }
    if (user) {
        req.logIn(user, function(err) {
            if (err) { return next(err); }
            return redirect(pathTo.organizations());
        });
    } else {
        flash('error', info.message)
        redirect('/');
    }
  })(req, res, next);
});

flash was removed from express in 3.0, and is not a separate connect middleware, it could be that compound implements it differently (it could be on response instead of request for example I really dont know), but calling it in the controller where it is globally available should work.


--

Ark R

unread,
Jan 31, 2013, 8:19:54 PM1/31/13
to compo...@googlegroups.com
That worked perfectly. Thank you very much.

One last question, is this method implemented by your model, because until i commented it out it broke.

if(!member.authenticate(password))

Michael Hayes

unread,
Jan 31, 2013, 8:33:20 PM1/31/13
to compo...@googlegroups.com
yes it is, sorry my code is a bit messy I tried to remove anything that was not relavant, heres most of my user model if you want to take a look:
ar crypto = require('crypto');
module.exports = function (compound, User) {

    User.setter.password = function (pass) {
        this.salt = this.makeSalt();
        this._password = this.encryptPassword(pass);
    };

    User.prototype.authenticate = function(plainText) {
        return this.encryptPassword(plainText) === this.password;
    }

    User.prototype.makeSalt =function(date) {
        return Math.round(((date || new Date()).valueOf() * Math.random())) + '';
    }

    User.prototype.encryptPassword = function(password) {
        return crypto.createHmac('sha1', this.salt).update(password).digest('hex');
    }
    User.beforeCreate = function(next) {
        this.createdAt = new Date();
        next();
    }

    User.validatesPresenceOf('name', 'email', 'password');
    User.validatesLengthOf('password', {min: 5, message: {min: 'Password is too short'}});
    User.validatesUniquenessOf('email', {message: 'email is not unique'});
};
and this is the db schema

var User = describe('User', function () {
    property('name', String);
    property('email', String);
    property('password', String);
    property('salt', String);
    property('createAt', Date);
    property('resetTokin', String);
    property('resetDate', Date);
});

Tyson Cadenhead

unread,
Apr 3, 2013, 5:28:24 PM4/3/13
to compo...@googlegroups.com
I know this is probably a little late, but I just wrote a blog post about how I got Compound-Passport working with the latest release of Compound: http://tysonjs.com/blog/authentication-with-compound-passport

Anatoliy Chakkaev

unread,
Apr 3, 2013, 5:56:14 PM4/3/13
to compo...@googlegroups.com
Thanks Tyson! That's great. And I would really appreciate if you take a look at guides drafts I've started writing recently: http://compoundjs.github.com/guides/ work is in progress and any pull-request are very welcome.


On Thu, Apr 4, 2013 at 1:28 AM, Tyson Cadenhead <tysonca...@gmail.com> wrote:
I know this is probably a little late, but I just wrote a blog post about how I got Compound-Passport working with the latest release of Compound: http://tysonjs.com/blog/authentication-with-compound-passport

--

Matt Hupman

unread,
Apr 5, 2013, 5:37:56 PM4/5/13
to compo...@googlegroups.com
Tyson,

Thanks for the guide! Do you mind sharing what your User model looks like?  I've got PassportJS integrated (I think) but I'm struggling a bit with the User model and it's password handling, confirmation, etc.

Regards,
Matt

Benjamin McGinnis

unread,
May 9, 2013, 9:13:28 PM5/9/13
to compo...@googlegroups.com
Could anyone on this chain help me get the view that's rendering the PassportJS login form to use the application_layout?  

I'm basically doing the approach in Tyson's guide.  I've tried using the layout approach from express < 3.0 but I get 'layout' is not defined errors or no change depending on which approach there.  I've also thought about making a dedicated controller for auth instead of using PassportJS's default, that way it would at least inherit from the application controller and use it's layout, but I didn't want to open up that can of worms without asking for help first.

Here's the code in my environment.coffee which is currently controlling everything, without any of my improvement attempts:

    # Set up compound auth callback
    compoundPassport = require 'compound-passport'
    localStrategy = compoundPassport.strategies.local

    localStrategy.callback = (username, password, done) ->
      app.models.User.all { where: { username: username }}, (err, user) ->
          
          user = user[0]

          if (err)
            return done(err)
          if (!user)
            return done(err, false)
          if (!user.authenticate(password))
            return done(err, false)
            
          return done(err, user)

  # PassportJS Login screen, 'compound-passport' package creates app.post('/login') route.
  # Both of these are outside of CompoundJS controllers so they don't get caught by
  # Controller.before filter and cause an infinite loop
  app.get '/login', (req, res) ->
    res.render 'auth/login.ejs', { user: req.user, message: req.flash 'error' }

Maxime Picaud

unread,
Dec 2, 2013, 4:42:34 PM12/2/13
to compo...@googlegroups.com
I had the same problem with the existence of flash. As recommended on Passport website (http://passportjs.org/guide/authenticate/) I installed 'connect-flash' by following their instruction and all works fine without done nothing more than installing compound-passport, create a User model and create a signin form :)
Reply all
Reply to author
Forward
0 new messages