node.js - 如何将配置的 Passport 对象传递到 Express4 中的路由模块?

标签 node.js passport.js express-4

从 Express 4 开始你不应该做

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('/');
    });
};

相反,您应该使用 express.Route() 函数和

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

如何将配置的通行证传递到 Express 4 中的路由模块?

最佳答案

function 导出仍可用于在模块之间传递 passport 引用。它只会创建并返回一个Router,而不是直接修改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;
};

然后,应用 可以使用它:

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

关于node.js - 如何将配置的 Passport 对象传递到 Express4 中的路由模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27340538/

相关文章:

node.js - 文件上传卡在生产代码上,但在本地工作 - multer,node.js

node.js - 如何查看 socket.html 的输出?

node.js - 在使用带sequelize 的 Passport 时,isAuthenticated 始终为 false

node.js - 如何在mocha的帮助下测试使用nodejs和passport编写的社交登录应用程序

node.js - Nodejs+express 应用程序 API 的正确模块化结构

javascript - Express4错误中间件序列

javascript - 如何使用套接字将按钮的背景颜色数据发送到服务器

node.js - 如何判断项目/应用程序需要哪个版本的 Node

javascript - NodeJS readFile() 检索文件名

node.js - 带 Passport 的 NodeJS 快速验证器