2015-02-13 07:22:32 +03:00
|
|
|
import Ember from 'ember';
|
2016-05-24 15:06:59 +03:00
|
|
|
import ValidationEngine from 'ghost-admin/mixins/validation-engine';
|
2015-05-27 03:41:12 +03:00
|
|
|
|
2016-01-19 16:03:27 +03:00
|
|
|
const {
|
|
|
|
Controller,
|
|
|
|
computed,
|
|
|
|
inject: {service}
|
|
|
|
} = Ember;
|
2015-10-28 14:36:45 +03:00
|
|
|
|
|
|
|
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',
|
|
|
|
|
2016-01-19 16:03:27 +03:00
|
|
|
ghostPaths: service(),
|
|
|
|
notifications: service(),
|
|
|
|
session: service(),
|
|
|
|
ajax: 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(() => {
|
2016-01-18 18:37:14 +03:00
|
|
|
let authUrl = this.get('ghostPaths.url').api('authentication', 'passwordreset');
|
2015-10-28 14:36:45 +03:00
|
|
|
this.toggleProperty('submitting');
|
2016-01-18 18:37:14 +03:00
|
|
|
this.get('ajax').put(authUrl, {
|
2015-05-27 03:41:12 +03:00
|
|
|
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);
|
2016-01-18 18:37:14 +03:00
|
|
|
}).catch((error) => {
|
|
|
|
this.get('notifications').showAPIError(error, {key: 'password.reset'});
|
2015-10-28 14:36:45 +03:00
|
|
|
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
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|