I'm using Passport for authentication in conjunction with node-orm2. Passport requires you to register functions for serializing and deserializing users to and from the session. My users are stored in a database which I'm accessing through node-orm2's Express middleware. The orm2 middleware tags its models onto the req object for easy access. Unfortunately, Passport does not provide access to the req object in deserializeUser
. I've come across this solution, but am hoping for something better:
var User;
passport.use({ passReqToCallback: true }, new LocalStrategy(function (request, username, password, done) {
if (!User) {
User = request.models.User;
}
User
.find({ username: username })
.limit(1)
.run(function (err, users) {
var user = users[0];
if (err) {
done(err);
} else if (!hasher.verify(password, user.password)) {
done(null, false);
} else {
done(null, user);
}
});
}));
passport.deserializeUser(function (id, done) {
User.get(id, done);
});
req will be passed to passport's serialize and deserialize callbacks in the upcoming release, 0.2.0: https://github.com/jaredhanson/passport/pull/160