javascript Passport.js 没有将用户传递给 req.login() 中的请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24171489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passport.js is not passing user to request in req.login()
提问by Eduardo de Luna
My passport.js configuration goes like so:
我的passport.js 配置是这样的:
const Local = require("passport-local").Strategy;
const USMODEL = require("../models/user.js");
passport.serializeUser(function(user, done) {
console.log("SERIALIZING USER");
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
console.log("DESUSER", id);
var US = mongoose.model("RegUser", USMODEL);
US.findById(id, function(err, user) {
done(err, id);
});
});
passport.use("local-login", new Local({
usernameField: "email",
passwordField: "password",
passReqToCallback: true
},function(req, email, password, done) {
var US = mongoose.model("RegUser", USMODEL);
US.findOne({"email": email}, function(err, user){
if(err) throw err;
if(!user) return done(null, false);
if(!user.validPassword(password)) {
console.log("password not valid");
return done(null, false);
}
return done(null, user);
});
}));
I'm changing the mongoose model within each function because I juggle with multiple collections at a time and I like to have complete control of what's going on.
我正在更改每个函数中的猫鼬模型,因为我一次处理多个集合,并且我喜欢完全控制正在发生的事情。
My router.js file has the following paths that make use of the passport middleware:
我的 router.js 文件具有以下使用通行证中间件的路径:
app.get("/user/login", function(req, res) {
res.render("signin");
});
app.post('/user/login', function (req, res){
passport.authenticate('local-login', function(err, user, info){
if (err) return res.redirect("/");
if (!user) return res.redirect('/');
else {
req.login(user, function(err) {
if (err) return next(err);
console.log("Request Login supossedly successful.");
return res.redirect('/admin/filter');
});
}
})(req, res);
});
Which, upon successful authentication, redirects to /admin/filter in the same router that goes like so.
其中,在成功验证后,重定向到 /admin/filter 在同一个路由器中,就像这样。
app.get("/admin/filter", isLoggedIn, function(req, res){
//rendering stuff here
});
Now, the admin/filter request goes past a middleware called isLoggedIn
which, in theory protects my endpoints. It goes like so:
现在,管理/过滤器请求通过了一个中间件isLoggedIn
,从理论上讲,它可以保护我的端点。它是这样的:
function isLoggedIn(req, res, next) {
console.log("This is the authentication middleware, is req authenticated?");
console.log(req.isAuthenticated());
console.log("Does req.user exist?")
console.log(req.user);
return next();
}
Now, you would expect that because I called req.login and I got redirected to my endpoint of choice, the request would be authenticated. This is not the case.
现在,您会期望因为我调用了 req.login 并且我被重定向到我选择的端点,所以请求将被验证。不是这种情况。
Request Login supossedly successful.
This is the authentication middleware, is req authenticated?
false
Does req.user exist?
undefined
I can't seem to find the source of my problem. Everything checks out, as the strategy is being invoked, as well as the callback function and req.login
which would render, in theory, a req.user
object with data in it. One odd thing I've observed is that I don't see the passport.deserializeUser()
method in action. Ever. But that could be tangential to the problem. Passport is definitely using my strategy and rendering a user object, but somehow this same object is not going into the request. Do you have any suggestion or idea about what is going on?
我似乎无法找到问题的根源。当策略被调用时,一切都会检查出来,回调函数req.login
也会在理论上呈现一个req.user
包含数据的对象。我观察到的一件奇怪的事情是我没有看到该passport.deserializeUser()
方法在起作用。曾经。但这可能与问题无关。Passport 肯定在使用我的策略并呈现用户对象,但不知何故这个相同的对象并没有进入请求。你对正在发生的事情有什么建议或想法吗?
采纳答案by Eduardo de Luna
I solved the problem by juggling around with the tutorial I started with when I first learned how to use the Passport middleware. Turns out I was doing the configuring wrong: My code used to be like this in the server file:
我通过在我第一次学习如何使用 Passport 中间件时开始使用的教程来解决这个问题。原来我的配置是错误的:我的代码在服务器文件中曾经是这样的:
pass = require("passport");
app.use(pass.initialize());
app.use(pass.session());
require("./app/config/passport.js")(pass);
when it should have been this:
当它应该是这样的:
pass = require("passport");
require("./app/config/passport.js")(pass);
app.use(pass.initialize());
app.use(pass.session());
Either I missed the part in the documentation where it's specified that configuration must come before initialization or it's simply written off as a trivial thing to remark. Either way, I solved my problem.
要么我错过了文档中指定配置必须在初始化之前出现的部分,要么只是将其作为微不足道的评论而注销。不管怎样,我解决了我的问题。