user.js 1.0 KB

12345678910111213141516171819202122232425262728293031
  1. var SEA = require('./sea');
  2. var Gun = SEA.Gun;
  3. var then = require('./then');
  4. function User(root){
  5. this._ = {$: this};
  6. }
  7. User.prototype = (function(){ function F(){}; F.prototype = Gun.chain; return new F() }()) // Object.create polyfill
  8. User.prototype.constructor = User;
  9. // let's extend the gun chain with a `user` function.
  10. // only one user can be logged in at a time, per gun instance.
  11. Gun.chain.user = function(pub){
  12. var gun = this, root = gun.back(-1), user;
  13. if(pub){ return root.get('~'+pub) }
  14. if(user = root.back('user')){ return user }
  15. var root = (root._), at = root, uuid = at.opt.uuid || Gun.state.lex;
  16. (at = (user = at.user = gun.chain(new User))._).opt = {};
  17. at.opt.uuid = function(cb){
  18. var id = uuid(), pub = root.user;
  19. if(!pub || !(pub = pub.is) || !(pub = pub.pub)){ return id }
  20. id = id + '~' + pub + '.';
  21. if(cb && cb.call){ cb(null, id) }
  22. return id;
  23. }
  24. return user;
  25. }
  26. Gun.User = User;
  27. module.exports = User;