我正在尝试在快速会话登录后进行自动调整,但它给了我undefined: AUTH:
router.post('/login', (req, res, next) => {
model.User.findOne({
email: req.body.email,
password: hash(req.body.password)
}).lean().exec().then(
user => {
if (!user) {
return res.status(401).send({message: 'Invalid email and/or password'});
}
req.session.user = user;
req.session.authorized = true;
console.log(req.session);
return res.send({token: createJWT(user)});
}
).catch(next);
});
此req.session按预期工作,但当我尝试验证我的路线时,我得到了未定义: 路线:
router.use('/api', require('cors')(), require('./api')(Router().use(global.app.security.authorize())));
授权服务:
module.exports = () =>
(req, res, next) => {
console.log(req.session.user);
if (req.session.authorized) {
req.user = req.session.user;
return next();
}
};
如果我尝试在我的任何路线上使用会话,那么这个也会被定义...
服务器:
const winston = require('winston'),
express = require('express'),
bodyParser = require('body-parser');
const server = module.exports;
server.start = () => {
const api = global.app.api = express();
api.set('port', process.env.PORT || global.app.config.server.port);
api.disable('x-powered-by');
api.use(require('morgan')(global.app.config.logger || (api.get('env') === 'development' ? 'dev' : 'combined')));
api.use(bodyParser.json(global.app.config.server.request.bodyParser));
api.use(bodyParser.urlencoded(global.app.config.server.request.bodyParser));
api.use(require('cookie-parser')(global.app.config.auth.sessionSecret));
api.use(require('cookie-session')({secret: global.app.config.auth.sessionSecret}));
require('../../routes/')(api);
api.listen(api.get('port'), () => winston.info('%s started at port %s', global.app.name, api.get('port')));
};