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.

142 lines
3.6 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. console.log('GET /users');
  16. res.status(200).jsonp(users);
  17. });
  18. };
  19. //GET - Return a User with specified ID
  20. exports.findById = function(req, res) {
  21. userModel.findById(req.params.id, function(err, user) {
  22. if(err) return res.send(500, err.message);
  23. console.log('GET /users/' + req.params.id);
  24. res.status(200).jsonp(user);
  25. });
  26. };
  27. exports.findUserByUsername = function(req, res) {
  28. userModel.find({
  29. username: req.params.username
  30. }, function(err, user) {
  31. if (err) throw err;
  32. if (!user) {
  33. res.json({ success: false, message: 'no user found' });
  34. } else if (user) {
  35. console.log(user);
  36. // return the information including token as JSON
  37. //res.jsonp(user);
  38. res.status(200).jsonp(user[0]);
  39. }
  40. });
  41. };
  42. //POST - Insert a new User in the DB
  43. exports.addUser = function(req, res) {
  44. console.log('POST new user, name: ' + req.body.username);
  45. //console.log(req.body);
  46. var user = new userModel({
  47. username: req.body.username,
  48. password: req.body.password,
  49. description: req.body.description,
  50. avatar: req.body.avatar,
  51. mail: req.body.mail,
  52. phone: req.body.phone,
  53. telegram: req.body.telegram
  54. });
  55. user.save(function(err, user) {
  56. if(err) return res.send(500, err.message);
  57. res.status(200).jsonp(user);
  58. });
  59. };
  60. //PUT - Update a user already exists
  61. exports.updateUser = function(req, res) {
  62. userModel.findById(req.params.id, function(err, user) {
  63. user.username = req.body.username;
  64. user.password = req.body.password;
  65. user.description = req.body.description;
  66. user.avatar = req.body.avatar;
  67. user.mail = req.body.mail;
  68. user.phone = req.body.phone;
  69. user.telegram = req.body.telegram;
  70. user.save(function(err) {
  71. if(err) return res.send(500, err.message);
  72. res.status(200).jsonp(user);
  73. });
  74. });
  75. };
  76. //DELETE - Delete a user with specified ID
  77. exports.deleteUser = function(req, res) {
  78. userModel.findById(req.params.id, function(err, user) {
  79. user.remove(function(err) {
  80. if(err) return res.send(500, err.message);
  81. res.status(200).jsonp(req.params.id);
  82. console.log('DELETE /users/' + req.params.id);
  83. })
  84. });
  85. };
  86. //POST - auth user
  87. exports.login = function(req, res) {
  88. // find the user
  89. userModel.findOne({
  90. username: req.body.username
  91. }, function(err, user) {
  92. if (err) throw err;
  93. if (!user) {
  94. res.json({ success: false, message: 'Authentication failed. User not found.' });
  95. } else if (user) {
  96. // check if password matches
  97. if (user.password != req.body.password) {
  98. res.json({ success: false, message: 'Authentication failed. Wrong password.' });
  99. } else {
  100. // if user is found and password is right
  101. // create a token
  102. var token = jwt.sign(user, app.get('superSecret'), {
  103. //expiresInMinutes: 1440 // expires in 24 hours
  104. expiresIn: '60m'
  105. });
  106. console.log(user);
  107. // return the information including token as JSON
  108. res.json({
  109. success: true,
  110. message: 'Enjoy your token!',
  111. token: token,
  112. avatar: user.avatar
  113. });
  114. }
  115. }
  116. });
  117. };