p5.js-web-editor/server/controllers/session.controller.js

33 lines
887 B
JavaScript
Raw Normal View History

2016-06-23 22:29:55 +00:00
import passport from 'passport';
2016-06-14 20:48:16 +00:00
export function createSession(req, res, next) {
2016-06-23 22:29:55 +00:00
passport.authenticate('local', (err, user) => { // eslint-disable-line consistent-return
if (err) { return next(err); }
if (!user) {
return res.status(401).send({ error: 'Invalid username or password' });
}
2016-06-14 20:48:16 +00:00
2016-06-23 22:29:55 +00:00
req.logIn(user, (innerErr) => {
if (innerErr) { return next(innerErr); }
return res.json({
email: req.user.email,
username: req.user.username,
preferences: req.user.preferences,
id: req.user._id
2016-06-23 22:29:55 +00:00
});
});
})(req, res, next);
}
2016-06-23 22:29:55 +00:00
export function getSession(req, res) {
if (req.user) {
return res.json({
email: req.user.email,
username: req.user.username,
preferences: req.user.preferences,
id: req.user._id
2016-06-23 22:29:55 +00:00
});
}
return res.status(404).send({ message: 'Session does not exist' });
}