Advertisement
Guest User

Untitled

a guest
Jan 31st, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. var mongoose = require('mongoose');
  2. var validate = require('mongoose-validator');
  3. var Schema = mongoose.Schema;
  4. var crypto = require('crypto');
  5. var utilities = require('../services/utilities');
  6.  
  7. var userSchema = new Schema({
  8. firstname: { type : String, trim : true },
  9. lastname: { type : String, trim : true },
  10. username: { type: String, required: true, unique: true, lowercase: true, trim : true, index : true },
  11. password: { type: String, validate: passwordValidator },
  12. email: { type : String, trim : true, validate: emailValidator },
  13. phone: { type : String, trim : true },
  14. address: { type : String, trim : true },
  15. groups: [{ type : Schema.Types.ObjectId, ref : 'Group' }],
  16. created_at: Date,
  17. updated_at: Date
  18. },
  19. {
  20. toObject: { virtuals: true },
  21. toJSON: { virtuals: true }
  22. });
  23.  
  24. userSchema.virtual('fullname').get(function () {
  25. return [this.firstname, this.lastname].filter(Boolean).join(' ');
  26. });
  27.  
  28. userSchema.pre('save', function (next) {
  29. if (this.password) {
  30. this.password = encrypt(this.password);
  31. }
  32. utilities.reviewDate(this);
  33. next();
  34. });
  35.  
  36. function encrypt(text) {
  37. return crypto.createHash("SHA512").update(text).digest("base64");
  38. }
  39.  
  40. var emailValidator = [
  41. validate({
  42. validator: 'isEmail',
  43. message: 'Please fill a valid email address'
  44. })
  45. ];
  46.  
  47. var passwordValidator = [
  48. validate({
  49. validator: 'isLength',
  50. arguments: [6, 50],
  51. message: 'Password should be between {ARGS[0]} and {ARGS[1]} characters'
  52. }),
  53. validate({
  54. validator: 'matches',
  55. arguments: /\d/,
  56. message: 'Password should contain numbers'
  57. }),
  58. validate({
  59. validator: 'matches',
  60. arguments: /[a-zA-Z]/,
  61. message: 'Password should contain letters'
  62. }),
  63. validate({
  64. validator: 'matches',
  65. arguments: /[A-Z]/,
  66. message: 'Password must contain one uppercase letter'
  67. }),
  68. validate({
  69. validator: 'matches',
  70. arguments: /[\!\@\#\$\%\^\&\*\(\)\_\+\.\,\;\:]/,
  71. message: 'Password should contain a special characters like !@#$%^&*()_+'
  72. })
  73. ];
  74.  
  75. module.exports = mongoose.model('User', userSchema);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement