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';
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
const {Controller, computed, inject} = Ember;
|
|
|
|
|
|
|
|
export default Controller.extend(ValidationEngine, {
|
2015-05-27 03:41:12 +03:00
|
|
|
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-10-28 14:36:45 +03:00
|
|
|
ghostPaths: inject.service('ghost-paths'),
|
|
|
|
notifications: inject.service(),
|
|
|
|
session: inject.service(),
|
2015-05-26 05:10:50 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
email: computed('token', function () {
|
2015-05-27 03:41:12 +03:00
|
|
|
// 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
|
2015-10-28 14:36:45 +03:00
|
|
|
clearData() {
|
2015-05-27 03:41:12 +03:00
|
|
|
this.setProperties({
|
|
|
|
newPassword: '',
|
|
|
|
ne2Password: '',
|
|
|
|
token: ''
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
2015-10-28 14:36:45 +03:00
|
|
|
submit() {
|
|
|
|
let credentials = this.getProperties('newPassword', 'ne2Password', 'token');
|
|
|
|
|
2015-08-25 14:32:29 +03:00
|
|
|
this.set('flowErrors', '');
|
2015-12-16 00:51:36 +03:00
|
|
|
this.get('hasValidated').addObjects(['newPassword', 'ne2Password']);
|
2015-10-28 14:36:45 +03:00
|
|
|
this.validate().then(() => {
|
|
|
|
this.toggleProperty('submitting');
|
2015-05-27 03:41:12 +03:00
|
|
|
ajax({
|
2015-10-28 14:36:45 +03:00
|
|
|
url: this.get('ghostPaths.url').api('authentication', 'passwordreset'),
|
2015-05-27 03:41:12 +03:00
|
|
|
type: 'PUT',
|
|
|
|
data: {
|
|
|
|
passwordreset: [credentials]
|
|
|
|
}
|
2015-10-28 14:36:45 +03:00
|
|
|
}).then((resp) => {
|
|
|
|
this.toggleProperty('submitting');
|
|
|
|
this.get('notifications').showAlert(resp.passwordreset[0].message, {type: 'warn', delayed: true, key: 'password.reset'});
|
|
|
|
this.get('session').authenticate('authenticator:oauth2', this.get('email'), credentials.newPassword);
|
|
|
|
}).catch((response) => {
|
|
|
|
this.get('notifications').showAPIError(response, {key: 'password.reset'});
|
|
|
|
this.toggleProperty('submitting');
|
2015-05-27 03:41:12 +03:00
|
|
|
});
|
2015-12-16 00:51:36 +03:00
|
|
|
}).catch((error) => {
|
2015-10-28 14:36:45 +03:00
|
|
|
if (this.get('errors.newPassword')) {
|
|
|
|
this.set('flowErrors', this.get('errors.newPassword')[0].message);
|
2015-08-25 14:32:29 +03:00
|
|
|
}
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
if (this.get('errors.ne2Password')) {
|
|
|
|
this.set('flowErrors', this.get('errors.ne2Password')[0].message);
|
2015-08-25 14:32:29 +03:00
|
|
|
}
|
2015-12-16 00:51:36 +03:00
|
|
|
|
|
|
|
if (this.get('errors.length') === 0) {
|
|
|
|
throw error;
|
|
|
|
}
|
2015-05-27 03:41:12 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|