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.

190 lines
4.5 KiB

8 years ago
8 years ago
8 years ago
8 years ago
  1. //File: controllers/userController.js
  2. var mongoose = require('mongoose');
  3. var userModel = mongoose.model('userModel');
  4. /* */
  5. var jwt = require('jsonwebtoken'); // used to create, sign, and verify tokens
  6. var express = require("express");
  7. var app = express();
  8. var config = require('../config'); // get our config file
  9. app.set('superSecret', config.secret); // secret variable
  10. /* */
  11. //GET - Return all Users in the DB
  12. exports.findAllUsers = function(req, res) {
  13. userModel.find(function(err, users) {
  14. if(err) res.send(500, err.message);
  15. //password deletion
  16. for(var i=0; i<users.length; i++)
  17. {
  18. users[i].password="";
  19. console.log(users[i].password);
  20. }
  21. console.log('GET /users');
  22. res.status(200).jsonp(users);
  23. });
  24. };
  25. //GET - Return a User with specified ID
  26. exports.findById = function(req, res) {
  27. userModel.findById(req.params.id, function(err, user) {
  28. if(err) return res.send(500, err.message);
  29. console.log('GET /users/' + req.params.id);
  30. //password deletion
  31. user.password="";
  32. res.status(200).jsonp(user);
  33. });
  34. };
  35. exports.findUserByUsername = function(req, res) {
  36. userModel.find({
  37. username: req.params.username
  38. }, function(err, user) {
  39. if (err) throw err;
  40. if (!user) {
  41. res.json({ success: false, message: 'no user found' });
  42. } else if (user) {
  43. // return the information including token as JSON
  44. //res.jsonp(user);
  45. user.password="";
  46. console.log(user);
  47. res.status(200).jsonp(user[0]);
  48. }
  49. });
  50. };
  51. //POST - Insert a new User in the DB
  52. exports.addUser = function(req, res) {
  53. console.log('POST new user, name: ' + req.body.username);
  54. //console.log(req.body);
  55. var user = new userModel({
  56. username: req.body.username,
  57. password: req.body.password,
  58. description: req.body.description,
  59. mail: req.body.mail,
  60. avatar: req.body.avatar,
  61. github: req.body.github,
  62. web: req.body.web,
  63. projects: req.body.projects,
  64. connected: req.body.connected
  65. });
  66. user.save(function(err, user) {
  67. if(err) return res.send(500, err.message);
  68. res.status(200).jsonp(user);
  69. });
  70. };
  71. //PUT - Update a user already exists
  72. exports.updateUser = function(req, res) {
  73. userModel.findById(req.params.id, function(err, user) {
  74. user.username = req.body.username;
  75. user.password = req.body.password;
  76. user.description = req.body.description;
  77. user.avatar = req.body.avatar;
  78. user.mail = req.body.mail;
  79. user.phone = req.body.phone;
  80. user.telegram = req.body.telegram;
  81. user.save(function(err) {
  82. if(err) return res.send(500, err.message);
  83. user.password="";
  84. res.status(200).jsonp(user);
  85. });
  86. });
  87. };
  88. //DELETE - Delete a user with specified ID
  89. exports.deleteUser = function(req, res) {
  90. userModel.findById(req.params.id, function(err, user) {
  91. user.remove(function(err) {
  92. if(err) return res.send(500, err.message);
  93. res.status(200).jsonp(req.params.id);
  94. console.log('DELETE /users/' + req.params.id);
  95. })
  96. });
  97. };
  98. //POST - auth user
  99. exports.login = function(req, res) {
  100. // find the user
  101. userModel.findOne({
  102. username: req.body.username
  103. }, function(err, user) {
  104. if (err) throw err;
  105. if (!user) {
  106. res.json({ success: false, message: 'Authentication failed. User not found.' });
  107. } else if (user) {
  108. // check if password matches
  109. if (user.password != req.body.password) {
  110. res.json({ success: false, message: 'Authentication failed. Wrong password.' });
  111. } else {
  112. // if user is found and password is right
  113. // create a token
  114. var token = jwt.sign(user, app.get('superSecret'), {
  115. //expiresInMinutes: 1440 // expires in 24 hours
  116. expiresIn: '60m'
  117. });
  118. //console.log(user);
  119. //update connected=true
  120. user.connected= true;
  121. user.save(function(err) {
  122. if(err) return res.send(500, err.message);
  123. });
  124. // return the information including token as JSON
  125. res.json({
  126. success: true,
  127. message: 'Enjoy your token!',
  128. token: token,
  129. user: user
  130. });
  131. }
  132. }
  133. });
  134. };
  135. exports.logout = function(req, res) {
  136. // find the user
  137. userModel.findOne({
  138. username: req.body.username
  139. }, function(err, user) {
  140. if (err) throw err;
  141. if (!user) {
  142. res.json({ success: false, message: 'Authentication failed. User not found.' });
  143. } else if (user) {
  144. //update connected=true
  145. user.connected= false;
  146. user.save(function(err) {
  147. if(err) return res.send(500, err.message);
  148. });
  149. // return the information including token as JSON
  150. res.json({
  151. success: true,
  152. message: 'logged out'
  153. });
  154. }
  155. });
  156. };