FIX: Rate limited errors on forgot password were not displayed

This commit is contained in:
Robin Ward 2016-11-16 11:20:55 -05:00
parent 26f9a7ac50
commit 036954d5b4

View File

@ -1,62 +1,54 @@
import { ajax } from 'discourse/lib/ajax';
import ModalFunctionality from 'discourse/mixins/modal-functionality';
import { escapeExpression } from 'discourse/lib/utilities';
import { extractError } from 'discourse/lib/ajax-error';
import computed from 'ember-addons/ember-computed-decorators';
export default Ember.Controller.extend(ModalFunctionality, {
// You need a value in the field to submit it.
submitDisabled: function() {
return Ember.isEmpty((this.get('accountEmailOrUsername') || '').trim()) || this.get('disabled');
}.property('accountEmailOrUsername', 'disabled'),
@computed('accountEmailOrUsername', 'disabled')
submitDisabled(accountEmailOrUsername, disabled) {
return Ember.isEmpty((accountEmailOrUsername || '').trim()) || disabled;
},
onShow: function() {
onShow() {
if ($.cookie('email')) {
this.set('accountEmailOrUsername', $.cookie('email'));
}
},
actions: {
submit: function() {
var self = this;
submit() {
if (this.get('submitDisabled')) return false;
this.set('disabled', true);
var success = function(data) {
// don't tell people what happened, this keeps it more secure (ensure same on server)
var escaped = escapeExpression(self.get('accountEmailOrUsername'));
var isEmail = self.get('accountEmailOrUsername').match(/@/);
var key = 'forgot_password.complete_' + (isEmail ? 'email' : 'username');
var extraClass;
ajax('/session/forgot_password', {
data: { login: this.get('accountEmailOrUsername').trim() },
type: 'POST'
}).then(data => {
const escaped = escapeExpression(this.get('accountEmailOrUsername'));
const isEmail = this.get('accountEmailOrUsername').match(/@/);
let key = 'forgot_password.complete_' + (isEmail ? 'email' : 'username');
let extraClass;
if (data.user_found === true) {
key += '_found';
self.set('accountEmailOrUsername', '');
this.set('accountEmailOrUsername', '');
bootbox.alert(I18n.t(key, {email: escaped, username: escaped}));
self.send("closeModal");
this.send("closeModal");
} else {
if (data.user_found === false) {
key += '_not_found';
extraClass = 'error';
}
self.flash(I18n.t(key, {email: escaped, username: escaped}), extraClass);
this.flash(I18n.t(key, {email: escaped, username: escaped}), extraClass);
}
};
var fail = function(e) {
self.flash(e.responseJSON.errors[0], 'error');
};
ajax('/session/forgot_password', {
data: { login: this.get('accountEmailOrUsername').trim() },
type: 'POST'
}).then(success, fail).finally(function(){
setTimeout(function(){
self.set('disabled',false);
}, 1000);
}).catch(e => {
this.flash(extractError(e), 'error');
}).finally(() => {
setTimeout(() => this.set('disabled', false), 1000);
});
return false;