login-modal.js 2.9 KB

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