Advertisement
Guest User

Untitled

a guest
Apr 16th, 2018
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. const mongoose = require('mongoose');
  2. const bcrypt = require('bcryptjs');
  3.  
  4. // You must write a userSchema for a new user account,
  5. // in a source code file named "user.js"
  6. const userSchema = require('./models/user.js');
  7.  
  8. module.exports = function (mongoDBUserConnectionString) {
  9.  
  10. let User;
  11.  
  12. return {
  13. connect: function () {
  14. return new Promise(function (resolve, reject) {
  15. let db = mongoose.createConnection(mongoDBUserConnectionString);
  16.  
  17. db.on('error', (err) => {
  18. reject(err); // reject the promise with the provided error
  19. });
  20.  
  21. db.once('open', () => {
  22. User = db.model("users", userSchema);
  23. resolve();
  24. });
  25. });
  26. },
  27. registerUser: function (userData) {
  28. return new Promise(function (resolve, reject) {
  29.  
  30. if (userData.password != userData.password2) {
  31. reject("Passwords do not match");
  32. } else {
  33.  
  34. // Generate a "salt" using 10 rounds
  35. bcrypt.genSalt(10, function (err, salt) {
  36. if (err) {
  37. reject("There was an error encrypting the password");
  38. } else {
  39.  
  40. // Encrypt the password: userData.password
  41. bcrypt.hash(userData.password, salt, function (err, hash) {
  42.  
  43. if (err) {
  44. reject("There was an error encrypting the password");
  45. } else {
  46.  
  47. userData.password = hash;
  48.  
  49. var newUser = new User(userData);
  50.  
  51. newUser.save((err) => {
  52. if (err) {
  53. if (err.code == 11000) {
  54. reject("User Name already taken");
  55. } else {
  56. reject("There was an error creating the user: " + err);
  57. }
  58.  
  59. } else {
  60. resolve("User " + userData.userName + " successfully registered");
  61. }
  62. });
  63. }
  64. });
  65. }
  66. });
  67. }
  68. });
  69. },
  70. chekUser: function (userData) {
  71. return new Promise(function (resolve, reject) {
  72.  
  73. User.find({ userName: userData.userName })
  74. .exec()
  75. .then((users) => {
  76.  
  77. if (users.length == 0) {
  78. reject("Unable to find user " + userData.userName);
  79. } else {
  80. bcrypt.compare(userData.password, users[0].password).then((res) => {
  81. if (res === true) {
  82. resolve(users[0]);
  83. } else {
  84. reject("Incorrect password for user " + userData.userName);
  85. }
  86. });
  87. }
  88. }).catch((err) => {
  89. reject("Unable to find user " + userData.userName);
  90. });
  91. });
  92. },
  93. findUserById: function (userId) {
  94. return new Promise(function (resolve, reject) {
  95.  
  96. User.find({ _id: userId })
  97. .exec()
  98. .then((users) => {
  99. if (users.length == 0) {
  100. reject("Unable to find user with id " + userId);
  101. } else {
  102. resolve(users[0]);
  103. }
  104. }).catch((err) => {
  105. reject("Unable to find user with id " + userId);
  106. });
  107. });
  108. }
  109. };
  110.  
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement