signin.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. (function (Misago) {
  2. 'use strict';
  3. function persistent(el, isInit, context) {
  4. context.retain = true;
  5. }
  6. var signin = {
  7. controller: function() {
  8. return {
  9. busy: m.prop(false),
  10. showActivation: m.prop(false),
  11. username: m.prop(''),
  12. password: m.prop(''),
  13. validation: {
  14. 'username': [Misago.validators.required()],
  15. 'password': [Misago.validators.required()]
  16. }
  17. };
  18. },
  19. submit: function(ctrl, _) {
  20. if (ctrl.busy()) {
  21. return false;
  22. }
  23. if (_.validate(ctrl).length) {
  24. _.alert.error(gettext("Fill out both fields."));
  25. return false;
  26. }
  27. m.startComputation();
  28. ctrl.busy(true);
  29. m.endComputation();
  30. var credentials = {
  31. username: ctrl.username(),
  32. password: ctrl.password()
  33. };
  34. var self = this;
  35. _.api.endpoint('auth').post(credentials).then(
  36. function() {
  37. self.success(credentials, _);
  38. },
  39. function(error) {
  40. self.error(ctrl, error, _);
  41. });
  42. return false;
  43. },
  44. success: function(credentials, _) {
  45. var $form = $('#hidden-login-form');
  46. // refresh CSRF token because api call to /auth/ changed it
  47. _.ajax.refreshCsrfToken();
  48. // fill out form with user credentials and submit it, this will tell
  49. // misago to redirect user back to right page, and will trigger browser's
  50. // key ring feature
  51. $form.find('input[type="hidden"]').val(_.ajax.csrfToken);
  52. $form.find('input[name="redirect_to"]').val(m.route());
  53. $form.find('input[name="username"]').val(credentials.username);
  54. $form.find('input[name="password"]').val(credentials.password);
  55. $form.submit();
  56. },
  57. error: function(ctrl, rejection, _) {
  58. // activate form again
  59. m.startComputation();
  60. ctrl.busy(false);
  61. m.endComputation();
  62. // handle returned error
  63. if (rejection.status === 400) {
  64. if (rejection.code === 'inactive_admin') {
  65. _.alert.info(rejection.detail);
  66. } else if (rejection.code === 'inactive_user') {
  67. _.alert.info(rejection.detail);
  68. ctrl.showActivation(true);
  69. } else if (rejection.code === 'banned') {
  70. _.modal();
  71. _.router.error403({
  72. message: '',
  73. ban: rejection.detail
  74. });
  75. } else {
  76. _.alert.error(rejection.detail);
  77. }
  78. } else {
  79. _.api.alert(rejection);
  80. }
  81. },
  82. view: function(ctrl, _) {
  83. return m('.modal-dialog.modal-sm.modal-signin[role="document"]',
  84. {config: persistent},
  85. m('.modal-content', [
  86. _.component('modal:header', gettext('Sign in')),
  87. m('form', {onsubmit: this.submit.bind(this, ctrl, _)}, [
  88. m('.modal-body', [
  89. m('.form-group',
  90. m('.control-input',
  91. Misago.input({
  92. disabled: ctrl.busy(),
  93. value: ctrl.username,
  94. placeholder: gettext("Username or e-mail")
  95. })
  96. )
  97. ),
  98. m('.form-group',
  99. m('.control-input',
  100. Misago.input({
  101. type: 'password',
  102. disabled: ctrl.busy(),
  103. value: ctrl.password,
  104. placeholder: gettext("Password")
  105. })
  106. )
  107. )
  108. ]),
  109. m('.modal-footer',
  110. _.component('button', {
  111. class: '.btn-primary.btn-block',
  112. submit: true,
  113. loading: ctrl.busy(),
  114. label: gettext('Sign in')
  115. })
  116. )
  117. ])
  118. ])
  119. );
  120. }
  121. };
  122. Misago.addService('modal:sign-in', function(_) {
  123. _.modal('sign-in', signin);
  124. },
  125. {
  126. after: 'modals'
  127. });
  128. }(Misago.prototype));