我以为我遵循了文档,但我得到了 401 响应。我有一个接收 401 响应的简单 POST 表单。
这是我的示例代码(案例 1):
passport.serializeUser(function(user, done) {
console.log("Serializing:", user);
done(null, JSON.parse(user));
});
passport.deserializeUser(function(obj, done) {
console.log("Deserializing:" + obj);
done(null, obj);
});
passport.use(new LocalStrategy(
function(username, password, done) {
console.log("Authenticating.....:" + username);
User.findOne({ username: username }, function(err, user) {
if (err) { return done(err); }
if (!user) {
return done(null, false, { message: 'Incorrect username.' });
}
if (!user.validPassword(password)) {
return done(null, false, { message: 'Incorrect password.' });
}
return done(null, user);
});
}
));
app.post('/auth/login',
passport.authenticate('local'),
function (req, res) {
console.log("Made it here");
}
);
上面的代码从不打印任何发送到 console.log 的消息。我所看到的只是来自 nodejs/express 的一条消息,在控制台中显示“POST /auth/login 401”。
有趣的是,我将 app.post 函数更改为如下(案例 2):
app.post('/auth/login',
passport.authenticate('local', {
successRedirect:'/partials/playlist/playlists',
failureRedirect:'/partials/welcome'
}),
function (req, res) {
console.log("Made it here");
}
);
这一次,唯一被调用的链接是 /partials/welcome 重定向为“POST /auth/login 302”。同样,控制台中没有记录任何消息。
我的问题:为什么两个 app.post 实现的行为不同(CASE 1 vs CASE 2)?为什么在 CASE 2 中从未调用过我的“SucessRedirect”?为什么 console.log 中没有打印任何内容?
有任何想法吗?