user.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**
  2. * @module:shared.api.user
  3. */
  4. import _converse from '../_converse.js';
  5. import presence_api from './presence.js';
  6. import connection_api from '../connection/api.js';
  7. import { replacePromise } from '../../utils/session.js';
  8. import { attemptNonPreboundSession, setUserJID } from '../../utils/init.js';
  9. import { getOpenPromise } from '@converse/openpromise';
  10. import { user_settings_api } from '../settings/user/api.js';
  11. import { LOGOUT } from '../constants.js';
  12. const api = {
  13. /**
  14. * This grouping collects API functions related to the current logged in user.
  15. *
  16. * @namespace _converse.api.user
  17. * @memberOf _converse.api
  18. */
  19. user: {
  20. settings: user_settings_api,
  21. ...presence_api,
  22. /**
  23. * @method _converse.api.user.jid
  24. * @returns {string} The current user's full JID (Jabber ID)
  25. * @example _converse.api.user.jid())
  26. */
  27. jid () {
  28. return connection_api.get()?.jid;
  29. },
  30. /**
  31. * Logs the user in.
  32. *
  33. * If called without any parameters, Converse will try
  34. * to log the user in by calling the `prebind_url` or `credentials_url` depending
  35. * on whether prebinding is used or not.
  36. *
  37. * @method _converse.api.user.login
  38. * @param { string } [jid]
  39. * @param { string } [password]
  40. * @param { boolean } [automatic=false] - An internally used flag that indicates whether
  41. * this method was called automatically once the connection has been
  42. * initialized. It's used together with the `auto_login` configuration flag
  43. * to determine whether Converse should try to log the user in if it
  44. * fails to restore a previous auth'd session.
  45. * @returns { Promise<void> }
  46. */
  47. async login (jid, password, automatic=false) {
  48. const { api } = _converse;
  49. jid = jid || api.settings.get('jid');
  50. const connection = connection_api.init(jid);
  51. if (api.settings.get("connection_options")?.worker && (await connection.restoreWorkerSession())) {
  52. return;
  53. }
  54. if (jid) {
  55. jid = await setUserJID(jid);
  56. }
  57. /**
  58. * *Hook* which allows 3rd party code to attempt logging in before
  59. * the core code attempts it.
  60. *
  61. * Note: If the hook handler has logged the user in, it should set the
  62. * `success` flag on the payload to `true`.
  63. *
  64. * @typedef {Object} LoginHookPayload
  65. * @property {string} jid
  66. * @property {string} password
  67. * @property {boolean} [automatic] - An internally used flag that indicates whether
  68. * this method was called automatically once the connection has been initialized.
  69. * @property {boolean} [success] - A flag which indicates whether
  70. * login has succeeded. If a hook handler receives a payload with
  71. * this flag, it should NOT attempt to log in.
  72. * If a handler has successfully logged in, it should return the
  73. * payload with this flag set to true.
  74. *
  75. * @event _converse#login
  76. * @param {typeof api.user} context
  77. * @param {LoginHookPayload} payload
  78. */
  79. const { success } = await _converse.api.hook('login', this, { jid, password, automatic });
  80. if (success) return;
  81. password = password || api.settings.get("password");
  82. const credentials = (jid && password) ? { jid, password } : null;
  83. await attemptNonPreboundSession(credentials, automatic);
  84. },
  85. /**
  86. * Logs the user out of the current XMPP session.
  87. * @method _converse.api.user.logout
  88. * @example _converse.api.user.logout();
  89. */
  90. async logout () {
  91. const { api } = _converse;
  92. /**
  93. * Triggered before the user is logged out
  94. * @event _converse#beforeLogout
  95. */
  96. await api.trigger('beforeLogout', {'synchronous': true});
  97. const promise = getOpenPromise();
  98. const complete = () => {
  99. // Recreate all the promises
  100. Object.keys(_converse.promises).forEach((p) => replacePromise(_converse, p));
  101. // Remove the session JID, otherwise the user would just be logged
  102. // in again upon reload. See #2759
  103. localStorage.removeItem('conversejs-session-jid');
  104. /**
  105. * Triggered once the user has logged out.
  106. * @event _converse#logout
  107. */
  108. api.trigger('logout');
  109. promise.resolve();
  110. }
  111. const connection = connection_api.get();
  112. if (connection) {
  113. connection.setDisconnectionCause(LOGOUT, undefined, true);
  114. api.listen.once('disconnected', () => complete());
  115. connection.disconnect();
  116. } else {
  117. complete();
  118. }
  119. return promise;
  120. }
  121. }
  122. }
  123. export default api;