Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

LoginDialog.js 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. let 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. let loginButtons = [{
  45. title: APP.translation.generateTranslationHTML("dialog.Ok"),
  46. value: true
  47. }];
  48. let 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. titleKey: 'dialog.passwordRequired',
  60. html: getPasswordInputHtml(),
  61. buttons: loginButtons,
  62. focus: ':input:first',
  63. submit: function (e, v, m, f) {
  64. e.preventDefault();
  65. if (v) {
  66. let jid = f.username;
  67. let password = f.password;
  68. if (jid && password) {
  69. connDialog.goToState('connecting');
  70. successCallback(toJid(jid, config.hosts), password);
  71. }
  72. } else {
  73. // User cancelled
  74. cancelCallback();
  75. }
  76. }
  77. },
  78. connecting: {
  79. titleKey: 'dialog.connecting',
  80. html: '<div id="connectionStatus"></div>',
  81. buttons: [],
  82. defaultButton: 0
  83. },
  84. finished: {
  85. titleKey: 'dialog.error',
  86. html: '<div id="errorMessage"></div>',
  87. buttons: finishedButtons,
  88. defaultButton: 0,
  89. submit: function (e, v) {
  90. e.preventDefault();
  91. if (v === 'retry') {
  92. connDialog.goToState('login');
  93. } else {
  94. // User cancelled
  95. cancelCallback();
  96. }
  97. }
  98. }
  99. };
  100. var connDialog = APP.UI.messageHandler.openDialogWithStates(
  101. states, { persistent: true, closeText: '' }, null
  102. );
  103. /**
  104. * Displays error message in 'finished' state which allows either to cancel
  105. * or retry.
  106. * @param error the key to the error to be displayed.
  107. * @param options the options to the error message (optional)
  108. */
  109. this.displayError = function (error, options) {
  110. let finishedState = connDialog.getState('finished');
  111. let errorMessageElem = finishedState.find('#errorMessage');
  112. let messageKey;
  113. if (error === JitsiConnectionErrors.PASSWORD_REQUIRED) {
  114. // this is a password required error, as login window was already
  115. // open once, this means username or password is wrong
  116. messageKey = 'dialog.incorrectPassword';
  117. }
  118. else {
  119. messageKey = 'dialog.connectErrorWithMsg';
  120. if (!options)
  121. options = {};
  122. options.msg = error;
  123. }
  124. errorMessageElem.attr("data-i18n", messageKey);
  125. APP.translation.translateElement($(errorMessageElem), options);
  126. connDialog.goToState('finished');
  127. };
  128. /**
  129. * Show message as connection status.
  130. * @param {string} messageKey the key to the message
  131. */
  132. this.displayConnectionStatus = function (messageKey) {
  133. let connectingState = connDialog.getState('connecting');
  134. let connectionStatus = connectingState.find('#connectionStatus');
  135. connectionStatus.attr("data-i18n", messageKey);
  136. APP.translation.translateElement($(connectionStatus));
  137. };
  138. /**
  139. * Closes LoginDialog.
  140. */
  141. this.close = function () {
  142. connDialog.close();
  143. };
  144. }
  145. export default {
  146. /**
  147. * Show new auth dialog for JitsiConnection.
  148. *
  149. * @param {function(jid, password)} successCallback
  150. * @param {function} [cancelCallback] callback to invoke if user canceled.
  151. *
  152. * @returns {LoginDialog}
  153. */
  154. showAuthDialog: function (successCallback, cancelCallback) {
  155. return new LoginDialog(successCallback, cancelCallback);
  156. },
  157. /**
  158. * Show notification that external auth is required (using provided url).
  159. * @param {string} url URL to use for external auth.
  160. * @param {function} callback callback to invoke when auth popup is closed.
  161. * @returns auth dialog
  162. */
  163. showExternalAuthDialog: function (url, callback) {
  164. var dialog = APP.UI.messageHandler.openCenteredPopup(
  165. url, 910, 660,
  166. // On closed
  167. callback
  168. );
  169. if (!dialog) {
  170. APP.UI.messageHandler.openMessageDialog(null, "dialog.popupError");
  171. }
  172. return dialog;
  173. },
  174. /**
  175. * Shows a notification that authentication is required to create the
  176. * conference, so the local participant should authenticate or wait for a
  177. * host.
  178. *
  179. * @param {string} room - The name of the conference.
  180. * @param {function} onAuthNow - The callback to invoke if the local
  181. * participant wants to authenticate.
  182. * @returns dialog
  183. */
  184. showAuthRequiredDialog(room, onAuthNow) {
  185. const msg = APP.translation.generateTranslationHTML(
  186. '[html]dialog.WaitForHostMsg',
  187. { room }
  188. );
  189. const buttonTxt = APP.translation.generateTranslationHTML(
  190. 'dialog.IamHost'
  191. );
  192. const buttons = [{
  193. title: buttonTxt,
  194. value: 'authNow'
  195. }];
  196. return APP.UI.messageHandler.openDialog(
  197. 'dialog.WaitingForHost',
  198. msg,
  199. true,
  200. buttons,
  201. (e, submitValue) => {
  202. // Do not close the dialog yet.
  203. e.preventDefault();
  204. // Open login popup.
  205. if (submitValue === 'authNow') {
  206. onAuthNow();
  207. }
  208. }
  209. );
  210. }
  211. };