2015-02-13 07:22:32 +03:00
|
|
|
import Ember from 'ember';
|
2015-05-27 03:41:12 +03:00
|
|
|
import {request as ajax} from 'ic-ajax';
|
|
|
|
import ValidationEngine from 'ghost/mixins/validation-engine';
|
|
|
|
|
|
|
|
export default Ember.Controller.extend(ValidationEngine, {
|
|
|
|
newPassword: '',
|
|
|
|
ne2Password: '',
|
|
|
|
token: '',
|
|
|
|
submitting: false,
|
2015-08-25 14:32:29 +03:00
|
|
|
flowErrors: '',
|
2015-05-27 03:41:12 +03:00
|
|
|
|
|
|
|
validationType: 'reset',
|
|
|
|
|
2015-05-26 05:10:50 +03:00
|
|
|
ghostPaths: Ember.inject.service('ghost-paths'),
|
|
|
|
notifications: Ember.inject.service(),
|
2015-10-18 21:17:02 +03:00
|
|
|
session: Ember.inject.service(),
|
2015-05-26 05:10:50 +03:00
|
|
|
|
2015-05-27 03:41:12 +03:00
|
|
|
email: Ember.computed('token', function () {
|
|
|
|
// The token base64 encodes the email (and some other stuff),
|
|
|
|
// each section is divided by a '|'. Email comes second.
|
|
|
|
return atob(this.get('token')).split('|')[1];
|
|
|
|
}),
|
|
|
|
|
|
|
|
// Used to clear sensitive information
|
|
|
|
clearData: function () {
|
|
|
|
this.setProperties({
|
|
|
|
newPassword: '',
|
|
|
|
ne2Password: '',
|
|
|
|
token: ''
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
submit: function () {
|
|
|
|
var credentials = this.getProperties('newPassword', 'ne2Password', 'token'),
|
|
|
|
self = this;
|
2015-08-25 14:32:29 +03:00
|
|
|
this.set('flowErrors', '');
|
2015-08-27 23:28:41 +03:00
|
|
|
this.get('hasValidated').addObjects((['newPassword', 'ne2Password']));
|
2015-07-21 20:56:17 +03:00
|
|
|
this.validate().then(function () {
|
|
|
|
self.toggleProperty('submitting');
|
2015-05-27 03:41:12 +03:00
|
|
|
ajax({
|
|
|
|
url: self.get('ghostPaths.url').api('authentication', 'passwordreset'),
|
|
|
|
type: 'PUT',
|
|
|
|
data: {
|
|
|
|
passwordreset: [credentials]
|
|
|
|
}
|
|
|
|
}).then(function (resp) {
|
|
|
|
self.toggleProperty('submitting');
|
2015-10-07 17:44:23 +03:00
|
|
|
self.get('notifications').showAlert(resp.passwordreset[0].message, {type: 'warn', delayed: true, key: 'password.reset'});
|
2015-10-18 21:17:02 +03:00
|
|
|
self.get('session').authenticate('authenticator:oauth2', self.get('email'), credentials.newPassword);
|
2015-05-27 03:41:12 +03:00
|
|
|
}).catch(function (response) {
|
2015-10-07 17:44:23 +03:00
|
|
|
self.get('notifications').showAPIError(response, {key: 'password.reset'});
|
2015-05-27 03:41:12 +03:00
|
|
|
self.toggleProperty('submitting');
|
|
|
|
});
|
2015-08-25 14:32:29 +03:00
|
|
|
}).catch(function () {
|
|
|
|
if (self.get('errors.newPassword')) {
|
|
|
|
self.set('flowErrors', self.get('errors.newPassword')[0].message);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self.get('errors.ne2Password')) {
|
|
|
|
self.set('flowErrors', self.get('errors.ne2Password')[0].message);
|
|
|
|
}
|
2015-05-27 03:41:12 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|