在路线之间传递物品(护照)

passing objects (passport) between routes

认为我正在尝试做的事情应该相对容易,但我正在失去线索,并且可能会失去这样做的意愿。

使用 node 和 express 4 设置节点应用程序。我使用 passport 进行身份验证。遵循了 scott.io 的绝妙指南,该指南非常出色 https://scotch.io/tutorials/easy-node-authentication-setup-and-local

它很有魅力。但是,我想分开我的路线,因为我喜欢保持整洁(那是谎言,但我打算让谎言继续存在)。

我的计划是有四组路线。 api(映射到 /api,使用文件 ./routes/api.js) 索引(映射到 /,使用文件 ./routes/index.js) auth(映射到 /auth,跟踪所有身份验证、回调以及一些激活器和其他位)

现在我的问题是,我需要让应用程序可以使用通行证(或者获取 api.js 和 indes.js 以便能够调用 passport.js 中的函数)并且我可以'不太清楚如何。

我的计划是像这样启动护照:

var passport = require('passport');
app.use(session({secret: 'Not-telling-you)',
    saveUninitialized: true,
    resave: true
})); // session secret
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session
//Configuring the passports
require('./config/passport')(passport);

这应该可以让我在应用程序中使用护照

下一步加载路由模块

var auth = require('./routes/auth')(app, passport);
var users = require('./routes/users')(app,passport);
var activator = require('./routes/activator')(app,passport);

这应该允许我在模块中访问它们吗?

映射应用中的所有商品

app.use('/api', api);
app.use('/auth', auth);
app.use('/', index);

然后编写模块如下(这是一个超级简单的auth版本)

var bodyParser = require('body-parser');
var activator = require('activator');
var express = require('express');
var router = express.Router();


//Lets read the configuration files we need
var activatorCfg = require('../config/activator.js')
var cfgWebPage = require('../config/webpage.js');

//So we can read the headers easily
router.use(bodyParser.json()); // support json encoded bodies
router.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies

//Activating activator, so we can actively activate the actives
activator.init({user: activatorCfg, transport: activatorCfg.smtpUrl , from: activatorCfg.fromEmail, templates: activatorCfg.templatesDir});


router.get('/login', function(req, res) {
    res.render('login.ejs', { title: 'Betchanow - Social betting as it should be' , loginUrl: cfgWebPage.loginUrl, trackingID: cfgWebPage.googleTracking.trackingID, message: req.flash('loginMessage') });
});


module.exports=function(app, passport) {
    router
}

我的问题是,如果我这样做,快递会投诉

      throw new TypeError('Router.use() requires middleware function but got a
            ^
TypeError: Router.use() requires middleware function but got a undefined

如果我只是 return 路由器(跳过将其包装在一个函数中),我最终会得到一个
var search = 1 + req.url.indexOf('?'); ^ 类型错误:无法读取未定义的 属性 'indexOf'

那么,是否有一种正确、简单或最好是正确且简单的方法来实现这一目标? 认为诀窍是通过应用程序和护照(或仅通过护照),认为我需要访问所有三个护照中的数据或功能,并且因为我也计划使用 ACL,所以想将其添加到 auth让我的生活也变得简单。

============== 编辑 =============

这是我的问题。 如果我现在对身份验证路由执行 post(下面的代码)

//Lets load the modules, note the missing passport
var bodyParser = require('body-parser');
var activator = require('activator');
var express = require('express');
var router = express.Router();


//Lets read the configuration files we need
var activatorCfg = require('../config/activator.js')
var cfgWebPage = require('../config/webpage.js');

//So we can read the headers easily
router.use(bodyParser.json()); // support json encoded bodies
router.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies

//Activating activator, so we can actively activate the actives
activator.init({user: activatorCfg, transport: activatorCfg.smtpUrl , from: activatorCfg.fromEmail, templates: activatorCfg.templatesDir});

//Lets start with our routes
// process the login form
router.post('/login', passport.authenticate('local-login', {
    successRedirect : '/', // redirect to the secure profile section
    failureRedirect : '/login', // redirect back to the signup page if there is an error
    failureFlash : true // allow flash messages
}));

module.exports=function(app, passport) {
    return router;
}

我最终遇到了路由代码 (./routes/auth.js) 不知道护照是什么的问题。 (在应用程序中加载如下):

app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session
//Configuring the passports
require('./config/passport')(passport);

您将收到错误消息,因为您没有退回路由器。

module.exports=function(app, passport) {
    return router;
}

编辑:

您将无法访问护照 属性,因为您没有将其传递或放置在任何地方。由于我不确定 passport 是如何工作的(它是否作为一个单例),所以你的路由文件中有几个选项:

var passport = require('passport')

可能"just work",或

var passport; // at the top of your routes file

// your routes

module.exports = function(app, _passport) {
    passport = _passport;
    return router;
}

第三种选择是将整个路由包装在 exports 方法中:

// your requires here

module.exports = function(app, passport) {
    //So we can read the headers easily
    router.use(bodyParser.json()); // support json encoded bodies
    router.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies

    //Activating activator, so we can actively activate the actives
    activator.init({user: activatorCfg, transport: activatorCfg.smtpUrl , from: activatorCfg.fromEmail, templates: activatorCfg.templatesDir});

    //Lets start with our routes
    // process the login form
    router.post('/login', passport.authenticate('local-login', {
        successRedirect : '/', // redirect to the secure profile section
        failureRedirect : '/login', // redirect back to the signup page if there is an error
        failureFlash : true // allow flash messages
    }));
    return router;
}