login-modal.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. toastError: 'toastError',
  42. showBan: 'showBan',
  43. error: function(jqXHR) {
  44. var rejection = jqXHR.responseJSON;
  45. if (jqXHR.status !== 400) {
  46. this.sendAction('toastError', jqXHR);
  47. } else if (rejection.code === 'inactive_admin') {
  48. this.toast.info(rejection.detail);
  49. } else if (rejection.code === 'inactive_user') {
  50. this.toast.info(rejection.detail);
  51. this.set('showActivation', true);
  52. } else if (rejection.code === 'banned') {
  53. this.sendAction('showBan', rejection.detail);
  54. Ember.run(function() {
  55. Ember.$('#loginModal').modal('hide');
  56. });
  57. } else {
  58. this.toast.error(rejection.detail);
  59. }
  60. },
  61. actions: {
  62. submit: function() {
  63. if (this.get('isLoading')) {
  64. return;
  65. }
  66. var credentials = {
  67. username: Ember.$.trim(this.get('username')),
  68. password: Ember.$.trim(this.get('password'))
  69. };
  70. if (!credentials.username || !credentials.password) {
  71. this.toast.warning(gettext("Fill out both fields."));
  72. return;
  73. }
  74. var self = this;
  75. this.rpc.ajax(this.get('settings.loginApiUrl'), credentials
  76. ).then(function() {
  77. self.success(credentials);
  78. }, function(jqXHR) {
  79. self.error(jqXHR);
  80. }).finally(function() {
  81. self.set('isLoading', false);
  82. });
  83. },
  84. // Go-to links
  85. activateAccount: function() {
  86. this.get('router').transitionTo('activation');
  87. Ember.$('#loginModal').modal('hide');
  88. },
  89. forgotPassword: function() {
  90. this.get('router').transitionTo('forgotten-password');
  91. Ember.$('#loginModal').modal('hide');
  92. }
  93. }
  94. });