2016-06-24 00:29:55 +02:00
|
|
|
import User from '../models/user';
|
2016-06-09 02:52:59 +02:00
|
|
|
|
|
|
|
export function createUser(req, res, next) {
|
2016-06-24 00:29:55 +02:00
|
|
|
const user = new User({
|
|
|
|
username: req.body.username,
|
2016-06-09 02:52:59 +02:00
|
|
|
email: req.body.email,
|
|
|
|
password: req.body.password
|
|
|
|
});
|
|
|
|
|
2016-06-24 00:29:55 +02:00
|
|
|
User.findOne({ email: req.body.email },
|
|
|
|
(err, existingUser) => { // eslint-disable-line consistent-return
|
|
|
|
if (err) { res.status(404).send({ error: err }); }
|
2016-06-09 02:52:59 +02:00
|
|
|
|
2016-06-24 00:29:55 +02:00
|
|
|
if (existingUser) {
|
|
|
|
return res.status(422).send({ error: 'Email is in use' });
|
|
|
|
}
|
|
|
|
user.save((saveErr) => { // eslint-disable-line consistent-return
|
|
|
|
if (saveErr) { return next(saveErr); }
|
|
|
|
req.logIn(user, (loginErr) => { // eslint-disable-line consistent-return
|
|
|
|
if (loginErr) {
|
|
|
|
return next(loginErr);
|
|
|
|
}
|
|
|
|
res.json({
|
|
|
|
email: req.user.email,
|
2016-08-10 00:45:59 +02:00
|
|
|
username: req.user.username,
|
|
|
|
preferences: req.user.preferences,
|
|
|
|
id: req.user._id
|
2016-06-24 00:29:55 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2016-08-05 03:43:13 +02:00
|
|
|
|
|
|
|
export function updatePreferences(req, res) {
|
2016-08-09 20:20:54 +02:00
|
|
|
User.findById(req.user.id, (err, user) => {
|
|
|
|
if (err) {
|
|
|
|
return res.status(500).json({error: err});
|
|
|
|
}
|
|
|
|
if (!user){
|
|
|
|
return res.status(404).json({error: 'Document not found'});
|
|
|
|
}
|
|
|
|
|
|
|
|
const preferences = Object.assign({}, user.preferences, req.body.preferences);
|
|
|
|
user.preferences = preferences;
|
|
|
|
|
|
|
|
user.save((err) => {
|
|
|
|
if (err) {
|
|
|
|
return res.status(500).json({error: err});
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.json(user.preferences);
|
|
|
|
});
|
2016-08-05 03:43:13 +02:00
|
|
|
})
|
|
|
|
}
|