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.

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