You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LoginDialog.js 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /* global $, APP, config */
  2. import { toJid } from '../../../react/features/base/connection/functions';
  3. import {
  4. JitsiConnectionErrors
  5. } from '../../../react/features/base/lib-jitsi-meet';
  6. /**
  7. * Build html for "password required" dialog.
  8. * @returns {string} html string
  9. */
  10. function getPasswordInputHtml() {
  11. const placeholder = config.hosts.authdomain
  12. ? 'user identity'
  13. : 'user@domain.net';
  14. return `
  15. <input name="username" type="text"
  16. class="input-control"
  17. data-i18n="[placeholder]dialog.user"
  18. placeholder=${placeholder} autofocus>
  19. <input name="password" type="password"
  20. class="input-control"
  21. data-i18n="[placeholder]dialog.userPassword">`;
  22. }
  23. /**
  24. * Generate cancel button config for the dialog.
  25. * @returns {Object}
  26. */
  27. function cancelButton() {
  28. return {
  29. title: APP.translation.generateTranslationHTML('dialog.Cancel'),
  30. value: false
  31. };
  32. }
  33. /**
  34. * Auth dialog for JitsiConnection which supports retries.
  35. * If no cancelCallback provided then there will be
  36. * no cancel button on the dialog.
  37. *
  38. * @class LoginDialog
  39. * @constructor
  40. *
  41. * @param {function(jid, password)} successCallback
  42. * @param {function} [cancelCallback] callback to invoke if user canceled.
  43. */
  44. function LoginDialog(successCallback, cancelCallback) {
  45. const loginButtons = [ {
  46. title: APP.translation.generateTranslationHTML('dialog.Ok'),
  47. value: true
  48. } ];
  49. const finishedButtons = [ {
  50. title: APP.translation.generateTranslationHTML('dialog.retry'),
  51. value: 'retry'
  52. } ];
  53. // show "cancel" button only if cancelCallback provided
  54. if (cancelCallback) {
  55. loginButtons.push(cancelButton());
  56. finishedButtons.push(cancelButton());
  57. }
  58. const states = {
  59. login: {
  60. buttons: loginButtons,
  61. focus: ':input:first',
  62. html: getPasswordInputHtml(),
  63. titleKey: 'dialog.passwordRequired',
  64. submit(e, v, m, f) { // eslint-disable-line max-params
  65. e.preventDefault();
  66. if (v) {
  67. const jid = f.username;
  68. const password = f.password;
  69. if (jid && password) {
  70. // eslint-disable-next-line no-use-before-define
  71. connDialog.goToState('connecting');
  72. successCallback(toJid(jid, config.hosts), password);
  73. }
  74. } else {
  75. // User cancelled
  76. cancelCallback();
  77. }
  78. }
  79. },
  80. connecting: {
  81. buttons: [],
  82. defaultButton: 0,
  83. html: '<div id="connectionStatus"></div>',
  84. titleKey: 'dialog.connecting'
  85. },
  86. finished: {
  87. buttons: finishedButtons,
  88. defaultButton: 0,
  89. html: '<div id="errorMessage"></div>',
  90. titleKey: 'dialog.error',
  91. submit(e, v) {
  92. e.preventDefault();
  93. if (v === 'retry') {
  94. // eslint-disable-next-line no-use-before-define
  95. connDialog.goToState('login');
  96. } else {
  97. // User cancelled
  98. cancelCallback();
  99. }
  100. }
  101. }
  102. };
  103. const connDialog = APP.UI.messageHandler.openDialogWithStates(
  104. states,
  105. {
  106. closeText: '',
  107. persistent: true,
  108. zIndex: 1020
  109. },
  110. null
  111. );
  112. /**
  113. * Displays error message in 'finished' state which allows either to cancel
  114. * or retry.
  115. * @param error the key to the error to be displayed.
  116. * @param options the options to the error message (optional)
  117. */
  118. this.displayError = function(error, options) {
  119. const finishedState = connDialog.getState('finished');
  120. const errorMessageElem = finishedState.find('#errorMessage');
  121. let messageKey;
  122. if (error === JitsiConnectionErrors.PASSWORD_REQUIRED) {
  123. // this is a password required error, as login window was already
  124. // open once, this means username or password is wrong
  125. messageKey = 'dialog.incorrectPassword';
  126. } else {
  127. messageKey = 'dialog.connectErrorWithMsg';
  128. if (!options) {
  129. options = {};// eslint-disable-line no-param-reassign
  130. }
  131. options.msg = error;
  132. }
  133. errorMessageElem.attr('data-i18n', messageKey);
  134. APP.translation.translateElement($(errorMessageElem), options);
  135. connDialog.goToState('finished');
  136. };
  137. /**
  138. * Show message as connection status.
  139. * @param {string} messageKey the key to the message
  140. */
  141. this.displayConnectionStatus = function(messageKey) {
  142. const connectingState = connDialog.getState('connecting');
  143. const connectionStatus = connectingState.find('#connectionStatus');
  144. connectionStatus.attr('data-i18n', messageKey);
  145. APP.translation.translateElement($(connectionStatus));
  146. };
  147. /**
  148. * Closes LoginDialog.
  149. */
  150. this.close = function() {
  151. connDialog.close();
  152. };
  153. }
  154. export default {
  155. /**
  156. * Show new auth dialog for JitsiConnection.
  157. *
  158. * @param {function(jid, password)} successCallback
  159. * @param {function} [cancelCallback] callback to invoke if user canceled.
  160. *
  161. * @returns {LoginDialog}
  162. */
  163. showAuthDialog(successCallback, cancelCallback) {
  164. return new LoginDialog(successCallback, cancelCallback);
  165. },
  166. /**
  167. * Show notification that external auth is required (using provided url).
  168. * @param {string} url URL to use for external auth.
  169. * @param {function} callback callback to invoke when auth popup is closed.
  170. * @returns auth dialog
  171. */
  172. showExternalAuthDialog(url, callback) {
  173. const dialog = APP.UI.messageHandler.openCenteredPopup(
  174. url, 910, 660,
  175. // On closed
  176. callback
  177. );
  178. if (!dialog) {
  179. APP.UI.messageHandler.showWarning({
  180. descriptionKey: 'dialog.popupError',
  181. titleKey: 'dialog.popupErrorTitle'
  182. });
  183. }
  184. return dialog;
  185. }
  186. };