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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /* global $, APP, config */
  2. import { toJid } from '../../../react/features/base/connection';
  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. },
  108. null
  109. );
  110. /**
  111. * Displays error message in 'finished' state which allows either to cancel
  112. * or retry.
  113. * @param error the key to the error to be displayed.
  114. * @param options the options to the error message (optional)
  115. */
  116. this.displayError = function(error, options) {
  117. const finishedState = connDialog.getState('finished');
  118. const errorMessageElem = finishedState.find('#errorMessage');
  119. let messageKey;
  120. if (error === JitsiConnectionErrors.PASSWORD_REQUIRED) {
  121. // this is a password required error, as login window was already
  122. // open once, this means username or password is wrong
  123. messageKey = 'dialog.incorrectPassword';
  124. } else {
  125. messageKey = 'dialog.connectErrorWithMsg';
  126. if (!options) {
  127. options = {};// eslint-disable-line no-param-reassign
  128. }
  129. options.msg = error;
  130. }
  131. errorMessageElem.attr('data-i18n', messageKey);
  132. APP.translation.translateElement($(errorMessageElem), options);
  133. connDialog.goToState('finished');
  134. };
  135. /**
  136. * Show message as connection status.
  137. * @param {string} messageKey the key to the message
  138. */
  139. this.displayConnectionStatus = function(messageKey) {
  140. const connectingState = connDialog.getState('connecting');
  141. const connectionStatus = connectingState.find('#connectionStatus');
  142. connectionStatus.attr('data-i18n', messageKey);
  143. APP.translation.translateElement($(connectionStatus));
  144. };
  145. /**
  146. * Closes LoginDialog.
  147. */
  148. this.close = function() {
  149. connDialog.close();
  150. };
  151. }
  152. export default {
  153. /**
  154. * Show new auth dialog for JitsiConnection.
  155. *
  156. * @param {function(jid, password)} successCallback
  157. * @param {function} [cancelCallback] callback to invoke if user canceled.
  158. *
  159. * @returns {LoginDialog}
  160. */
  161. showAuthDialog(successCallback, cancelCallback) {
  162. return new LoginDialog(successCallback, cancelCallback);
  163. },
  164. /**
  165. * Show notification that external auth is required (using provided url).
  166. * @param {string} url URL to use for external auth.
  167. * @param {function} callback callback to invoke when auth popup is closed.
  168. * @returns auth dialog
  169. */
  170. showExternalAuthDialog(url, callback) {
  171. const dialog = APP.UI.messageHandler.openCenteredPopup(
  172. url, 910, 660,
  173. // On closed
  174. callback
  175. );
  176. if (!dialog) {
  177. APP.UI.messageHandler.showWarning({
  178. descriptionKey: 'dialog.popupError',
  179. titleKey: 'dialog.popupErrorTitle'
  180. });
  181. }
  182. return dialog;
  183. },
  184. /**
  185. * Shows a notification that authentication is required to create the
  186. * conference, so the local participant should authenticate or wait for a
  187. * host.
  188. *
  189. * @param {string} room - The name of the conference.
  190. * @param {function} onAuthNow - The callback to invoke if the local
  191. * participant wants to authenticate.
  192. * @returns dialog
  193. */
  194. showAuthRequiredDialog(room, onAuthNow) {
  195. const msg = APP.translation.generateTranslationHTML(
  196. '[html]dialog.WaitForHostMsg',
  197. { room }
  198. );
  199. const buttonTxt = APP.translation.generateTranslationHTML(
  200. 'dialog.IamHost'
  201. );
  202. const buttons = [ {
  203. title: buttonTxt,
  204. value: 'authNow'
  205. } ];
  206. return APP.UI.messageHandler.openDialog(
  207. 'dialog.WaitingForHost',
  208. msg,
  209. true,
  210. buttons,
  211. (e, submitValue) => {
  212. // Do not close the dialog yet.
  213. e.preventDefault();
  214. // Open login popup.
  215. if (submitValue === 'authNow') {
  216. onAuthNow();
  217. }
  218. }
  219. );
  220. }
  221. };