How to transfer a configured passport object to route modules in Express4?

Since from Express 4 you should not 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 should use the express.Route() function and

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

How to transfer a configured passport to route modules in Express 4?

+6
source share
1 answer

The function export can still be used to pass the passport link between modules. It would simply create and return a Router , and would not directly change the app .

 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 with:

 var routes = require('./app/routes.js')(passport); app.use('/', routes); 
+7
source

Source: https://habr.com/ru/post/979238/


All Articles