website/models/admin-user.js
RedDuckss 26058d8fa9 Added linting
Added the linter ESLint, added a custom set of rules and fixed linting issues
2018-10-11 04:09:13 -04:00

60 lines
1.4 KiB
JavaScript

/*
admin-user.js -
file containing the model file for admin user
*/
const mongoose = require('mongoose');
const uniqueValidator = require('mongoose-unique-validator');
const bcrypt = require('bcrypt');
const adminUserSchema = new mongoose.Schema({
username: {
type: String,
required: [true, 'Username is required.'],
minlength: [1, 'Username must be at-least 1 character.'],
maxlength: [42, 'Username cannot be more than 42 characters long.'],
unique: true,
trim: true,
},
password: {
type: String,
required: [true, 'Password is required.'],
minlength: [7, 'Password must be at-least 7 characters.'],
maxlength: [256, 'Password cannot be more than 256 characters long.'],
trim: true,
}
});
adminUserSchema.plugin(uniqueValidator, {message: '{PATH} already in use.'});
adminUserSchema.pre('save', function(next) {
// only hash the password if it has been modified (or is new)
if (!this.isModified('password')) {
return next();
}
// Hash the password with 10 rounds.
bcrypt.hash(this.get('password'), 10, (err, hash) => {
if (err) {
return next(err);
}
this.set('password', hash);
next();
});
});
adminUserSchema.statics.findByUsername = function(username) {
return this.model('adminUser').findOne({
username
});
};
const adminUserModel = mongoose.model('adminUser', adminUserSchema);
module.exports = {
adminUserModel,
adminUserSchema
};