Guest User

Untitled

a guest
Jun 21st, 2018
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. //Importing modules
  2. var mongo = require("mongoose");
  3. var Schema = mongo.Schema;
  4. var bcrypt = require('bcrypt-nodejs');
  5.  
  6. var User = require("./User");
  7.  
  8. var UserSchema = new Schema({
  9. username: {
  10. type: String,
  11. required: true,
  12. unique: true
  13. },
  14. password: {
  15. type: String,
  16. required: true
  17. },
  18. email: {
  19. type: String,
  20. required: true
  21. },
  22. loginDate: {
  23. type: Date,
  24. default: Date.now
  25. }
  26. });
  27.  
  28. UserSchema.pre('save', function(next) {
  29. var user = this;
  30. // only hash the password if it has been modified (or is new)
  31. if (!user.isModified('password'))
  32. return next();
  33. // generate a salt
  34. bcrypt.genSalt(SALT_WORK_FACTOR, function(err, salt) {
  35. if (err)
  36. return next(err);
  37. // hash the password using our new salt
  38. bcrypt.hash(user.password, salt, function(err, hash) {
  39. if (err)
  40. return next(err);
  41. // override the cleartext password with the hashed one
  42. user.password = hash;
  43. next();
  44. });
  45. });
  46.  
  47. });
  48.  
  49. UserSchema.methods.comparePassword = function(candidatePassword, cb) {
  50. bcrypt.compare(candidatePassword, this.password, function(err, isMatch) {
  51. if (err)
  52. return cb(err);
  53. cb(null, isMatch);
  54. });
  55. };
  56.  
  57. module.exports = mongo.model("Login", UserSchema);
Add Comment
Please, Sign In to add comment