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

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