discourse/app/assets/javascripts/discourse/controllers/forgot-password.js.es6

67 lines
2.0 KiB
Plaintext
Raw Normal View History

2016-07-01 01:55:44 +08:00
import { ajax } from 'discourse/lib/ajax';
2014-08-13 07:04:36 +08:00
import ModalFunctionality from 'discourse/mixins/modal-functionality';
import { escapeExpression } from 'discourse/lib/utilities';
2014-08-13 07:04:36 +08:00
export default Ember.Controller.extend(ModalFunctionality, {
2014-05-06 23:48:04 +08:00
// You need a value in the field to submit it.
submitDisabled: function() {
return Ember.isEmpty((this.get('accountEmailOrUsername') || '').trim()) || this.get('disabled');
2014-08-18 08:55:30 +08:00
}.property('accountEmailOrUsername', 'disabled'),
2014-05-06 23:48:04 +08:00
onShow: function() {
if ($.cookie('email')) {
this.set('accountEmailOrUsername', $.cookie('email'));
}
},
2014-05-06 23:48:04 +08:00
actions: {
submit: function() {
2014-08-18 08:55:30 +08:00
var self = this;
2014-05-06 23:48:04 +08:00
2014-08-18 08:55:30 +08:00
if (this.get('submitDisabled')) return false;
this.set('disabled', true);
var success = function(data) {
2014-08-18 08:55:30 +08:00
// 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;
if (data.user_found === true) {
key += '_found';
self.set('accountEmailOrUsername', '');
bootbox.alert(I18n.t(key, {email: escaped, username: escaped}));
self.send("closeModal");
} else {
if (data.user_found === false) {
key += '_not_found';
extraClass = 'error';
}
self.flash(I18n.t(key, {email: escaped, username: escaped}), extraClass);
2014-08-18 08:55:30 +08:00
}
};
var fail = function(e) {
self.flash(e.responseJSON.errors[0], 'error');
2014-08-18 08:55:30 +08:00
};
2016-07-01 01:55:44 +08:00
ajax('/session/forgot_password', {
data: { login: this.get('accountEmailOrUsername').trim() },
2014-05-06 23:48:04 +08:00
type: 'POST'
2014-08-18 08:55:30 +08:00
}).then(success, fail).finally(function(){
setTimeout(function(){
self.set('disabled',false);
}, 1000);
2014-05-06 23:48:04 +08:00
});
return false;
}
}
});