Guest User

Untitled

a guest
Mar 4th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. const mongoose = require("mongoose");
  2. const Schema = mongoose.Schema;
  3. const bcrypt = require("bcrypt");
  4.  
  5. const userSchema = new Schema({
  6. firstName: String,
  7. lastName: String,
  8. email: String,
  9. password: String
  10. })
  11.  
  12. userSchema.pre('save', function(next) {
  13. let salt = bcrypt.genSaltSync(saltRounds);
  14. let hash = bcrypt.hashSync(this.password, salt);
  15. this.password = hash;
  16. next();
  17. });
  18.  
  19. userSchema.statics.login = (email,password) => {
  20. return this.findOne({ email }).exec()
  21. .then((user) => {
  22. if (!user) {
  23. return null;
  24. }
  25.  
  26. return bcrypt.compare(password, user.password)
  27. .then((comp)=> {
  28. let _user = user._doc;
  29. delete _user.password;
  30. _user.logged = comp;
  31. return _user;
  32. });
  33. });
  34. }
Add Comment
Please, Sign In to add comment