【发布时间】:2017-02-03 19:17:12
【问题描述】:
在使用用户身份验证时,我在重定向期间面临无限循环。
以下是 app.js 中的代码片段:
const hauthen = require('./handlers/hauthen');
const routes = require('./routes/index');
const authen = require('./routes/authen');
const users = require('./routes/users');
app.use(hauthen);
// routes
app.use('/', routes);
app.use('/authen', authen);
app.use('/users', users);
而这段代码来自认证页面hauthen.js:
router.all("/authen", (req, res, next) => {
if (req.authen) {
res.redirect("/");
} else {
next();
}
});
router.all("*", function(req, res, next) {
if (req.authen !== undefined) {
next();
} else {
res.redirect('/authen');
}
});
基本思想是如果用户尚未通过身份验证,则重定向到登录页面。但我在控制台中得到了根 url“/”的这个。
GET / 302 16.464 ms - 58
GET /authen 302 2.930 ms - 58
GET /authen 302 1.587 ms - 58
GET /authen 302 0.854 ms - 58
GET /authen 302 1.467 ms - 58
GET /authen 302 1.878 ms - 58
GET /authen 302 0.681 ms - 58
那么,是什么导致了无限重定向问题以及如何解决呢? 我做错了吗?
【问题讨论】:
标签: javascript node.js authentication redirect