Guest User

Untitled

a guest
Jan 8th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. // models/users.js
  2. const mongoose = require('mongoose');
  3. const Schema = mongoose.Schema;
  4. const bcrypt = require('bcryptjs');
  5. // const address = require('./address');
  6. // const address = mongoose.model('address','AdressSchema');
  7.  
  8. var userSchema = new Schema({
  9. username:{
  10. type: String,
  11. required: true,
  12. unique: false
  13. },
  14. email:{
  15. type: String,
  16. required: true,
  17. unique: false
  18. },
  19. password:{
  20. type: String,
  21. required: true,
  22. // minlength: 6
  23. },
  24. name:{
  25. type: String,
  26. require: true,
  27. // minlength: 3,
  28. uunique: false
  29. },
  30. role: {
  31. type: String
  32. },
  33. // address: {
  34. // city:{
  35. // type: String
  36. // },
  37. // state:{
  38. // type: String
  39. // },
  40. // mobNumber:{
  41. // type: Number
  42. // },
  43. // flatNumber:{
  44. // type: String
  45. // },
  46. // nearby:{
  47. // type: String
  48. // },
  49. // country:{
  50. // type: String
  51. // }
  52. // }
  53. address:{type: Schema.Types.ObjectId, ref:'AdressSchema'}
  54. });
  55.  
  56. module.exports = mongoose.model('Users',userSchema);
  57. // created for using this in our function
  58. const Users = mongoose.model('Users',userSchema);
  59.  
  60. // function to find user by id
  61. module.exports.getUserById = function(id,callback){
  62. Users.findById(id,callback);
  63. };
  64.  
  65. // function to find user by username
  66. module.exports.getUserByUsername = function(username,callback){
  67. const query = {username: username};
  68. Users.findOne(query,callback);
  69. };
  70.  
  71. // function to add user
  72. module.exports.addUser = function(newUser, callback){
  73. bcrypt.genSalt(10, (err,salt) => {
  74. bcrypt.hash(newUser.password, salt, (err, hash) => {
  75. if(err){
  76. console.log(err);
  77. }
  78. // after hash changed plain password into hashed password
  79. newUser.password = hash;
  80. newUser.save(callback);
  81. });
  82. });
  83. };
  84.  
  85. // function to compare passwordd
  86. module.exports.comparePassword = function(candidatePassword, hash,callback){
  87. console.log('candidatePassword-', candidatePassword);
  88. console.log('hashPassword-',hash);
  89. bcrypt.compare(candidatePassword,hash,(err,isMatch) =>{
  90. if(err){
  91. console.log(err);
  92. }
  93. callback(null,isMatch);
  94. });
  95. };
Add Comment
Please, Sign In to add comment