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.

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