login-modal.js 3.1 KB

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