2016-06-24 00:29:55 +02:00
|
|
|
import passport from 'passport';
|
2016-06-14 22:48:16 +02:00
|
|
|
|
|
|
|
export function createSession(req, res, next) {
|
2016-06-24 00:29:55 +02:00
|
|
|
passport.authenticate('local', (err, user) => { // eslint-disable-line consistent-return
|
|
|
|
if (err) { return next(err); }
|
|
|
|
if (!user) {
|
2019-09-06 19:30:06 +02:00
|
|
|
return res.status(401).json({ message: 'Invalid username or password.' });
|
2016-06-24 00:29:55 +02:00
|
|
|
}
|
2016-06-14 22:48:16 +02:00
|
|
|
|
2016-06-24 00:29:55 +02:00
|
|
|
req.logIn(user, (innerErr) => {
|
|
|
|
if (innerErr) { return next(innerErr); }
|
|
|
|
return res.json({
|
|
|
|
email: req.user.email,
|
2016-08-10 00:45:59 +02:00
|
|
|
username: req.user.username,
|
|
|
|
preferences: req.user.preferences,
|
2017-06-26 18:48:28 +02:00
|
|
|
verified: req.user.verified,
|
2016-08-10 00:45:59 +02:00
|
|
|
id: req.user._id
|
2016-06-24 00:29:55 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
})(req, res, next);
|
2016-06-15 01:11:42 +02:00
|
|
|
}
|
|
|
|
|
2016-06-24 00:29:55 +02:00
|
|
|
export function getSession(req, res) {
|
|
|
|
if (req.user) {
|
|
|
|
return res.json({
|
|
|
|
email: req.user.email,
|
2016-08-09 23:50:45 +02:00
|
|
|
username: req.user.username,
|
2016-08-10 00:45:59 +02:00
|
|
|
preferences: req.user.preferences,
|
2017-06-26 18:48:28 +02:00
|
|
|
verified: req.user.verified,
|
2016-08-10 00:45:59 +02:00
|
|
|
id: req.user._id
|
2016-06-24 00:29:55 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return res.status(404).send({ message: 'Session does not exist' });
|
|
|
|
}
|
2016-08-28 02:46:20 +02:00
|
|
|
|
|
|
|
export function destroySession(req, res) {
|
|
|
|
req.logout();
|
2016-11-17 17:15:35 +01:00
|
|
|
res.json({ success: true });
|
2016-08-28 02:46:20 +02:00
|
|
|
}
|
|
|
|
|