You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

254 lines
7.6 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. //File: controllers/userController.js
  2. var mongoose = require('mongoose');
  3. var userModel = mongoose.model('userModel');
  4. var notificationModel = mongoose.model('notificationModel');
  5. /* */
  6. var jwt = require('jsonwebtoken'); // used to create, sign, and verify tokens
  7. var express = require("express");
  8. var app = express();
  9. var config = require('../config'); // get our config file
  10. app.set('superSecret', config.secret); // secret variable
  11. var crypto = require('crypto');
  12. /* */
  13. //POST - Insert a new User in the DB
  14. exports.signup = function(req, res) {
  15. var user = new userModel({
  16. username: req.body.username,
  17. password: crypto.createHash('sha256').update(req.body.password).digest('base64'),
  18. description: req.body.description,
  19. avatar: req.body.avatar,
  20. email: req.body.email,
  21. phone: req.body.phone,
  22. telegram: req.body.telegram
  23. });
  24. if (user.username == undefined) {
  25. return res.status(500).jsonp("empty inputs");
  26. } else if (user.password == undefined) {
  27. return res.status(500).jsonp("empty inputs");
  28. } else if (user.email == undefined) {
  29. return res.status(500).jsonp("empty inputs");
  30. }
  31. user.save(function(err, user) {
  32. if (err) return res.send(500, err.message);
  33. exports.login(req, res);
  34. });
  35. };
  36. //POST - auth user
  37. exports.login = function(req, res) {
  38. // find the user
  39. userModel.findOne({
  40. username: req.body.username
  41. })
  42. .select('+password')
  43. .exec(function(err, user) {
  44. if (err) throw err;
  45. if (!user) {
  46. res.json({
  47. success: false,
  48. message: 'Authentication failed. User not found.'
  49. });
  50. } else if (user) {
  51. req.body.password = crypto.createHash('sha256').update(req.body.password).digest('base64');
  52. // check if password matches
  53. if (user.password != req.body.password) {
  54. res.json({
  55. success: false,
  56. message: 'Authentication failed. Wrong password.'
  57. });
  58. } else {
  59. // if user is found and password is right
  60. // create a token
  61. var token = jwt.sign({foo: 'bar'}, app.get('superSecret'), {
  62. //expiresInMinutes: 1440 // expires in 24 hours
  63. //expiresIn: '60m'
  64. });
  65. user.token = token;
  66. user.save(function(err, user) {
  67. if (err) return res.send(500, err.message);
  68. //res.status(200).jsonp(travel);
  69. console.log(user);
  70. // return the information including token as JSON
  71. user.password = "";
  72. res.json({
  73. success: true,
  74. message: 'Enjoy your token!',
  75. token: token,
  76. user: user
  77. });
  78. });
  79. }
  80. }
  81. });
  82. };
  83. //GET - Return all Users in the DB
  84. exports.getAllUsers = function(req, res) {
  85. userModel.find()
  86. .limit(Number(req.query.pageSize))
  87. .skip(Number(req.query.pageSize) * Number(req.query.page))
  88. .exec(function (err, users) {
  89. if (err) return res.send(500, err.message);
  90. res.status(200).jsonp(users);
  91. });
  92. };
  93. exports.getUserById = function (req, res) {
  94. userModel.findOne({_id: req.params.userid})
  95. .lean()
  96. .populate('travels', 'title from to date type')
  97. .exec(function (err, user) {
  98. if (err) return res.send(500, err.message);
  99. if (!user) {
  100. res.json({success: false, message: 'User not found.'});
  101. } else if (user) {
  102. res.status(200).jsonp(user);
  103. }
  104. });
  105. };
  106. exports.getUserByToken = function (req, res) {
  107. userModel.findOne({'token': req.headers['x-access-token']})
  108. .lean()
  109. .populate('travels', 'title from to date')
  110. .exec(function (err, user) {
  111. if (err) return res.send(500, err.message);
  112. if (!user) {
  113. res.json({success: false, message: 'User not found.'});
  114. } else if (user) {
  115. res.status(200).jsonp(user);
  116. }
  117. });
  118. };
  119. exports.getNotifications = function (req, res) {
  120. userModel.findOne({'token': req.headers['x-access-token']})
  121. .lean()
  122. .populate('notifications')
  123. .exec(function (err, user) {
  124. if (err) return res.send(500, err.message);
  125. if (!user) {
  126. res.json({success: false, message: 'User not found.'});
  127. } else if (user) {
  128. res.status(200).jsonp(user.notifications);
  129. }
  130. });
  131. };
  132. exports.updateUser = function (req, res) {
  133. userModel.update({'token': req.headers['x-access-token']}, req.body,
  134. function (err) {
  135. if (err) return console.log(err);
  136. exports.getUserByToken(req, res);
  137. });
  138. };
  139. //DELETE - Delete a user with specified ID
  140. exports.deleteUser = function(req, res) {
  141. userModel.findOne({'token': req.headers['x-access-token']})
  142. .exec(function(err, user) {
  143. user.remove(function(err) {
  144. if (err) return res.send(500, err.message);
  145. res.status(200).jsonp("deleted");
  146. })
  147. });
  148. };
  149. /* fav */
  150. exports.addFav = function(req, res) {
  151. var tokenuser;
  152. userModel.find({
  153. token: req.headers['x-access-token']
  154. }, function(err, users) {
  155. tokenuser = users[0];
  156. });
  157. userModel.findById(req.params.userId, function(err, user) {
  158. // first search if user have already said like
  159. var favRepeated = false;
  160. for (var i = 0; i < user.favs.length; i++) {
  161. if (user.favs[i].username == tokenuser.username) {
  162. favRepeated = true;
  163. }
  164. }
  165. console.log("favRepeated: " + favRepeated);
  166. if (favRepeated == false) {
  167. //fav
  168. var fav = {
  169. userId: tokenuser._id,
  170. username: tokenuser.username,
  171. avatar: tokenuser.avatar
  172. };
  173. user.favs.push(fav);
  174. //notification
  175. var notification = {
  176. concept: "like",
  177. otherusername: tokenuser.username,
  178. description: "user " + tokenuser.username + " favs you",
  179. date: new Date(),
  180. link: ""
  181. };
  182. user.notifications.push(notification);
  183. user.save(function(err, user) {
  184. if (err) return res.send(500, err.message);
  185. //once saved, send the users json to client
  186. userModel.find(function(err, users) {
  187. if (err) res.send(500, err.message);
  188. res.status(200).jsonp(users);
  189. });
  190. });
  191. } else {
  192. userModel.find(function(err, users) {
  193. if (err) res.send(500, err.message);
  194. res.status(200).jsonp(users);
  195. });
  196. }
  197. });
  198. };
  199. exports.doUnfav = function(req, res) {
  200. var tokenuser;
  201. userModel.find({
  202. token: req.headers['x-access-token']
  203. }, function(err, users) {
  204. tokenuser = users[0];
  205. });
  206. userModel.findById(req.params.userId, function(err, user) {
  207. for (var i = 0; i < user.favs.length; i++) {
  208. if (user.favs[i].username == tokenuser.username) {
  209. user.favs.splice(i, 1);
  210. }
  211. }
  212. user.save(function(err, travel) {
  213. if (err) return res.send(500, err.message);
  214. //res.status(200).jsonp(travel);
  215. userModel.find(function(err, users) {
  216. if (err) res.send(500, err.message);
  217. res.status(200).jsonp(users);
  218. });
  219. });
  220. });
  221. };