js和Passport.js。所以我有这个:
router.post('/login', middleware1, middleware2);
我的app.js
passport.use(new passportLocal(
{ usernameField: 'email' },
function(email, password, done) {
// working logic here
// this returns a user object
// to the middleware1 if query is ok
// returns error otherwise.
}
));
这是我的middlewares
middleware1 = function (req, res, next) {
passport.authenticate('local', function(error, user, message){
// How do I get the 'user' object and pass it
// to the outer req or res?
req.user = user; // <- is this ok?
})(req, res, next);
req.user = user;
next();
}
middleware2 = function (req, res, next) {
console.log("user ", req.user); // <- this will obviously print null or undefined
}
现在。如何将user object
从passport.authenticate()
传递到middleware2
。我正在考虑使用像此promise
这样的passport.authenticate().then()
,但我不知道语法。请帮忙。谢谢。
答案 0 :(得分:0)
app.post('/login',
passport.authenticate('local'),
function(req, res) {
// If this function gets called, authentication was successful.
// `req.user` contains the authenticated user.
res.redirect('/users/' + req.user.username);
});
authenticate()的函数签名是标准的Connect中间件,方便用作路由中间件
为什么要将passport.authenticate()包装在另一个函数中?
我不确定你要做什么,但我可能会在没有误导的情况下开始
router.post('/login', passport.authenticate('local'), middleware2(req,res,next));
function middleware2(req,res,next) {
console.log("user ", req.user);
}
如果你希望传递给你的函数的req对象设置护照js选项'passReqToCallback:true'