login-modal.js 2.8 KB

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