2020-04-13 00:02:40 +00:00
|
|
|
var mongoose = require('mongoose');
|
|
|
|
var router = require('express').Router();
|
|
|
|
var passport = require('passport');
|
|
|
|
var User = mongoose.model('User');
|
|
|
|
var auth = require('../auth');
|
|
|
|
|
2020-10-20 19:25:00 +00:00
|
|
|
router.get('/user', auth.required, function (req, res, next) {
|
|
|
|
User.findById(req.payload.id).then(function (user) {
|
|
|
|
if (!user) { return res.sendStatus(401); }
|
2020-04-13 00:02:40 +00:00
|
|
|
|
2020-10-20 19:25:00 +00:00
|
|
|
return res.json({ user: user.toAuthJSON() });
|
2020-04-13 00:02:40 +00:00
|
|
|
}).catch(next);
|
|
|
|
});
|
|
|
|
|
2020-10-20 19:25:00 +00:00
|
|
|
router.put('/user', auth.required, function (req, res, next) {
|
|
|
|
User.findById(req.payload.id).then(function (user) {
|
|
|
|
if (!user) { return res.sendStatus(401); }
|
2020-04-13 00:02:40 +00:00
|
|
|
|
|
|
|
// only update fields that were actually passed...
|
2020-10-20 19:25:00 +00:00
|
|
|
if (typeof req.body.user.username !== 'undefined') {
|
2020-04-13 00:02:40 +00:00
|
|
|
user.username = req.body.user.username;
|
|
|
|
}
|
2020-10-20 19:25:00 +00:00
|
|
|
if (typeof req.body.user.email !== 'undefined') {
|
2020-04-13 00:02:40 +00:00
|
|
|
user.email = req.body.user.email;
|
|
|
|
}
|
2020-10-20 19:25:00 +00:00
|
|
|
if (typeof req.body.user.bio !== 'undefined') {
|
2020-04-13 00:02:40 +00:00
|
|
|
user.bio = req.body.user.bio;
|
|
|
|
}
|
2020-10-20 19:25:00 +00:00
|
|
|
if (typeof req.body.user.image !== 'undefined') {
|
2020-04-13 00:02:40 +00:00
|
|
|
user.image = req.body.user.image;
|
|
|
|
}
|
2020-10-20 19:25:00 +00:00
|
|
|
if (typeof req.body.user.areTracksVisibleForAll !== 'undefined') {
|
2020-08-16 21:23:18 +00:00
|
|
|
user.areTracksVisibleForAll = req.body.user.areTracksVisibleForAll;
|
|
|
|
}
|
2020-10-20 19:25:00 +00:00
|
|
|
if (typeof req.body.user.password !== 'undefined' && req.body.user.password != '') {
|
2020-04-13 00:02:40 +00:00
|
|
|
user.setPassword(req.body.user.password);
|
|
|
|
}
|
|
|
|
|
2020-10-20 19:25:00 +00:00
|
|
|
return user.save().then(function () {
|
|
|
|
return res.json({ user: user.toAuthJSON() });
|
2020-04-13 00:02:40 +00:00
|
|
|
});
|
|
|
|
}).catch(next);
|
|
|
|
});
|
|
|
|
|
2020-10-20 19:25:00 +00:00
|
|
|
router.post('/users/login', function (req, res, next) {
|
|
|
|
if (!req.body.user.email) {
|
|
|
|
return res.status(422).json({ errors: { email: "can't be blank" } });
|
2020-04-13 00:02:40 +00:00
|
|
|
}
|
|
|
|
|
2020-10-20 19:25:00 +00:00
|
|
|
if (!req.body.user.password) {
|
|
|
|
return res.status(422).json({ errors: { password: "can't be blank" } });
|
2020-04-13 00:02:40 +00:00
|
|
|
}
|
|
|
|
|
2020-10-20 19:25:00 +00:00
|
|
|
passport.authenticate('local', { session: false }, function (err, user, info) {
|
|
|
|
if (err) { return next(err); }
|
2020-04-13 00:02:40 +00:00
|
|
|
|
2020-10-20 19:25:00 +00:00
|
|
|
if (user) {
|
2020-04-13 00:02:40 +00:00
|
|
|
user.token = user.generateJWT();
|
2020-10-20 19:25:00 +00:00
|
|
|
return res.json({ user: user.toAuthJSON() });
|
2020-04-13 00:02:40 +00:00
|
|
|
} else {
|
|
|
|
return res.status(422).json(info);
|
|
|
|
}
|
|
|
|
})(req, res, next);
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = router;
|