login-modal.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. signIn: 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. var $form = Ember.$('#hidden-login-form');
  49. // we need to refresh CSRF token because previous api call changed it
  50. $form.find('input[name=csrfmiddlewaretoken]').val(getCsrfToken());
  51. // fill out form with user credentials and submit it, this will tell
  52. // misago to redirect user back to right page, and will trigger browser's
  53. // key ring feature
  54. $form.find('input[name=redirect_to]').val(window.location.href);
  55. $form.find('input[name=username]').val(credentials.username);
  56. $form.find('input[name=password]').val(credentials.password);
  57. $form.submit();
  58. }, function(jqXHR) {
  59. var rejection = jqXHR.responseJSON;
  60. if (typeof rejection.code === "undefined") {
  61. self.send("error", rejection);
  62. } else {
  63. if (rejection.code === 'inactive_admin') {
  64. self.get('toast').info(rejection.detail);
  65. } else if (rejection.code === 'inactive_user') {
  66. self.get('toast').info(rejection.detail);
  67. self.set('showActivation', true);
  68. } else if (rejection.code === 'banned') {
  69. self.send('showBan', rejection.detail);
  70. Ember.run(function() {
  71. Ember.$('#loginModal').modal('hide');
  72. });
  73. } else {
  74. self.get('toast').error(rejection.detail);
  75. }
  76. }
  77. }).finally(function() {
  78. self.set('isLoading', false);
  79. });
  80. },
  81. forgotPassword: function() {
  82. this.transitionToRoute('forgotten-password');
  83. Ember.$('#loginModal').modal('hide');
  84. },
  85. activateAccount: function() {
  86. Ember.$('#loginModal').modal('hide');
  87. }
  88. }
  89. });