login-modal.js 2.9 KB

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