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.

116 lines
4.5 KiB

  1. var jwt = require('jsonwebtoken'); // used to create, sign, and verify tokens
  2. var express = require("express");
  3. var app = express();
  4. var config = require('../config'); // get our config file
  5. app.set('superSecret', config.secret); // secret variable
  6. var crypto = require('crypto');
  7. var mongoose = require('mongoose');
  8. var userModel = mongoose.model('userModel');
  9. var pageSize = config.pageSize;
  10. exports.signup = function (req, res) {
  11. var user = new userModel({
  12. username: req.body.username,
  13. password: crypto.createHash('sha256').update(req.body.password).digest('base64'),
  14. description: req.body.description,
  15. email: req.body.email
  16. });
  17. user.save(function (err, user) {
  18. if (err) return res.status(500).send(err.message);
  19. //res.status(200).jsonp(user); en comptes de retoranr la data del signup, fem el login directament
  20. console.log("signup fet correctament, redirigint al login internament automàtic");
  21. exports.login(req, res);
  22. });
  23. };
  24. exports.login = function (req, res) {
  25. userModel.findOne({
  26. email: req.body.email
  27. })
  28. .select('+password')
  29. .exec(function (err, user) {
  30. if (err) throw err;
  31. if (!user) {
  32. res.json({success: false, message: 'Authentication failed. User not found.'});
  33. } else if (user) {
  34. req.body.password = crypto.createHash('sha256').update(req.body.password).digest('base64');
  35. if (user.password != req.body.password) {
  36. res.json({success: false, message: 'Authentication failed. Wrong password.'});
  37. } else {
  38. var indexToken = -1;
  39. for (var i = 0; i < user.tokens.length; i++) {
  40. if (user.tokens[i].userAgent == req.body.userAgent) {
  41. indexToken = JSON.parse(JSON.stringify(i));//stringify i parse pq es faci una còpia de la variable i, enlloc de una referència
  42. }
  43. }
  44. console.log(indexToken);
  45. if (indexToken == -1) {//userAgent no exist
  46. var tokenGenerated = jwt.sign({foo: 'bar'}, app.get('superSecret'), {
  47. // expiresIn: 86400 // expires in 24 hours
  48. });
  49. var newToken = {
  50. userAgent: req.body.userAgent,
  51. token: tokenGenerated,
  52. os: req.body.os,
  53. browser: req.body.browser,
  54. device: req.body.device,
  55. os_version: req.body.os_version,
  56. browser_version: req.body.browser_version,
  57. ip: req.body.ip,
  58. lastLogin: Date()
  59. };
  60. user.tokens.push(newToken);
  61. } else {//userAgent already exist
  62. user.tokens[indexToken].token = "";
  63. var tokenGenerated = jwt.sign({foo: 'bar'}, app.get('superSecret'), {
  64. // expiresIn: 86400 // expires in 24 hours
  65. });
  66. user.tokens[indexToken].token = tokenGenerated;
  67. user.tokens[indexToken].ip = req.body.ip;
  68. user.tokens[indexToken].lastLogin = Date();
  69. }
  70. user.save(function (err, user) {
  71. if (err) return res.send(500, err.message);
  72. // return the information including token as JSON
  73. user.password = "";
  74. res.json({
  75. user: user,
  76. success: true,
  77. message: 'Enjoy your token!',
  78. token: tokenGenerated
  79. });
  80. });
  81. }
  82. }
  83. });
  84. };
  85. exports.getAllUsers = function(req, res) {
  86. userModel.find()
  87. .limit(Number(req.query.pageSize))
  88. .skip(pageSize * Number(req.query.page))
  89. .exec(function (err, users) {
  90. if (err) return res.send(500, err.message);
  91. res.status(200).jsonp(users);
  92. });
  93. };
  94. exports.getUserById = function (req, res) {
  95. userModel.findOne({_id: req.params.userid})
  96. .lean()
  97. .populate('events', 'title description img date')
  98. .exec(function (err, user) {
  99. if (err) return res.send(500, err.message);
  100. if (!user) {
  101. res.json({success: false, message: 'User not found.'});
  102. } else if (user) {
  103. res.status(200).jsonp(user);
  104. }
  105. });
  106. };