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.

262 lines
6.3 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. var fav = {
  113. userId: tokenuser._id,
  114. username: tokenuser.username,
  115. avatar: tokenuser.avatar
  116. };
  117. user.favs.push(fav);
  118. user.save(function(err, user) {
  119. if(err) return res.send(500, err.message);
  120. //res.status(200).jsonp(travel);
  121. userModel.find(function(err, users) {
  122. if(err) res.send(500, err.message);
  123. res.status(200).jsonp(users);
  124. });
  125. });
  126. }else{
  127. userModel.find(function(err, users) {
  128. if(err) res.send(500, err.message);
  129. res.status(200).jsonp(users);
  130. });
  131. }
  132. });
  133. };
  134. exports.doUnfav = function(req, res) {
  135. var tokenuser;
  136. userModel.find({
  137. token: req.headers['x-access-token']
  138. }, function(err, users){
  139. tokenuser=users[0];
  140. });
  141. userModel.findById(req.params.userId, function(err, user){
  142. for(var i=0; i<user.favs.length; i++)
  143. {
  144. if(user.favs[i].username==tokenuser.username)
  145. {
  146. user.favs.splice(i, 1);
  147. }
  148. }
  149. user.save(function(err, travel) {
  150. if(err) return res.send(500, err.message);
  151. //res.status(200).jsonp(travel);
  152. userModel.find(function(err, users) {
  153. if(err) res.send(500, err.message);
  154. res.status(200).jsonp(users);
  155. });
  156. });
  157. });
  158. };
  159. //PUT - Update a user already exists
  160. exports.updateUser = function(req, res) {
  161. userModel.findById(req.params.id, function(err, user) {
  162. user.username = req.body.username;
  163. user.password = md5(req.body.password);
  164. user.description = req.body.description;
  165. user.avatar = req.body.avatar;
  166. user.mail = req.body.mail;
  167. user.phone = req.body.phone;
  168. user.telegram = req.body.telegram;
  169. user.save(function(err) {
  170. if(err) return res.send(500, err.message);
  171. user.password="";
  172. users.token="";
  173. res.status(200).jsonp(user);
  174. });
  175. });
  176. };
  177. //DELETE - Delete a user with specified ID
  178. exports.deleteUser = function(req, res) {
  179. userModel.findById(req.params.id, function(err, user) {
  180. user.remove(function(err) {
  181. if(err) return res.send(500, err.message);
  182. res.status(200).jsonp(req.params.id);
  183. console.log('DELETE /users/' + req.params.id);
  184. })
  185. });
  186. };
  187. //POST - auth user
  188. exports.login = function(req, res) {
  189. // find the user
  190. userModel.findOne({
  191. username: req.body.username
  192. }, function(err, user) {
  193. if (err) throw err;
  194. if (!user) {
  195. res.json({ success: false, message: 'Authentication failed. User not found.' });
  196. } else if (user) {
  197. req.body.password=md5(req.body.password);
  198. // check if password matches
  199. if (user.password != req.body.password) {
  200. res.json({ success: false, message: 'Authentication failed. Wrong password.' });
  201. } else {
  202. // if user is found and password is right
  203. // create a token
  204. var token = jwt.sign(user, app.get('superSecret'), {
  205. //expiresInMinutes: 1440 // expires in 24 hours
  206. //expiresIn: '60m'
  207. });
  208. user.token=token;
  209. user.save(function(err, user) {
  210. if(err) return res.send(500, err.message);
  211. //res.status(200).jsonp(travel);
  212. console.log(user);
  213. // return the information including token as JSON
  214. user.password="";
  215. res.json({
  216. success: true,
  217. message: 'Enjoy your token!',
  218. token: token,
  219. avatar: user.avatar,
  220. userid: user._id,
  221. userdata: user
  222. });
  223. });
  224. }
  225. }
  226. });
  227. };