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.9KB

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