Advertisement
tty30

Untitled

Mar 31st, 2015
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var bcrypt = require('bcrypt-nodejs');
  2.  
  3. /* The UsersDAO must be constructed with a connected database object */
  4. function UsersDAO(db) {
  5.     "use strict";
  6.  
  7.     /* If this constructor is called without the "new" operator, "this" points
  8.      * to the global object. Log a warning and call it correctly. */
  9.     if (false === (this instanceof UsersDAO)) {
  10.         console.log('Warning: UsersDAO constructor called without "new" operator');
  11.         return new UsersDAO(db);
  12.     }
  13.  
  14.     var users = db.collection("users");
  15.  
  16.     this.addUser = function(username, password, email, callback) {
  17.         "use strict";
  18.  
  19.         // Generate password hash
  20.         var salt = bcrypt.genSaltSync();
  21.         var password_hash = bcrypt.hashSync(password, salt);
  22.  
  23.         // Create user document
  24.         var user = {'_id': username, 'password': password_hash};
  25.  
  26.         // Add email if set
  27.         if (email != "") {
  28.             user['email'] = email;
  29.         }
  30.  
  31.         users.insert(user, function (err, result) {
  32.             "use strict";
  33.  
  34.             if (!err) {
  35.                 console.log("Inserted new user");
  36.                 return callback(null, result[0]);
  37.             }
  38.  
  39.             return callback(err, null);
  40.         });
  41.     }
  42.  
  43.     this.validateLogin = function(username, password, callback) {
  44.         "use strict";
  45.  
  46.         // Callback to pass to MongoDB that validates a user document
  47.         function validateUserDoc(err, user) {
  48.             "use strict";
  49.  
  50.             if (err) return callback(err, null);
  51.  
  52.             if (user) {
  53.                 if (bcrypt.compareSync(password, user.password)) {
  54.                     callback(null, user);
  55.                 }
  56.                 else {
  57.                     var invalid_password_error = new Error("Invalid password");
  58.                     // Set an extra field so we can distinguish this from a db error
  59.                     invalid_password_error.invalid_password = true;
  60.                     callback(invalid_password_error, null);
  61.                 }
  62.             }
  63.             else {
  64.                 var no_such_user_error = new Error("User: " + user + " does not exist");
  65.                 // Set an extra field so we can distinguish this from a db error
  66.                 no_such_user_error.no_such_user = true;
  67.                 callback(no_such_user_error, null);
  68.             }
  69.         }
  70.  
  71.         users.findOne({ '_id' : username }, validateUserDoc);
  72.     }
  73. }
  74.  
  75. module.exports.UsersDAO = UsersDAO;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement