Advertisement
Guest User

Untitled

a guest
May 12th, 2016
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. var User = require('../model/user');
  2. var config = require('../config/database');
  3. var jwt = require('jwt-simple');
  4. var btc = require('bitcoin-address');
  5. var smtp = require('../config/smtp');
  6. var functions = {
  7. authenticate: function (req, res) {
  8. User.findOne({
  9. email: req.body.email
  10. }, function (err, user) {
  11. if (err) throw err;
  12.  
  13. if (!user) {
  14. res.status(403).send({
  15. success: false,
  16. msg: 'Authentication failed, User not found'
  17. });
  18. } else {
  19. user.comparePassword(req.body.password, function (err, isMatch) {
  20. if (isMatch && !err) {
  21. var token = jwt.encode(user, config.secret);
  22. res.json({success: true, token: token});
  23. } else {
  24. return res.status(403).send({
  25. success: false,
  26. msg: 'Authenticaton failed, wrong password.'
  27. });
  28. }
  29. })
  30. }
  31.  
  32. })
  33. },
  34. addNew: function (req, res) {
  35. if (!req.body.email) {
  36. console.log(req.body.email);
  37. res.json({success: false, msg: 'Enter all values'});
  38. } else {
  39. var pass = make_passwd(13, 'qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890');
  40. var newUser;
  41. if (btc.validate(req.body.btc_address)) {
  42. newUser = User({
  43. email: req.body.email,
  44. password: pass,
  45. btc_address: req.body.btc_address
  46. });
  47. } else {
  48. newUser = User({
  49. email: req.body.email,
  50. password: pass,
  51. btc_address: ''
  52. });
  53. }
  54. console.log(pass);
  55. smtp.sendMail({
  56. from: "Sender Name <stefan@plasticjam.com>",
  57. to: "Recipient Name <"+ req.body.email+">",
  58. subject: "Your password",
  59. text: "Here is your password: " + pass
  60. }, function(error, response){
  61. if(error){
  62. console.log(error);
  63. }else{
  64. console.log("Message sent: " + response.message);
  65. }
  66. });
  67. newUser.save(function (err) {
  68. if (err) {
  69. console.log(err);
  70. res.json({success: false, msg: 'Failed to save'})
  71. } else {
  72. res.json({success: true, msg: 'Successfully saved'});
  73. }
  74. })
  75. }
  76. },
  77. getinfo: function (req, res) {
  78. if (req.headers.authorization && req.headers.authorization.split(' ')[0] === 'Bearer') {
  79. var token = req.headers.authorization.split(' ')[1];
  80. var decodedtoken = jwt.decode(token, config.secret);
  81. return res.json({
  82. success: true,
  83. email: decodedtoken.email,
  84. btc_address: decodedtoken.btc_address
  85. });
  86. }
  87. else {
  88. return res.json({success: false, msg: 'No header'});
  89. }
  90. }
  91. };
  92. var make_passwd = function (n, a) {
  93. var index = (Math.random() * (a.length - 1)).toFixed(0);
  94. return n > 0 ? a[index] + make_passwd(n - 1, a) : '';
  95. }
  96. module.exports = functions;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement