【发布时间】:2016-01-08 11:25:51
【问题描述】:
认为我正在尝试做的事情应该相对容易,但我正在失去线索,并且可能会失去这样做的意愿。
使用 node 和 express 4 设置节点应用程序。我使用护照进行身份验证。跟随 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
如果我只是返回路由器(跳过将其包装在函数中),我最终会得到一个
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);
【问题讨论】:
-
我没有使用过护照,因此无法提供任何帮助,但是您会收到错误消息,因为您没有返回路由器:module.exports=function(app, passport) {返回路由器; }。但根据您的问题,您不需要将应用程序传递给函数
-
现在我觉得自己像个傻瓜。我想你解决了它,忙于考虑护照对象,忘记了返回,这似乎工作得非常完美。
-
可爱,那么我会添加一个完整的答案:)
-
抱歉,提早了,这让节点很高兴,但它不允许路由访问对象。已添加编辑
-
我已经编辑了我的答案...
标签: node.js express passport.js