Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var mongoose = require("mongoose");
- var Schema = mongoose.Schema;
- var constants = require("../config/constants");
- var bcrypt = require("bcrypt-nodejs");
- var UserSchema = new Schema({
- name: String,
- email: String,
- password: String,
- authorization:
- {
- type: Number,
- default: constants.authorization.default
- }
- });
- UserSchema.pre("save", (next) => {
- var user = this;
- /**
- * Only hash the password when it's been modified or if new.
- */
- // #####################################################
- // ERROR
- // if (!query.isModified("password"))
- // ^
- // TypeError: query.isModified is not a function
- //
- // user and this == {}
- // ####################################################
- if (!user.isModified("password"))
- {
- return next();
- }
- /**
- * hash password
- */
- bcrypt.hash(user.password, null, null, (err, hash) => {
- if (err)
- {
- return next(err);
- }
- user.password = hash;
- return next();
- });
- });
- // #####################################################
- // ERROR
- // user.verifyPassword(req.body.password, match => {
- // ^
- // TypeError: user.verifyPassword is not a function
- //
- // this == {}
- // ####################################################
- UserSchema.methods.verifyPassword = (reqPassword, callback) => {
- bcrypt.compare(reqPassword, this.password, (err, match) => {
- var e = null;
- var m = match;
- if (err)
- {
- e = err;
- m = false;
- }
- return callback(e, m);
- });
- };
- module.exports = mongoose.model("User", UserSchema);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement