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.

275 lines
6.5 KiB

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