login.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. const authPersist = require('./persist')
  2. // This internal func finalizes User authentication
  3. const finalizeLogin = async (alias, key, gunRoot, opts) => {
  4. const user = gunRoot._.user
  5. // add our credentials in-memory only to our root gun instance
  6. var tmp = user._.tag;
  7. var opt = user._.opt;
  8. user._ = gunRoot.get('~'+key.pub)._;
  9. user._.opt = opt;
  10. var tags = user._.tag;
  11. /*Object.values && Object.values(tmp).forEach(function(tag){
  12. // TODO: This is ugly & buggy code, it needs to be refactored & tested into a event "merge" utility.
  13. var t = tags[tag.tag];
  14. console.log("hm??", tag, t);
  15. if(!t){
  16. tags[tag.tag] = tag;
  17. return;
  18. }
  19. if(tag.last){
  20. tag.last.to = t.to;
  21. t.last = tag.last = t.last || tag.last;
  22. }
  23. t.to = tag.to;
  24. })*/
  25. //user._.tag = tmp || user._.tag;
  26. // so that way we can use the credentials to encrypt/decrypt data
  27. // that is input/output through gun (see below)
  28. const pub = key.pub
  29. const priv = key.priv
  30. const epub = key.epub
  31. const epriv = key.epriv
  32. user._.is = user.is = {alias: alias, pub: pub};
  33. Object.assign(user._, { alias: alias, pub: pub, epub: epub, sea: { pub: pub, priv: priv, epub: epub, epriv: epriv } })
  34. //console.log("authorized", user._);
  35. // persist authentication
  36. //await authPersist(user._, key.proof, opts) // temporarily disabled
  37. // emit an auth event, useful for page redirects and stuff.
  38. try {
  39. gunRoot._.on('auth', user._) // TODO: Deprecate this, emit on user instead! Update docs when you do.
  40. //user._.on('auth', user._) // Arrgh, this doesn't work without event "merge" code, but "merge" code causes stack overflow and crashes after logging in & trying to write data.
  41. } catch (e) {
  42. console.log('Your \'auth\' callback crashed with:', e)
  43. }
  44. // returns success with the user data credentials.
  45. return user._
  46. }
  47. module.exports = finalizeLogin