Im trying to create a cookie-session to authenticate users in my routes, when the use logs in i set the session:
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);
});
that console log shows the session correctly but then when i try to authenticate my routes
const {Router} = require('express');
const winston = require('winston');
module.exports = router => {
winston.info('Loading public-api...');
router.use('/', require('cors')(), require('./public-api')(Router()));
router.use('/api', require('cors')(), require('./api')(Router().use(global.app.security.authorize())));
router.use('/auth', require('cors')(), require('./auth')(Router()));
};
then the authorize service
module.exports = () =>
(req, res, next) => {
console.log(req.session);
if (req.session.authorized) {
req.user = req.session.user;
return next();
}
};
here req.session is empty.
Edit: i think i know where the error is from my server is hosted in localhost:6000 but the client in localhost:3000 how can i make it work.