login-modal.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import Ember from 'ember';
  2. import rpc from 'misago/utils/rpc';
  3. import getCsrfToken from 'misago/utils/csrf';
  4. export default Ember.Controller.extend({
  5. modal: null,
  6. isLoading: false,
  7. showActivation: false,
  8. username: '',
  9. password: '',
  10. scheduleSetup: function() {
  11. Ember.run.scheduleOnce('afterRender', this, this.setup);
  12. }.on('init'),
  13. setup: function() {
  14. this.modal = Ember.$('#loginModal').modal({show: false});
  15. this.modal.on('shown.bs.modal', function () {
  16. Ember.$('#loginModal').focus();
  17. });
  18. var self = this;
  19. this.modal.on('hidden.bs.modal', function() {
  20. self.reset();
  21. });
  22. },
  23. reset: function() {
  24. this.set('username', '');
  25. this.set('password', '');
  26. this.set('isLoading', false);
  27. this.set('showActivation', false);
  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.get('toast').warning(gettext("Fill out both fields."));
  43. return;
  44. }
  45. var self = this;
  46. rpc(this.get('settings.authApiUrl'), 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. // we need to refresh CSRF token because previous api call changed it
  58. $form.find('input[name=csrfmiddlewaretoken]').val(getCsrfToken());
  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.get('toast').info(rejection.detail);
  73. } else if (rejection.code === 'inactive_user') {
  74. this.get('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.get('toast').error(rejection.detail);
  83. }
  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. });