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.

252 lines
7.6 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. exports.getUserByToken(req, res);
  135. });
  136. };
  137. //DELETE - Delete a user with specified ID
  138. exports.deleteUser = function(req, res) {
  139. userModel.findOne({'token': req.headers['x-access-token']})
  140. .exec(function(err, user) {
  141. user.remove(function(err) {
  142. if (err) return res.send(500, err.message);
  143. res.status(200).jsonp("deleted");
  144. })
  145. });
  146. };
  147. /* fav */
  148. exports.addFav = function(req, res) {
  149. var tokenuser;
  150. userModel.find({
  151. token: req.headers['x-access-token']
  152. }, function(err, users) {
  153. tokenuser = users[0];
  154. });
  155. userModel.findById(req.params.userId, function(err, user) {
  156. // first search if user have already said like
  157. var favRepeated = false;
  158. for (var i = 0; i < user.favs.length; i++) {
  159. if (user.favs[i].username == tokenuser.username) {
  160. favRepeated = true;
  161. }
  162. }
  163. console.log("favRepeated: " + favRepeated);
  164. if (favRepeated == false) {
  165. //fav
  166. var fav = {
  167. userId: tokenuser._id,
  168. username: tokenuser.username,
  169. avatar: tokenuser.avatar
  170. };
  171. user.favs.push(fav);
  172. //notification
  173. var notification = {
  174. concept: "like",
  175. otherusername: tokenuser.username,
  176. description: "user " + tokenuser.username + " favs you",
  177. date: new Date(),
  178. link: ""
  179. };
  180. user.notifications.push(notification);
  181. user.save(function(err, user) {
  182. if (err) return res.send(500, err.message);
  183. //once saved, send the users json to client
  184. userModel.find(function(err, users) {
  185. if (err) res.send(500, err.message);
  186. res.status(200).jsonp(users);
  187. });
  188. });
  189. } else {
  190. userModel.find(function(err, users) {
  191. if (err) res.send(500, err.message);
  192. res.status(200).jsonp(users);
  193. });
  194. }
  195. });
  196. };
  197. exports.doUnfav = function(req, res) {
  198. var tokenuser;
  199. userModel.find({
  200. token: req.headers['x-access-token']
  201. }, function(err, users) {
  202. tokenuser = users[0];
  203. });
  204. userModel.findById(req.params.userId, function(err, user) {
  205. for (var i = 0; i < user.favs.length; i++) {
  206. if (user.favs[i].username == tokenuser.username) {
  207. user.favs.splice(i, 1);
  208. }
  209. }
  210. user.save(function(err, travel) {
  211. if (err) return res.send(500, err.message);
  212. //res.status(200).jsonp(travel);
  213. userModel.find(function(err, users) {
  214. if (err) res.send(500, err.message);
  215. res.status(200).jsonp(users);
  216. });
  217. });
  218. });
  219. };