在路线

时间:2016-01-08 11:25:51

标签: node.js express passport.js

认为我想要做的事情应该是相对容易,但我正在失去线程,并且可能会这样做。

使用节点和快递设置节点应用程序4.我使用护照进行身份验证。跟随scott.io的一个绝对惊人的指南,它很好地完成了这个技巧https://scotch.io/tutorials/easy-node-authentication-setup-and-local

它有魅力。但是,我想分开我的路线,因为我喜欢保持整洁(这是谎言,但我打算保持谎言生活)。

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

现在我的问题是,我需要让护照可用于app(或者获取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

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

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

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

所以这是我的问题。 如果我现在在认证路线上发帖(下面的代码)

//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);

1 个答案:

答案 0 :(得分:3)

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

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

编辑:

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

var passport = require('passport')

可以"只是工作"或

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;
}