login-modal.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import Ember from 'ember';
  2. import MisagoPreloadStore from 'misago/utils/preloadstore';
  3. import rpc from 'misago/utils/rpc';
  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(MisagoPreloadStore.get('authApiUrl'), credentials).then(function() {
  42. self.logIn(credentials);
  43. }, function(rejection) {
  44. self.authError(rejection);
  45. }).finally(function() {
  46. self.set('isLoading', false);
  47. });
  48. },
  49. logIn: function(credentials) {
  50. this.send('flashSuccess', gettext("You are authenticated!"));
  51. console.log(credentials);
  52. },
  53. authError: function(rejection) {
  54. if (rejection.code === 'inactive_admin') {
  55. this.send('flashInfo', rejection.detail);
  56. } else if (rejection.code === 'inactive_user') {
  57. this.send('flashInfo', rejection.detail);
  58. this.set('showActivation', true);
  59. } else if (rejection.code === 'banned') {
  60. } else {
  61. this.send('flashError', rejection.detail);
  62. }
  63. },
  64. actions: {
  65. open: function() {
  66. Ember.$('#loginModal').modal('show');
  67. },
  68. signIn: function() {
  69. if (!this.get('isLoading')) {
  70. var credentials = {
  71. username: Ember.$.trim(this.get('username')),
  72. password: Ember.$.trim(this.get('password'))
  73. };
  74. if (this.isValid(credentials)) {
  75. this.authenticate(credentials);
  76. } else {
  77. this.set('isLoading', false);
  78. }
  79. }
  80. }
  81. }
  82. });