login-modal.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. isBanned: false,
  8. showActivation: false,
  9. username: '',
  10. password: '',
  11. ban: null,
  12. scheduleSetup: function() {
  13. Ember.run.scheduleOnce('afterRender', this, this.setup);
  14. }.on('init'),
  15. setup: function() {
  16. this.modal = Ember.$('#loginModal').modal({show: false});
  17. this.modal.on('shown.bs.modal', function () {
  18. Ember.$('#loginModal').focus();
  19. });
  20. var self = this;
  21. this.modal.on('hidden.bs.modal', function() {
  22. self.reset();
  23. });
  24. },
  25. reset: function() {
  26. this.set('ban', null);
  27. this.set('username', '');
  28. this.set('password', '');
  29. this.set('isLoading', false);
  30. this.set('isBanned', false);
  31. this.set('showActivation', false);
  32. },
  33. isValid: function(credentials) {
  34. if (!credentials.username || !credentials.password) {
  35. this.send('flashWarning', gettext("Fill out both fields."));
  36. return false;
  37. }
  38. return true;
  39. },
  40. authenticate: function(credentials) {
  41. var self = this;
  42. rpc(this.get('settings.authApiUrl'), credentials).then(function() {
  43. self.logIn(credentials);
  44. }, function(rejection) {
  45. self.authError(rejection);
  46. self.set('isLoading', false);
  47. });
  48. },
  49. logIn: function(credentials) {
  50. var $form = Ember.$('#hidden-login-form');
  51. // we need to refresh CSRF token because previous api call changed it
  52. $form.find('input[name=csrfmiddlewaretoken]').val(getCsrfToken());
  53. // fill out form with user credentials and submit it, this will tell
  54. // misago to redirect user back to right page, and will trigger browser's
  55. // key ring feature
  56. $form.find('input[name=redirect_to]').val(window.location.href);
  57. $form.find('input[name=username]').val(credentials.username);
  58. $form.find('input[name=password]').val(credentials.password);
  59. $form.submit();
  60. },
  61. authError: function(rejection) {
  62. if (rejection.code === 'inactive_admin') {
  63. this.send('flashInfo', rejection.detail);
  64. } else if (rejection.code === 'inactive_user') {
  65. this.send('flashInfo', rejection.detail);
  66. this.set('showActivation', true);
  67. } else if (rejection.code === 'banned') {
  68. this.set('ban', rejection.detail);
  69. this.set('isBanned', true);
  70. } else {
  71. this.send('flashError', rejection.detail);
  72. }
  73. },
  74. actions: {
  75. open: function() {
  76. Ember.$('#loginModal').modal('show');
  77. },
  78. signIn: function() {
  79. if (!this.get('isLoading')) {
  80. var credentials = {
  81. username: Ember.$.trim(this.get('username')),
  82. password: Ember.$.trim(this.get('password'))
  83. };
  84. if (this.isValid(credentials)) {
  85. this.set('isLoading', true);
  86. this.authenticate(credentials);
  87. } else {
  88. this.set('isLoading', false);
  89. }
  90. }
  91. },
  92. }
  93. });