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.

235 lines
7.1 KiB

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