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.

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