Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

LoginDialog.js 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* global $, APP, config*/
  2. var XMPP = require('../../xmpp/xmpp');
  3. var Moderator = require('../../xmpp/moderator');
  4. //FIXME: use LoginDialog to add retries to XMPP.connect method used when
  5. // anonymous domain is not enabled
  6. /**
  7. * Creates new <tt>Dialog</tt> instance.
  8. * @param callback <tt>function(Strophe.Connection, Strophe.Status)</tt> called
  9. * when we either fail to connect or succeed(check Strophe.Status).
  10. * @param obtainSession <tt>true</tt> if we want to send ConferenceIQ to Jicofo
  11. * in order to create session-id after the connection is established.
  12. * @constructor
  13. */
  14. function Dialog(callback, obtainSession) {
  15. var self = this;
  16. var stop = false;
  17. var connection = APP.xmpp.createConnection();
  18. var message = '<h2 data-i18n="dialog.passwordRequired">';
  19. message += APP.translation.translateString("dialog.passwordRequired");
  20. message += '</h2>' +
  21. '<input name="username" type="text" ';
  22. if (config.hosts.authdomain) {
  23. message += 'placeholder="user identity" autofocus>';
  24. } else {
  25. message += 'placeholder="user@domain.net" autofocus>';
  26. }
  27. message += '<input name="password" ' +
  28. 'type="password" data-i18n="[placeholder]dialog.userPassword"' +
  29. ' placeholder="user password">';
  30. var okButton = APP.translation.generateTranslationHTML("dialog.Ok");
  31. var cancelButton = APP.translation.generateTranslationHTML("dialog.Cancel");
  32. var states = {
  33. login: {
  34. html: message,
  35. buttons: [
  36. { title: okButton, value: true},
  37. { title: cancelButton, value: false}
  38. ],
  39. focus: ':input:first',
  40. submit: function (e, v, m, f) {
  41. e.preventDefault();
  42. if (v) {
  43. var jid = f.username;
  44. var password = f.password;
  45. if (jid && password) {
  46. stop = false;
  47. if (jid.indexOf("@") < 0) {
  48. jid = jid.concat('@');
  49. if (config.hosts.authdomain) {
  50. jid += config.hosts.authdomain;
  51. } else {
  52. jid += config.hosts.domain;
  53. }
  54. }
  55. connection.reset();
  56. connDialog.goToState('connecting');
  57. connection.connect(jid, password, stateHandler);
  58. }
  59. } else {
  60. // User cancelled
  61. stop = true;
  62. callback();
  63. }
  64. }
  65. },
  66. connecting: {
  67. title: APP.translation.translateString('dialog.connecting'),
  68. html: '<div id="connectionStatus"></div>',
  69. buttons: [],
  70. defaultButton: 0
  71. },
  72. finished: {
  73. title: APP.translation.translateString('dialog.error'),
  74. html: '<div id="errorMessage"></div>',
  75. buttons: [
  76. {
  77. title: APP.translation.translateString('dialog.retry'),
  78. value: 'retry'
  79. },
  80. {
  81. title: APP.translation.translateString('dialog.Cancel'),
  82. value: 'cancel'
  83. },
  84. ],
  85. defaultButton: 0,
  86. submit: function (e, v, m, f) {
  87. e.preventDefault();
  88. if (v === 'retry')
  89. connDialog.goToState('login');
  90. else
  91. callback();
  92. }
  93. }
  94. };
  95. var connDialog
  96. = APP.UI.messageHandler.openDialogWithStates(states,
  97. { persistent: true, closeText: '' }, null);
  98. var stateHandler = function (status, message) {
  99. if (stop) {
  100. return;
  101. }
  102. var translateKey = "connection." + XMPP.getStatusString(status);
  103. var statusStr = APP.translation.translateString(translateKey);
  104. // Display current state
  105. var connectionStatus =
  106. connDialog.getState('connecting').find('#connectionStatus');
  107. connectionStatus.text(statusStr);
  108. switch (status) {
  109. case XMPP.Status.CONNECTED:
  110. stop = true;
  111. if (!obtainSession) {
  112. callback(connection, status);
  113. return;
  114. }
  115. // Obtaining session-id status
  116. connectionStatus.text(
  117. APP.translation.translateString(
  118. 'connection.FETCH_SESSION_ID'));
  119. // Authenticate with Jicofo and obtain session-id
  120. var roomName = APP.UI.generateRoomName();
  121. // Jicofo will return new session-id when connected
  122. // from authenticated domain
  123. connection.sendIQ(
  124. Moderator.createConferenceIq(roomName),
  125. function (result) {
  126. connectionStatus.text(
  127. APP.translation.translateString(
  128. 'connection.GOT_SESSION_ID'));
  129. stop = true;
  130. // Parse session-id
  131. Moderator.parseSessionId(result);
  132. callback(connection, status);
  133. },
  134. function (error) {
  135. console.error("Auth on the fly failed", error);
  136. stop = true;
  137. var errorMsg =
  138. APP.translation.translateString(
  139. 'connection.GET_SESSION_ID_ERROR') +
  140. $(error).find('>error').attr('code');
  141. self.displayError(errorMsg);
  142. connection.disconnect();
  143. });
  144. break;
  145. case XMPP.Status.AUTHFAIL:
  146. case XMPP.Status.CONNFAIL:
  147. case XMPP.Status.DISCONNECTED:
  148. stop = true;
  149. callback(connection, status);
  150. var errorMessage = statusStr;
  151. if (message)
  152. {
  153. errorMessage += ': ' + message;
  154. }
  155. self.displayError(errorMessage);
  156. break;
  157. default:
  158. break;
  159. }
  160. };
  161. /**
  162. * Displays error message in 'finished' state which allows either to cancel
  163. * or retry.
  164. * @param message the final message to be displayed.
  165. */
  166. this.displayError = function (message) {
  167. var finishedState = connDialog.getState('finished');
  168. var errorMessageElem = finishedState.find('#errorMessage');
  169. errorMessageElem.text(message);
  170. connDialog.goToState('finished');
  171. };
  172. /**
  173. * Closes LoginDialog.
  174. */
  175. this.close = function () {
  176. stop = true;
  177. connDialog.close();
  178. };
  179. }
  180. var LoginDialog = {
  181. /**
  182. * Displays login prompt used to establish new XMPP connection. Given
  183. * <tt>callback(Strophe.Connection, Strophe.Status)</tt> function will be
  184. * called when we connect successfully(status === CONNECTED) or when we fail
  185. * to do so. On connection failure program can call Dialog.close() method in
  186. * order to cancel or do nothing to let the user retry.
  187. * @param callback <tt>function(Strophe.Connection, Strophe.Status)</tt>
  188. * called when we either fail to connect or succeed(check
  189. * Strophe.Status).
  190. * @param obtainSession <tt>true</tt> if we want to send ConferenceIQ to
  191. * Jicofo in order to create session-id after the connection is
  192. * established.
  193. * @returns {Dialog}
  194. */
  195. show: function (callback, obtainSession) {
  196. return new Dialog(callback, obtainSession);
  197. }
  198. };
  199. module.exports = LoginDialog;