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.

264 lines
8.0 KiB

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