muffin
Version:
The 21st century way of building websites
42 lines (31 loc) • 952 B
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _db = require('../utils/db');
var _utils = require('../utils');
var _bcryptjs = require('bcryptjs');
const userSchema = _db.goose.Schema({
_id: String,
password: String,
email: String
});
userSchema.pre('save', function (next) {
let user = this;
// Only hash & salt password field if it differs from the
// one within the DB
if (!user.isModified('password')) return next();
// Auto-generates a salt and hash
(0, _bcryptjs.hash)(this.password, 10, function (err, hash) {
if (err) (0, _utils.log)(err);
user.password = hash;
next();
});
});
userSchema.methods.tryPassword = function (candidate) {
// Compare candidate password to the one in the DB
return (0, _bcryptjs.compareSync)(candidate, this.password);
};
const model = _db.goose.model('User', userSchema);
exports.default = model;
module.exports = exports['default'];