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.

248 lines
8.4 KiB

  1. //File: controllers/userController.js
  2. var mongoose = require('mongoose');
  3. var adminModel = mongoose.model('adminModel');
  4. var userModel = mongoose.model('userModel');
  5. var travelModel = mongoose.model('travelModel');
  6. var config = require('../config');
  7. var pageSize = config.pageSize;
  8. /* */
  9. var jwt = require('jsonwebtoken'); // used to create, sign, and verify tokens
  10. var express = require("express");
  11. var app = express();
  12. var config = require('../config'); // get our config file
  13. app.set('superSecret', config.secret); // secret variable
  14. var crypto = require('crypto');
  15. /* */
  16. var request = require('request');
  17. //POST - Insert a new User in the DB
  18. exports.signup = function(req, res) {
  19. //get random avatar
  20. var r = getRand(1, 10);
  21. randAvatar = getAvatar(r);
  22. var user = new userModel({
  23. username: req.body.username,
  24. password: crypto.createHash('sha256').update(req.body.password).digest('base64'),
  25. description: req.body.description,
  26. avatar: randAvatar,
  27. email: req.body.email,
  28. phone: req.body.phone,
  29. telegram: req.body.telegram
  30. });
  31. if (user.username == undefined) {
  32. return res.status(500).jsonp("empty inputs");
  33. } else if (user.password == undefined) {
  34. return res.status(500).jsonp("empty inputs");
  35. } else if (user.email == undefined) {
  36. return res.status(500).jsonp("empty inputs");
  37. }
  38. user.save(function(err, user) {
  39. if (err) return res.send(500, err.message);
  40. exports.login(req, res);
  41. });
  42. };
  43. //POST - auth user
  44. exports.login = function(req, res) {
  45. // find the user
  46. adminModel.findOne({
  47. username: req.body.username
  48. })
  49. .select('+password')
  50. .exec(function(err, user) {
  51. if (err) throw err;
  52. if (!user) {
  53. res.json({
  54. success: false,
  55. message: 'Authentication failed. User not found.'
  56. });
  57. } else if (user) {
  58. req.body.password = crypto.createHash('sha256').update(req.body.password).digest('base64');
  59. // check if password matches
  60. if (user.password != req.body.password) {
  61. res.json({
  62. success: false,
  63. message: 'Authentication failed. Wrong password.'
  64. });
  65. } else {
  66. // if user is found and password is right
  67. // create a token
  68. var token = jwt.sign({
  69. foo: 'bar'
  70. }, app.get('superSecret'), {
  71. //expiresInMinutes: 1440 // expires in 24 hours
  72. //expiresIn: '60m'
  73. });
  74. user.token = token;
  75. user.save(function(err, user) {
  76. if (err) return res.send(500, err.message);
  77. //res.status(200).jsonp(travel);
  78. console.log(user);
  79. // return the information including token as JSON
  80. user.password = "";
  81. res.json({
  82. success: true,
  83. message: 'Enjoy your token!',
  84. token: token,
  85. user: user
  86. });
  87. });
  88. }
  89. }
  90. });
  91. };
  92. exports.changePassword = function(req, res) {
  93. //if(req.body.)
  94. userModel.update({
  95. 'token': req.headers['x-access-token']
  96. }, req.body,
  97. function(err) {
  98. if (err) return console.log(err);
  99. exports.getUserByToken(req, res);
  100. });
  101. };
  102. function isNodeInNodes(node, nodes){
  103. for (var i=0; i<nodes.length; i++){
  104. if (node.title==nodes[i].title){
  105. return(i);
  106. }
  107. }
  108. return(-1);
  109. }
  110. exports.network = function(req, res) {
  111. userModel.find()
  112. .limit(pageSize)
  113. .skip(pageSize * Number(req.query.page))
  114. .lean()
  115. //.populate({path: 'travels', populate: {path: 'joins', populate: {path: 'username'}}})
  116. .populate('travels', 'title type joins')
  117. .populate('likes', 'username avatar')
  118. .exec(function(err, users) {
  119. if (err) return res.send(500, err.message);
  120. /*res.status(200).jsonp(users);*/
  121. var nodes=[];
  122. var edges=[];
  123. for (var i=0; i<users.length; i++){
  124. var node = {
  125. title: users[i].username,
  126. label: users[i].username,
  127. image: users[i].avatar,
  128. shape: "image",
  129. id: users[i]._id,
  130. group: users[i]._id
  131. };
  132. var lNode = isNodeInNodes(node, nodes);
  133. if (lNode<0){
  134. nodes.push(node);
  135. var uNode = nodes.length -1;
  136. }
  137. for(var j=0; j<users[i].likes.length; j++){
  138. /*console.log(i + ", " + j);
  139. console.log(nodes);*/
  140. var node = {
  141. title: users[i].likes[j].username,
  142. label: users[i].likes[j].username,
  143. image: users[i].likes[j].avatar,
  144. shape: "image",
  145. id: users[i].likes[j]._id
  146. };
  147. var lNode = isNodeInNodes(node, nodes);
  148. if (lNode<0){
  149. //node no exist
  150. nodes.push(node);
  151. lNode = nodes.length -1;
  152. }else{
  153. //node already exist
  154. }
  155. var edge={
  156. from: users[i]._id,
  157. to: users[i].likes[j]._id,
  158. arrows: "to",
  159. color: {
  160. color: "#E57373"//red300
  161. }
  162. };
  163. edges.push(edge);
  164. }
  165. for(var j=0; j<users[i].travels.length; j++){
  166. /*console.log(i + ", " + j);
  167. console.log(nodes);*/
  168. var node = {
  169. title: users[i].travels[j].title,
  170. label: users[i].travels[j].title,
  171. image: "img/" + users[i].travels[j].type + ".png",
  172. shape: "image",
  173. id: users[i].travels[j]._id,
  174. value: "0.5",
  175. group: users[i]._id
  176. };
  177. var lNode = isNodeInNodes(node, nodes);
  178. if (lNode<0){
  179. //node no exist
  180. nodes.push(node);
  181. lNode = nodes.length -1;
  182. }else{
  183. //node already exist
  184. }
  185. var edge={
  186. from: users[i]._id,
  187. to: users[i].travels[j]._id
  188. };
  189. edges.push(edge);
  190. //users joining travels
  191. /*for(var k=0; k<users[i].travels[j].joins.length; k++){
  192. var node = {
  193. title: users[i].travels[j].joins[k].username,
  194. label: users[i].travels[j].joins[k].username,
  195. image: users[i].travels[j].joins[k].avatar,
  196. shape: "image",
  197. id: users[i].travels[j].joins[k]._id
  198. };
  199. var lNode = isNodeInNodes(node, nodes);
  200. if (lNode<0){
  201. //node no exist
  202. nodes.push(node);
  203. lNode = nodes.length -1;
  204. }
  205. var edge={
  206. from: users[i].travels[j].joins[k]._id,
  207. to: users[i].travels[j]._id,
  208. color: {
  209. color: "#4DD0E1"//cyan300
  210. }
  211. };
  212. edges.push(edge);
  213. }*/
  214. }
  215. }
  216. var resp = {
  217. nodes: nodes,
  218. edges: edges
  219. };
  220. res.status(200).jsonp(resp);
  221. });
  222. };