2016-06-23 22:29:55 +00:00
|
|
|
import passport from 'passport';
|
2016-06-14 20:48:16 +00:00
|
|
|
|
2019-05-22 15:57:09 +00:00
|
|
|
import { userResponse } from './user.controller';
|
|
|
|
|
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) {
|
2016-09-02 21:31:07 +00:00
|
|
|
return res.status(401).send({ message: 'Invalid username or password.' });
|
2016-06-23 22:29:55 +00:00
|
|
|
}
|
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); }
|
2019-05-22 15:57:09 +00:00
|
|
|
return res.json(userResponse(req.user));
|
2016-06-23 22:29:55 +00:00
|
|
|
});
|
|
|
|
})(req, res, next);
|
2016-06-14 23:11:42 +00:00
|
|
|
}
|
|
|
|
|
2016-06-23 22:29:55 +00:00
|
|
|
export function getSession(req, res) {
|
|
|
|
if (req.user) {
|
2019-05-22 15:57:09 +00:00
|
|
|
return res.json(userResponse(req.user));
|
2016-06-23 22:29:55 +00:00
|
|
|
}
|
|
|
|
return res.status(404).send({ message: 'Session does not exist' });
|
|
|
|
}
|
2016-08-28 00:46:20 +00:00
|
|
|
|
|
|
|
export function destroySession(req, res) {
|
|
|
|
req.logout();
|
2016-11-17 16:15:35 +00:00
|
|
|
res.json({ success: true });
|
2016-08-28 00:46:20 +00:00
|
|
|
}
|
|
|
|
|