login-modal.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import Ember from 'ember';
  2. export default Ember.Controller.extend({
  3. modal: null,
  4. isLoading: false,
  5. showActivation: false,
  6. username: '',
  7. password: '',
  8. scheduleSetup: function() {
  9. Ember.run.scheduleOnce('afterRender', this, this.setup);
  10. }.on('init'),
  11. setup: function() {
  12. this.modal = Ember.$('#loginModal').modal({show: false});
  13. this.modal.on('shown.bs.modal', function () {
  14. Ember.$('#loginModal').focus();
  15. });
  16. var self = this;
  17. this.modal.on('hidden.bs.modal', function() {
  18. self.reset();
  19. });
  20. },
  21. reset: function() {
  22. this.setProperties({
  23. 'username': '',
  24. 'password': '',
  25. 'isLoading': false,
  26. 'showActivation': false
  27. });
  28. },
  29. actions: {
  30. open: function() {
  31. Ember.$('#loginModal').modal('show');
  32. },
  33. submit: function() {
  34. if (this.get('isLoading')) {
  35. return;
  36. }
  37. var credentials = {
  38. username: Ember.$.trim(this.get('username')),
  39. password: Ember.$.trim(this.get('password'))
  40. };
  41. if (!credentials.username || !credentials.password) {
  42. this.toast.warning(gettext("Fill out both fields."));
  43. return;
  44. }
  45. var self = this;
  46. this.rpc.ajax(this.get('settings.loginApiUrl'), credentials
  47. ).then(function() {
  48. self.send('success', credentials);
  49. }, function(jqXHR) {
  50. self.send('error', jqXHR);
  51. }).finally(function() {
  52. self.set('isLoading', false);
  53. });
  54. },
  55. success: function(credentials) {
  56. var $form = Ember.$('#hidden-login-form');
  57. // refresh CSRF token because parent api call changed it
  58. this.csrf.updateFormToken($form);
  59. // fill out form with user credentials and submit it, this will tell
  60. // misago to redirect user back to right page, and will trigger browser's
  61. // key ring feature
  62. $form.find('input[name=redirect_to]').val(window.location.href);
  63. $form.find('input[name=username]').val(credentials.username);
  64. $form.find('input[name=password]').val(credentials.password);
  65. $form.submit();
  66. },
  67. error: function(jqXHR) {
  68. var rejection = jqXHR.responseJSON;
  69. if (jqXHR.status !== 400) {
  70. this.send('toastError', jqXHR);
  71. } else if (rejection.code === 'inactive_admin') {
  72. this.toast.info(rejection.detail);
  73. } else if (rejection.code === 'inactive_user') {
  74. this.toast.info(rejection.detail);
  75. this.set('showActivation', true);
  76. } else if (rejection.code === 'banned') {
  77. this.send('showBan', rejection.detail);
  78. Ember.run(function() {
  79. Ember.$('#loginModal').modal('hide');
  80. });
  81. } else {
  82. this.toast.error(rejection.detail);
  83. }
  84. return false;
  85. },
  86. // Go-to links
  87. activateAccount: function() {
  88. this.transitionToRoute('activation');
  89. Ember.$('#loginModal').modal('hide');
  90. },
  91. forgotPassword: function() {
  92. this.transitionToRoute('forgotten-password');
  93. Ember.$('#loginModal').modal('hide');
  94. }
  95. }
  96. });