Advertisement
Guest User

Untitled

a guest
Mar 13th, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. var crypto = require('crypto');
  2.  
  3. var base_path = process.env.PWD;
  4.  
  5. var config = require(base_path + '/config.js');
  6. var mongoose = require("mongoose");
  7. var Schema = mongoose.Schema;
  8.  
  9.  
  10. // Load required packages
  11. var timestamps = require('mongoose-timestamp');
  12. var metadata = require('./plugins/metadata');
  13. var paginate = require('./plugins/paginate');
  14.  
  15. var _Schema = new Schema({
  16. username : { type : String, trim : true, lowercase : true, unique:true, required:true},
  17. password : {type: String, required : false},
  18. first_name : { type : String, trim : true, lowercase : true},
  19. last_name : { type : String, trim : true, lowercase : true},
  20. full_name : { type : String, trim : true, lowercase : true},
  21. email : { type : String, trim : true ,lowercase:true},
  22. role : { type : String, trim :true},
  23. resetPasswordToken: String,
  24. resetPasswordExpires: Date
  25. });
  26.  
  27. _Schema.statics.sort_by = "first_name -";
  28.  
  29. _Schema.pre('save', function (next) {
  30. this.full_name = (this.name || '') + ' ' + (this.last_name || '');
  31. next();
  32. });
  33.  
  34. _Schema.methods.auth = function(password, callback){
  35. var res = true;
  36. if(require('../helpers/crypto-util')(password) !== this.password){
  37. res = false;
  38. }
  39.  
  40. if(callback)
  41. callback(res);
  42. else
  43. return res;
  44. }
  45.  
  46. _Schema.statics.exists = function(email, callback){
  47. this.find({ email : email}, function(err, rs){
  48. callback(err, rs.length);
  49. })
  50. }
  51.  
  52. //add plugins
  53. _Schema.plugin(metadata);
  54. _Schema.plugin(paginate);
  55. _Schema.plugin(timestamps);
  56.  
  57. module.exports = mongoose.model('User', _Schema);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement