我需要在身份验证/登录后立即在会话中设置数据,所以我设置了这样的中间件。
middleware.json
{
"initial": { },
"session": {
"express-session": {
"params": {
"secret": "mysceret",
"saveUninitialized": true,
"resave": true
}
}
},
"auth:before": {},
"auth": {
"loopback#token": {}
},
"auth:after": {
"./middleware/store-current-user": {}
},
"parse": { }
}
在我的store-current-user.js中:
module.exports = function (options) {
console.log(" it is working here");
return function storeCurrentUser(req, res, next) {
console.log(" it is not working here");
if (!req.accessToken) {
return next();
}
app.models.User.findById(req.accessToken.userId, function (err, user) {
if (err) {
return next(err);
}
if (!user) {
return next(new Error('No user with this access token was found.'));
}else{
console.log(' ok '); // it is not working.
req.session.user = user;
next();
}
});
};
};
快递会话:"快递会议":" ^ 1.15.6"。 Loopback版本:" loopback":" ^ 3.0.0"
我失踪的地方?我无法理解。
请一些帮助。
答案 0 :(得分:0)
尝试这样的事情?
module.exports = function () {
console.log(" it is working here");
return function storeCurrentUser(req, res, next) {
console.log(" it is not working here");
if (!req.accessToken) {
next();
}
app.models.User.findById(req.accessToken.userId, function (err, user) {
if (err) {
next(err);
}
if (!user) {
next(new Error('No user with this access token was found.'));
}else{
console.log(' ok '); // it is not working.
req.session.user = user;
next();
}
});
};
};
options
(如果需要,请稍后再添加)return next()
,仅next()