Guest User

Untitled

a guest
Jan 15th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. const mongoose = require('mongoose');
  2. const bcrypt = require('bcryptjs');
  3. const config = require('../config/database');
  4.  
  5. // User schema
  6. const UserSchema = mongoose.Schema({
  7. name: {
  8. type: String
  9. },
  10. email: {
  11. type: String,
  12. required: true
  13. },
  14. username: {
  15. type: String,
  16. required: true
  17. },
  18. password: {
  19. type: String,
  20. required: true
  21. }
  22. });
  23.  
  24. const User = module.exports = mongoose.model('User', UserSchema);
  25.  
  26. module.exports.getUserById = function(id, callback){
  27. User.findById(id, callback);
  28. };
  29.  
  30. module.exports.getUserByUsername = function(username, callback){
  31. // This object needs to be created because findOnce needs an object
  32. const query = {username: username};
  33. User.findOne(query, callback);
  34. };
  35.  
  36. module.exports.addUser = function(newUser, callback){
  37. // Salt and Hash the password first!
  38. bcrypt.genSalt(10, function(err, salt){
  39. if(err) throw err;
  40. bcrypt.hash(newUser.password, salt, function(err, hash){
  41. if(err) throw err;
  42. newUser.password = hash;
  43. newUser.save(callback);
  44. });
  45. });
  46. };
Add Comment
Please, Sign In to add comment