I am developing a web application with node.js and I am using passport(http://passportjs.org/) for authentication.
I put all the passport code into the app.js file and it works pretty good...
During some refactoring steps I decided to put all the passport code into an own file/module called authentication.js and here is my problem:
In app.js there is the following "middleware" code:
app.configure(function(){
...
app.use(passport.initialize());
app.use(passport.session());
}
which I would like to replace by something like that:
var auth = require('./authentication.js');
app.configure(function(){
...
app.use(auth.initialize(app));
}
But actually I have no idea how the auth.initialize should look like.
I tried something like that:
function initialize(app){
return function(res,req,next){
app.use(passport.initialize(res,req, next));
app.use(passport.session(res,req, next));
next();
}
but without success... :-(
Does anyone have any idea on this issue?
Thanks in advance...
How about this?
// app.js
var auth = require('./authentication.js');
app.configure(function(){
...
auth.initialize(app);
});
// authentication.js
module.exports.initialize = function(app) {
app.use(passport.initialize());
app.use(passport.session());
};
Do make sure that other middleware that Passport depends on, like express.session and express.cookieParser, is configured before Passport is.