login.js 1.3 KB

123456789101112131415161718192021222324252627282930313233
  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._ = key.at.$._;
  9. user._.opt = opt;
  10. //user._.tag = tmp || user._.tag;
  11. // so that way we can use the credentials to encrypt/decrypt data
  12. // that is input/output through gun (see below)
  13. const pub = key.pub
  14. const priv = key.priv
  15. const epub = key.epub
  16. const epriv = key.epriv
  17. user._.is = user.is = {alias: alias, pub: pub};
  18. Object.assign(user._, { alias: alias, pub: pub, epub: epub, sea: { pub: pub, priv: priv, epub: epub, epriv: epriv } })
  19. //console.log("authorized", user._);
  20. // persist authentication
  21. //await authPersist(user._, key.proof, opts) // temporarily disabled
  22. // emit an auth event, useful for page redirects and stuff.
  23. try {
  24. gunRoot._.on('auth', user._)
  25. } catch (e) {
  26. console.log('Your \'auth\' callback crashed with:', e)
  27. }
  28. // returns success with the user data credentials.
  29. return user._
  30. }
  31. module.exports = finalizeLogin