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

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