Skip to content

Instantly share code, notes, and snippets.

@mateodelnorte
Created December 6, 2016 19:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mateodelnorte/8e9de1df8cab8ea12c9795c69f923d02 to your computer and use it in GitHub Desktop.
Save mateodelnorte/8e9de1df8cab8ea12c9795c69f923d02 to your computer and use it in GitHub Desktop.
import mongoose from '../mongo';
import bcrypt from 'bcrypt';
var userSchema = mongoose.Schema({
email: { type: String, index: true },
fullName: String,
isAdmin: { type: Boolean, index: true },
password: { type: String, index: true }
});
userSchema.statics.findAdminByEmail = function (email, cb) {
return this.findOne({ email: email, isAdmin: true }, cb);
}
userSchema.statics.findAdmins = function (cb) {
return this.find({ isAdmin: true }, cb);
}
userSchema.statics.findAll = function (cb) {
return this.find({ isAdmin: { $ne: true } })
.sort('-placeInQueue')
.sort('-accepted')
.exec(cb);
}
userSchema.statics.findByEmail = function (email, cb) {
return this.findOne({ email: email, isAdmin: undefined }, cb);
}
userSchema.statics.generateHash = (password) => {
return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
};
userSchema.statics.isPasswordValid = (user, password) => {
return bcrypt.compareSync(password, user.password);
};
export default mongoose.model('User', userSchema);
var config = require('cconfig')();
var mongoose = require('mongoose');
var mongoUrl = config.MONGO_URI || 'mongodb://127.0.0.1:27017';
console.log(`initializing connection to mongo at ${config.MONGO_URI}`);
mongoose.connect(config.MONGO_URI);
mongoose.connection.on('error', function (err) {
console.error(`✗ MongoDB Connection Error. Please make sure MongoDB is running: ${err}`);
throw err;
});
module.exports = mongoose;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment