123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- import Ember from 'ember';
- export default Ember.Component.extend({
- tagName: 'form',
- isBusy: false,
- showActivation: false,
- username: '',
- password: '',
- router: function() {
- return this.container.lookup('router:main');
- }.property(),
- submit: function() {
- if (this.get('isBusy')) {
- return false;
- }
- var credentials = {
- username: Ember.$.trim(this.get('username')),
- password: Ember.$.trim(this.get('password'))
- };
- if (!credentials.username || !credentials.password) {
- this.toast.warning(gettext("Fill out both fields."));
- return false;
- }
- var self = this;
- this.ajax.post(this.get('settings.loginApiUrl'), credentials
- ).then(function() {
- if (self.isDestroyed) { return; }
- self.success(credentials);
- }, function(jqXHR) {
- if (self.isDestroyed) { return; }
- self.error(jqXHR);
- });
- return false;
- },
- success: function(credentials) {
- var $form = Ember.$('#hidden-login-form');
- // refresh CSRF token because parent api call changed it
- this.csrf.updateFormToken($form);
- // fill out form with user credentials and submit it, this will tell
- // misago to redirect user back to right page, and will trigger browser's
- // key ring feature
- $form.find('input[name=redirect_to]').val(window.location.href);
- $form.find('input[name=username]').val(credentials.username);
- $form.find('input[name=password]').val(credentials.password);
- $form.submit();
- },
- error: function(jqXHR) {
- self.set('isBusy', false);
- var rejection = jqXHR.responseJSON;
- if (jqXHR.status !== 400) {
- this.toast.apiError(jqXHR);
- } else if (rejection.code === 'inactive_admin') {
- this.toast.info(rejection.detail);
- } else if (rejection.code === 'inactive_user') {
- this.toast.info(rejection.detail);
- this.set('showActivation', true);
- } else if (rejection.code === 'banned') {
- this.get('router').intermediateTransitionTo('error-banned', rejection.detail);
- this.modal.hide();
- } else {
- this.toast.error(rejection.detail);
- }
- }
- });
|