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.

156 lines
3.9 KiB

  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. if(user!=null){
  32. user.password="";
  33. }
  34. res.status(200).jsonp(user);
  35. });
  36. };
  37. exports.findUserByUsername = function(req, res) {
  38. userModel.find({
  39. username: req.params.username
  40. }, function(err, user) {
  41. if (err) throw err;
  42. if (!user) {
  43. res.json({ success: false, message: 'no user found' });
  44. } else if (user) {
  45. // return the information including token as JSON
  46. //res.jsonp(user);
  47. user.password="";
  48. console.log(user);
  49. res.status(200).jsonp(user[0]);
  50. }
  51. });
  52. };
  53. //POST - Insert a new User in the DB
  54. exports.addUser = function(req, res) {
  55. console.log('POST new user, name: ' + req.body.username);
  56. //console.log(req.body);
  57. var user = new userModel({
  58. username: req.body.username,
  59. password: req.body.password,
  60. description: req.body.description,
  61. avatar: req.body.avatar,
  62. mail: req.body.mail,
  63. phone: req.body.phone,
  64. telegram: req.body.telegram
  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. // return the information including token as JSON
  120. res.json({
  121. success: true,
  122. message: 'Enjoy your token!',
  123. token: token,
  124. avatar: user.avatar,
  125. userid: user._id
  126. });
  127. }
  128. }
  129. });
  130. };