Advertisement
Guest User

Untitled

a guest
May 2nd, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2. /*
  3.     Example of mongoose usage and example of User model
  4. */
  5.  
  6. let mongoose = require('mongoose');
  7. mongoose.Promise = global.Promise;
  8.  
  9. let host = '127.0.0.1';
  10. let port = 27017;
  11. let database = 'testdb';
  12. let url = `mongodb://${host}:${port}/${database}`;
  13. mongoose.connect(url);
  14.  
  15. let bcrypt = require('bcrypt');
  16.  
  17. let Schema = mongoose.Schema({
  18.     name: {
  19.         type: String,
  20.         required: true
  21.     },
  22.     email: {
  23.         type: String,
  24.         unique: true,
  25.         required: true
  26.     },
  27.     password: {
  28.         type: String,
  29.         required: true
  30.     }
  31. });
  32.  
  33. Schema.methods.comparePassword = function(password, cb) {
  34.     const user = this;
  35.  
  36.     bcrypt.compare(password, user.password, (err, isMatch) => {
  37.         if (err) {
  38.             throw err;
  39.         }
  40.  
  41.         if (!isMatch) {
  42.             console.log('Password incorrect');
  43.             return false;
  44.         }
  45.  
  46.         console.log('Password is correct');
  47.         return true;
  48.     })
  49. }
  50.  
  51. Schema.pre('save', function(next) {
  52.     const user = this;
  53.  
  54.     bcrypt.genSalt((err, salt) => {
  55.         if (err) {
  56.             throw err;
  57.         }
  58.  
  59.         bcrypt.hash(user.password, salt, (err, encrypted) => {
  60.             if (err) {
  61.                 throw err;
  62.             }
  63.  
  64.             user.password = encrypted;
  65.             next();
  66.         })
  67.     })
  68. })
  69.  
  70. let validateError = (err, cb) => {
  71.     if (err.name == 'MongoError' && err.code == 11000) {
  72.         console.log(`${err.name}[${err.code}]: ${err.message}`);
  73.     }
  74.     cb();
  75. }
  76.  
  77. let User = mongoose.model('User', Schema);
  78.  
  79. let testUser = new User({
  80.     name: 'Test User',
  81.     email: 'test.user3@localhost',
  82.     password: 'myPASSWoRd123'
  83. });
  84.  
  85. testUser.save((err, user) => {
  86.     if (err) {
  87.         return validateError(err, () => {
  88.             mongoose.connection.close();
  89.         });
  90.     }
  91.  
  92.     console.log(`User ${user.name} with email ${user.email} was saved successfully`);
  93.  
  94.     User.findOne({email: user.email}, (err, user) => {
  95.         if (err) {
  96.             return validateError(err, () => {
  97.                 mongoose.connection.close();
  98.             })
  99.         }
  100.  
  101.         user.comparePassword('myPASSWoRd1233');
  102.         mongoose.connection.close();
  103.     })
  104.    
  105. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement