Created
December 6, 2016 19:56
-
-
Save mateodelnorte/8e9de1df8cab8ea12c9795c69f923d02 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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