我的问题类似于 one ,但没有深入了解他的解决方案。
我正在使用 Passport 通过 Instagram 进行身份验证。验证成功后,用户被定向到“/”。此时,请求具有用户对象(又名它正在工作)。但是,一旦我重定向, req.user 是未定义的。 :'(
奇怪的是,每个请求都会调用 passport.deserializeUser。它成功地获取了用户对象,但是在中间件路径的某个地方,req.user 没有被设置(或被取消设置)。
// on successful auth, goto "/"
app.get('/', function(req, res) {
// if the request has the user object, go to the user page
if (req.user) {
res.redirect("/user/" + req.user._id);
}
res.render("index");
}
app.get('/user/:uid', function(req, res) {
console.log(req.user) // undefined
}
最佳答案
我的问题是在客户端使用 fetch 时没有指定发送 cookie。它在请求中包含凭据后起作用:“包含”字段。
fetch('/api/foo', {credentials: 'include'})
关于node.js - Node + Express + Passport : req. 用户未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16434893/