login-form.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import Ember from 'ember';
  2. export default Ember.Component.extend({
  3. tagName: 'form',
  4. isBusy: false,
  5. showActivation: false,
  6. username: '',
  7. password: '',
  8. router: function() {
  9. return this.container.lookup('router:main');
  10. }.property(),
  11. submit: function() {
  12. if (this.get('isBusy')) {
  13. return false;
  14. }
  15. var credentials = {
  16. username: Ember.$.trim(this.get('username')),
  17. password: Ember.$.trim(this.get('password'))
  18. };
  19. if (!credentials.username || !credentials.password) {
  20. this.toast.warning(gettext("Fill out both fields."));
  21. return false;
  22. }
  23. var self = this;
  24. this.ajax.post(this.get('settings.loginApiUrl'), credentials
  25. ).then(function() {
  26. if (self.isDestroyed) { return; }
  27. self.success(credentials);
  28. }, function(jqXHR) {
  29. if (self.isDestroyed) { return; }
  30. self.error(jqXHR);
  31. });
  32. return false;
  33. },
  34. success: function(credentials) {
  35. var $form = Ember.$('#hidden-login-form');
  36. // refresh CSRF token because parent api call changed it
  37. this.csrf.updateFormToken($form);
  38. // fill out form with user credentials and submit it, this will tell
  39. // misago to redirect user back to right page, and will trigger browser's
  40. // key ring feature
  41. $form.find('input[name=redirect_to]').val(window.location.href);
  42. $form.find('input[name=username]').val(credentials.username);
  43. $form.find('input[name=password]').val(credentials.password);
  44. $form.submit();
  45. },
  46. error: function(jqXHR) {
  47. self.set('isBusy', false);
  48. var rejection = jqXHR.responseJSON;
  49. if (jqXHR.status !== 400) {
  50. this.toast.apiError(jqXHR);
  51. } else if (rejection.code === 'inactive_admin') {
  52. this.toast.info(rejection.detail);
  53. } else if (rejection.code === 'inactive_user') {
  54. this.toast.info(rejection.detail);
  55. this.set('showActivation', true);
  56. } else if (rejection.code === 'banned') {
  57. this.get('router').intermediateTransitionTo('error-banned', rejection.detail);
  58. this.modal.hide();
  59. } else {
  60. this.toast.error(rejection.detail);
  61. }
  62. }
  63. });