我正在编写一个身份验证中间件,它应该在成功登录我们的注销后重定向到以前访问过的页面。这是完整的配置块:
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.static(__dirname + '/public'));
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.session({ secret: '__YouDontKnow__', store: new RedisStore }));
app.use(authCheck.run);
app.use(express.methodOverride());
app.use(app.router);
});
现在在app.use(authCheck.run)
下时app.use(express.session)
,Express 会抛出错误:错误:发送后无法设置标题。
如果我将这个调用移到上面app.use(express.session)
,它会抛出一个 500 错误,说明会话未定义。我的身份验证功能取决于会话对象。
我能做些什么?
编辑:这是 authCheck 函数:
exports.run = function (req, res, next) {
var urlparser = GLOBAL.urlparser,
url = req.urlp = urlparser.parse(req.url, true),
goToLastPage = function() {
var redirectUrl = (req.session.lastVisitedPage) ? req.session.lastVisitedPage : '/';
console.log("goToLastPage(%s)", redirectUrl);
res.redirect(redirectUrl);
return;
}
// Log out
if (url.pathname == '/user/logout') {
req.session.destroy();
console.log('User has logged out');
goToLastPage();
}
// Log in
if (url.pathname == '/user/login' && req.method == 'POST') {
var email = req.body.login.email,
password = req.body.login.password;
req.session.auth = true;
req.session.userId = 1;
console.log('User has logged in');
goToLastPage();
}
next();
return;
}