6

Since from Express 4 you're not supposed to do

require('./app/routes.js')(app, passport); // load our routes and pass in our app and fully configured passport

module.exports = function(app, passport) {
    // =====================================
    // FACEBOOK ROUTES =====================
    // =====================================
    // route for facebook authentication and login

    app.get('/auth/facebook', passport.authenticate('facebook', { scope : 'email' }));

    // handle the callback after facebook has authenticated the user
    app.get('/auth/facebook/callback',
        passport.authenticate('facebook', {
            successRedirect : '/profile',
            failureRedirect : '/'
        }));

    // route for logging out
    app.get('/logout', function(req, res) {
        req.logout();
        res.redirect('/');
    });
};

Instead, you're supposed to be using express.Route() function and

var routes = require('./app/routes.js');
app.use('/', routes);

How to pass the configured passport into the routes modules in Express 4?

laggingreflex
  • 32,948
  • 35
  • 141
  • 196
bolerovt
  • 623
  • 7
  • 18

1 Answers1

8

The function export can still be used to pass the passport reference between modules. It would just create and return a Router rather than modifying the app directly.

var express = require('express');

module.exports = function(passport) {
    var router = express.Router();

    router.get('/auth/facebook', passport.authenticate('facebook', { scope : 'email' }));

    // etc.

    return router;
};

And, the app can then use it with:

var routes = require('./app/routes.js')(passport);
app.use('/', routes);
Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199
  • but when I tried the implementation above, I just got a new problem: Cannot GET /auth/facebook/callback?code=************** and there is no error info. How can I deal with this? – bolerovt Dec 07 '14 at 15:17
  • @bolerovt I snipped out some of the routes from my post to keep it short, replaced with `// etc.` Did you copy that line or do you have the `router` with `'/auth/facebook/callback'` and `'/logout'` routes? – Jonathan Lonowski Dec 08 '14 at 15:35
  • I'm afraid yes.... Now I'm getting back to the original Error: read ETIMEDOUT... I have absolute no clue what's going on there... sorry man – bolerovt Dec 09 '14 at 03:22