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 7.3KB

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