Currently I am trying to use the middleware "passport" in an object oriented Node.js server. After restarting the server everything works fine. I can access the route where no authentication is required. But when I am trying to access a route with authentication I always get the response 401 (No-Authentication). This seems to be ok but unfortunately I do not enter the function(username, password, done) in the LocalStrategy.
So I think my problem is in the way I try to use a middleware in the oo javascript style. The template I used to start is from the RedHat OpenShift Cloud and can be seen in: https://github.com/openshift/nodejs-custom-version-openshift/blob/master/server.js
Here is my server initialization method where I tried to use the passport middleware:
self.initializeServer = function() {
self.createGetRoutes();
self.createPostRoutes();
self.app = express.create();
passport.use(new LocalStrategy(
function(username, password, done) {
console.log("TEST");
process.nextTick(function () {
console.log('Here I am!');
return done(null, user);
});
}
));
self.app.configure(function() {
self.app.use(passport.initialize());
});
// Paths without authentication
self.app.get('/holy', function(req, res) {res.send('SHIT! \n')});
// Add GET handlers for the app with authentication (from the getRoutes).
for (var g in self.getRoutes) {
self.app.get(g, passport.authenticate('local', { session: false }), self.getRoutes[g]);
}
// Add POST handlers for the app with authentication (from the postRoutes).
for (var p in self.postRoutes) {
self.app.post(p, passport.authenticate('local', { session: false }), self.postRoutes[p]);
}
};
Your problem here is that you don't use the right strategy. You should use the BasicStrategy to use the http authentication: Passport-HTTP
It looks to me that you are not using the express.router() middleware.