Advertisement
Guest User

Untitled

a guest
Nov 5th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. module.exports.getUserByUsername = function(username, callback){
  30.     const query = {username : username};
  31.     User.findOne(query,callback);
  32.  
  33. }
  34. module.exports.addUser = function(newUser, callback){
  35.     bcrypt.genSalt(10, (err, salt) => {
  36.         bcrypt.hash(newUser.password, salt, (err, hash) => {
  37.             if(err) throw err;
  38.             newUser.password = hash;
  39.             newUser.save(callback);
  40.         });
  41.     });
  42. }
  43.  
  44. module.exports.comparePassword= function(password, password_hashed, callback){
  45.     bcrypt.compare(password, password_hashed, (err, isMatch) =>{
  46.         if(err) throw err;
  47.         callback(null, isMatch);
  48.     });
  49. }
  50.  
  51.  
  52. /*{
  53.     "name":"John",
  54.     "email":"valafaf.com",
  55.     "username":"John",
  56.     "password":"1asd23",
  57. }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement