Created
January 7, 2018 23:38
-
-
Save danthegoodman1/e37ede20b2e230448070888bda1aa70f 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
const mongoose = require('mongoose'); | |
const bcrypt = require('bcryptjs'); | |
const config = require('../config/database'); | |
// User schema | |
const UserSchema = mongoose.Schema({ | |
name: { | |
type: String | |
}, | |
email: { | |
type: String, | |
required: true | |
}, | |
username: { | |
type: String, | |
required: true | |
}, | |
password: { | |
type: String, | |
required: true | |
} | |
}); | |
const User = module.exports = mongoose.model('User', UserSchema); | |
module.exports.getUserById = function(id, callback){ | |
User.findById(id, callback); | |
}; | |
module.exports.getUserByUsername = function(username, callback){ | |
// This object needs to be created because findOnce needs an object | |
const query = {username: username}; | |
User.findOne(query, callback); | |
}; | |
module.exports.addUser = function(newUser, callback){ | |
// Salt and Hash the password first! | |
bcrypt.genSalt(10, function(err, salt){ | |
if(err) throw err; | |
bcrypt.hash(newUser.password, salt, function(err, hash){ | |
if(err) throw err; | |
newUser.password = hash; | |
newUser.save(callback); | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment