TwoToc code
[YouAndWeb_TwoToc] / server / api / user / user.controller.js
1 'use strict';
2
3 var User = require('./user.model');
4 var passport = require('passport');
5 var config = require('../../config/environment');
6 var jwt = require('jsonwebtoken');
7
8 function validationError(res, statusCode) {
9   statusCode = statusCode || 422;
10   return function(err) {
11     res.status(statusCode).json(err);
12   }
13 }
14
15 function handleError(res, statusCode) {
16   statusCode = statusCode || 500;
17   return function(err) {
18     res.status(statusCode).send(err);
19   };
20 }
21
22 function respondWith(res, statusCode) {
23   statusCode = statusCode || 200;
24   return function() {
25     res.status(statusCode).end();
26   };
27 }
28
29 /**
30  * Get list of users
31  * restriction: 'admin'
32  */
33 exports.index = function(req, res) {
34   User.findAsync({}, '-salt -hashedPassword')
35     .then(function(users) {
36       res.status(200).json(users);
37     })
38     .catch(handleError(res));
39 };
40
41 /**
42  * Creates a new user
43  */
44 exports.create = function(req, res, next) {
45   var newUser = new User(req.body);
46   newUser.provider = 'local';
47   newUser.role = 'user';
48   newUser.saveAsync()
49     .spread(function(user) {
50       var token = jwt.sign({ _id: user._id }, config.secrets.session, {
51         expiresInMinutes: 60 * 5
52       });
53       user.token = token;
54       res.json({ token: token });
55     })
56     .catch(validationError(res));
57 };
58
59 /**
60  * Get a single user
61  */
62 exports.show = function(req, res, next) {
63   var userId = req.params.id;
64
65   User.findByIdAsync(userId)
66     .then(function(user) {
67       if (!user) {
68         return res.status(404).end();
69       }
70       res.json(user.profile);
71     })
72     .catch(function(err) {
73       return next(err);
74     });
75 };
76
77 /**
78  * Deletes a user
79  * restriction: 'admin'
80  */
81 exports.destroy = function(req, res) {
82   User.findByIdAndRemoveAsync(req.params.id)
83     .then(function() {
84       res.status(204).end();
85     })
86     .catch(handleError(res));
87 };
88
89 /**
90  * Change a users password
91  */
92 exports.changePassword = function(req, res, next) {
93   var userId = req.user._id;
94   var oldPass = String(req.body.oldPassword);
95   var newPass = String(req.body.newPassword);
96
97   User.findByIdAsync(userId)
98     .then(function(user) {
99       if (user.authenticate(oldPass)) {
100         user.password = newPass;
101         return user.saveAsync()
102           .then(function() {
103             res.status(204).end();
104           })
105           .catch(validationError(res));
106       } else {
107         return res.status(403).end();
108       }
109     });
110 };
111
112 /**
113  * Get my info
114  */
115 exports.me = function(req, res, next) {
116   var userId = req.user._id;
117
118   User.findOneAsync({ _id: userId }, '-salt -hashedPassword')
119     .then(function(user) { // don't ever give out the password or salt
120       if (!user) {
121         return res.status(401).end();
122       }
123       res.json(user);
124     })
125     .catch(function(err) {
126       return next(err);
127     });
128 };
129
130 /**
131  * Authentication callback
132  */
133 exports.authCallback = function(req, res, next) {
134   res.redirect('/');
135 };