loginform.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import { html } from 'lit';
  2. import { _converse, api, constants } from '@converse/headless';
  3. import tplSpinner from 'templates/spinner.js';
  4. import { CONNECTION_STATUS_CSS_CLASS } from '../constants.js';
  5. import { __ } from 'i18n';
  6. import 'shared/components/brand-heading.js';
  7. import 'shared/components/footer.js';
  8. const { ANONYMOUS, EXTERNAL, LOGIN, PREBIND, CONNECTION_STATUS } = constants;
  9. /**
  10. * @param {boolean} checked
  11. */
  12. function tplTrustCheckbox(checked) {
  13. const i18n_hint_trusted = __(
  14. 'To improve performance, we cache your data in this browser. ' +
  15. 'Uncheck this box if this is a public computer or if you want your data to be deleted when you log out. ' +
  16. "It's important that you explicitly log out, otherwise not all cached data might be deleted. " +
  17. 'Please note, when using an untrusted device, OMEMO encryption is NOT available.'
  18. );
  19. const i18n_trusted = __('This is a trusted device');
  20. return html`
  21. <div class="form-check mb-2 login-trusted">
  22. <input
  23. id="converse-login-trusted"
  24. type="checkbox"
  25. class="form-check-input"
  26. name="trusted"
  27. ?checked=${checked}
  28. />
  29. <label for="converse-login-trusted" class="form-check-label login-trusted__desc">${i18n_trusted}</label>
  30. <converse-popover title="${__('Info')}" text="${i18n_hint_trusted}"></converse-popover>
  31. </div>
  32. `;
  33. }
  34. export function tplConnectionURLInput() {
  35. const i18n_connection_url = __('Connection URL');
  36. const i18n_form_help = __('HTTP or websocket URL that is used to connect to your XMPP server');
  37. const i18n_placeholder = __('e.g. wss://example.org/xmpp-websocket');
  38. return html`
  39. <div class="mb-3 fade-in">
  40. <label for="converse-conn-url" class="form-label">${i18n_connection_url}</label>
  41. <p class="form-help instructions">${i18n_form_help}</p>
  42. <input
  43. required
  44. id="converse-conn-url"
  45. class="form-control"
  46. type="url"
  47. name="connection-url"
  48. placeholder="${i18n_placeholder}"
  49. />
  50. </div>
  51. `;
  52. }
  53. function tplPasswordInput() {
  54. const i18n_password = __('Password');
  55. return html`
  56. <div class="mb-3">
  57. <label for="converse-login-password" class="form-label">${i18n_password}</label>
  58. <input
  59. id="converse-login-password"
  60. class="form-control"
  61. required="required"
  62. value="${api.settings.get('password') ?? ''}"
  63. type="password"
  64. name="password"
  65. placeholder="${i18n_password}"
  66. />
  67. </div>
  68. `;
  69. }
  70. function tplRegisterLink() {
  71. const i18n_create_account = __('Create an account');
  72. const i18n_hint_no_account = __("Don't have a chat account?");
  73. return html`
  74. <div class="mt-3 text-center switch-form">
  75. <p class="mb-1">${i18n_hint_no_account}</p>
  76. <a class="register-account toggle-register-login" href="#converse/register">${i18n_create_account}</a>
  77. </div>
  78. `;
  79. }
  80. function tplShowRegisterLink() {
  81. return (
  82. api.settings.get('allow_registration') &&
  83. !api.settings.get('auto_login') &&
  84. _converse.pluggable.plugins['converse-register'].enabled(_converse)
  85. );
  86. }
  87. function tplAuthFields() {
  88. const authentication = api.settings.get('authentication');
  89. const i18n_login = __('Log in');
  90. const i18n_xmpp_address = __('XMPP Address');
  91. const locked_domain = api.settings.get('locked_domain');
  92. const default_domain = api.settings.get('default_domain');
  93. const placeholder_username = ((locked_domain || default_domain) && __('Username')) || __('user@domain');
  94. const show_trust_checkbox = api.settings.get('allow_user_trust_override');
  95. return html`
  96. <div class="mb-3">
  97. <label for="converse-login-jid" class="form-label">${i18n_xmpp_address}:</label>
  98. <input
  99. id="converse-login-jid"
  100. ?autofocus=${api.settings.get('auto_focus') ? true : false}
  101. value="${api.settings.get('jid') ?? ''}"
  102. required
  103. class="form-control"
  104. type="text"
  105. name="jid"
  106. placeholder="${placeholder_username}"
  107. />
  108. </div>
  109. ${authentication !== EXTERNAL ? tplPasswordInput() : ''}
  110. ${api.settings.get('show_connection_url_input') ? tplConnectionURLInput() : ''}
  111. ${show_trust_checkbox ? tplTrustCheckbox(show_trust_checkbox === 'off' ? false : true) : ''}
  112. <div class="text-center mb-3">
  113. <button class="btn btn-primary px-5 mx-auto" type="submit">${i18n_login}</button>
  114. </div>
  115. ${tplShowRegisterLink() ? tplRegisterLink() : ''}
  116. `;
  117. }
  118. function tplFormFields() {
  119. const authentication = api.settings.get('authentication');
  120. const i18n_disconnected = __('Disconnected');
  121. const i18n_anon_login = __('Click here to log in anonymously');
  122. return html`
  123. ${authentication == LOGIN || authentication == EXTERNAL ? tplAuthFields() : ''}
  124. ${authentication == ANONYMOUS
  125. ? html`<div class="text-center mb-3">
  126. <button class="btn btn-primary login-anon px-5 mx-auto" type="submit">${i18n_anon_login}</button>
  127. </div>`
  128. : ''}
  129. ${authentication == PREBIND ? html`<p class="alert alert-warning">${i18n_disconnected}</p>` : ''}
  130. `;
  131. }
  132. /**
  133. * @param {import('../loginform.js').default} el
  134. */
  135. export default (el) => {
  136. const { connfeedback } = _converse.state;
  137. const connection_status = connfeedback.get('connection_status');
  138. const feedback_class = CONNECTION_STATUS_CSS_CLASS?.[connection_status] ?? 'none';
  139. const conn_feedback_message = connfeedback.get('message');
  140. return html`<form id="converse-login" class="converse-form rounded" method="post" @submit=${el.onLoginFormSubmitted}>
  141. <div
  142. class="alert ${`alert-${feedback_class}`} conn-feedback mb-3 ${!conn_feedback_message ? 'd-none' : ''}"
  143. role="alert"
  144. >
  145. <span class="feedback-message">${conn_feedback_message}</span>
  146. </div>
  147. ${['CONNECTED', 'CONNECTING', 'AUTHENTICATING', 'RECONNECTING'].includes(CONNECTION_STATUS[connection_status])
  148. ? html`<div class="text-center my-3">${tplSpinner()}</div>`
  149. : tplFormFields()}
  150. </form>`;
  151. };