Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass the configured passport object into the routes modules in Express4?

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?

like image 839
bolerovt Avatar asked Dec 07 '14 07:12

bolerovt


Video Answer


1 Answers

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);
like image 150
Jonathan Lonowski Avatar answered Sep 21 '22 01:09

Jonathan Lonowski