index.js
function User(db, username, opts) {
  if (typeof db !== 'object') { throw new TypeError('db must be an object'); }
  if (typeof username !== 'string') { throw new TypeError('username must be a string'); }

  opts = opts || {};
  if (typeof opts !== 'object') { throw new TypeError('opts must be an object'); }

  var realm = '_default';
  if (typeof opts.realm !== 'undefined') {
    if (typeof opts.realm !== 'string') { throw new TypeError('opts.realm must be a string'); }
    realm = opts.realm;
  }
  if (typeof opts.debug !== 'undefined' && typeof opts.debug !== 'boolean') { throw new TypeError('opts.debug must be a boolean'); }
  if (typeof opts.hide !== 'undefined' && typeof opts.hide !== 'boolean') { throw new TypeError('opts.hide must be a boolean'); }

  if (username.length < 2) { throw new Error('username must be at least 2 characters'); }
  if (username.length > 128) { throw new Error('username can not exceed 128 characters'); }
  if (realm.length < 1) { throw new Error('opts.realm must be at least 1 character'); }
  if (realm.length > 128) { throw new Error('opts.realm can not exceed 128 characters'); }

  this._db = db;
  this._username = username;
  this._realm = realm;

  this._debug = opts.debug || false;
  this._hide = opts.hide || false;

  // keys that should be mapped from the user object that is stored in the user db
  this._protectedDbKeys = {
    realm: true,
    username: true,
    password: true
  };

  // keys that can not be used in the user object that is stored in the user db
  this._illegalDbKeys = {
    _protectedDbKeys: true,
    _illegalDbKeys: true,
    _db: true,
    _debug: true,
    _hide: true
  };
}
function User(db, username, opts) {
  if (typeof db !== 'object') { throw new TypeError('db must be an object'); }
  if (typeof username !== 'string') { throw new TypeError('username must be a string'); }

  opts = opts || {};
  if (typeof opts !== 'object') { throw new TypeError('opts must be an object'); }

  var realm = '_default';
  if (typeof opts.realm !== 'undefined') {
    if (typeof opts.realm !== 'string') { throw new TypeError('opts.realm must be a string'); }
    realm = opts.realm;
  }
  if (typeof opts.debug !== 'undefined' && typeof opts.debug !== 'boolean') { throw new TypeError('opts.debug must be a boolean'); }
  if (typeof opts.hide !== 'undefined' && typeof opts.hide !== 'boolean') { throw new TypeError('opts.hide must be a boolean'); }

  if (username.length < 2) { throw new Error('username must be at least 2 characters'); }
  if (username.length > 128) { throw new Error('username can not exceed 128 characters'); }
  if (realm.length < 1) { throw new Error('opts.realm must be at least 1 character'); }
  if (realm.length > 128) { throw new Error('opts.realm can not exceed 128 characters'); }

  this._db = db;
  this._username = username;
  this._realm = realm;

  this._debug = opts.debug || false;
  this._hide = opts.hide || false;

  // keys that should be mapped from the user object that is stored in the user db
  this._protectedDbKeys = {
    realm: true,
    username: true,
    password: true
  };

  // keys that can not be used in the user object that is stored in the user db
  this._illegalDbKeys = {
    _protectedDbKeys: true,
    _illegalDbKeys: true,
    _db: true,
    _debug: true,
    _hide: true
  };
}
function User ( db , username , opts ) { if ( typeof db !== 'object' ) { throw new TypeError ( 'db must be an object' ) ; } if ( typeof username !== 'string' ) { throw new TypeError ( 'username must be a string' ) ; } opts = opts || { } ; if ( typeof opts !== 'object' ) { throw new TypeError ( 'opts must be an object' ) ; } var realm = '_default' ; if ( typeof opts . realm !== 'undefined' ) { if ( typeof opts . realm !== 'string' ) { throw new TypeError ( 'opts.realm must be a string' ) ; } realm = opts . realm ; } if ( typeof opts . debug !== 'undefined' && typeof opts . debug !== 'boolean' ) { throw new TypeError ( 'opts.debug must be a boolean' ) ; } if ( typeof opts . hide !== 'undefined' && typeof opts . hide !== 'boolean' ) { throw new TypeError ( 'opts.hide must be a boolean' ) ; } if ( username . length < 2 ) { throw new Error ( 'username must be at least 2 characters' ) ; } if ( username . length > 128 ) { throw new Error ( 'username can not exceed 128 characters' ) ; } if ( realm . length < 1 ) { throw new Error ( 'opts.realm must be at least 1 character' ) ; } if ( realm . length > 128 ) { throw new Error ( 'opts.realm can not exceed 128 characters' ) ; } this . _db = db ; this . _username = username ; this . _realm = realm ; this . _debug = opts . debug || false ; this . _hide = opts . hide || false ; // keys that should be mapped from the user object that is stored in the user db this . _protectedDbKeys = { realm : true , username : true , password : true } ; // keys that can not be used in the user object that is stored in the user db this . _illegalDbKeys = { _protectedDbKeys : true , _illegalDbKeys : true , _db : true , _debug : true , _hide : true } ; }
Create a new User object. Either for maintenance, verification or registration.
A user may be bound to a realm.

@param {Object} db  object that implements find, updateHash and insert methods
@param {String} username  the name of the user to bind this instance to
@param {Object} [opts]  object containing optional parameters

opts:
realm {String, default "_default"}  optional realm the user belongs to
debug {Boolean, default false} whether to do extra console logging or not
hide {Boolean, default false} whether to suppress errors or not (for testing)

Three functions db must support:
find should accept: lookup, callback
lookup {Object}:
realm {String}
username {String}
callback {Function} should call back with:
err {Object}     error object or null
user {Object}    user object

updateHash should accept: lookup, hash, callback
lookup {Object}:
realm {String}
username {String}
hash {String}      bcrypt hash
callback {Function} should call back with:
err {Object}     error object or null

insert should accept: user, callback
user {Object}:
realm {String}
username {String}
callback {Function} should call back with:
err {Object}     error object or null
Create a new User object . Either for maintenance verification or registration . A user may be bound to a realm .