Guest User

Untitled

a guest
Dec 14th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. const mongoose = require("mongoose"),
  2. bcrypt = require("bcrypt");
  3.  
  4. mongoose.Promise = Promise; //!
  5. // userSchema = new mongoose.Schema({
  6. mongoose.connect(
  7. "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
  8. );
  9. mongoose.connection
  10. .once("open", () => {
  11. console.info("=================>mongoose connection open");
  12. })
  13. .on("error", error => {
  14. console.warn("!!!!!!!!!!!!!!!mongoose connection error", error);
  15. throw new Error(error);
  16. });
  17.  
  18. const UserSchema = new mongoose.Schema({
  19. email: {
  20. type: String,
  21. unique: true,
  22. lowercase: true,
  23. required: [true, "Email is required."]
  24. },
  25. profile: {
  26. name: { type: String, default: "" },
  27. picture: { type: String, default: "" }
  28. },
  29. address: String,
  30. history: [
  31. {
  32. date: Date,
  33. paid: { type: Number, default: 0 },
  34. item: { type: mongoose.Schema.Types.ObjectId }
  35. }
  36. ]
  37. });
  38.  
  39. UserSchema.pre("save", next => {
  40. const user = this;
  41. if (!user.isModified(`password`)) return next();
  42. bcrypt.genSalt(10, (error, salt) => {
  43. if (error) return next(error);
  44. bcrypt.hash(user.password, salt, null, (error, hash) => {
  45. //progress
  46. if (error) return next(error);
  47. user.password = hash;
  48. next();
  49. });
  50. });
  51. });
  52.  
  53. UserSchema.methods.comparePassword = password =>
  54. bcrypt.compare(password, this.password);
  55. const User = mongoose.model("User", UserSchema);
  56. module.exports = mongoose.model(`User`, UserSchema);
Add Comment
Please, Sign In to add comment