2016-07-06 22:47:30 +03:00
|
|
|
import Controller from 'ember-controller';
|
2017-05-29 21:50:03 +03:00
|
|
|
import ValidationEngine from 'ghost-admin/mixins/validation-engine';
|
2016-07-06 22:47:30 +03:00
|
|
|
import computed from 'ember-computed';
|
|
|
|
import injectService from 'ember-service/inject';
|
2017-01-25 16:00:58 +03:00
|
|
|
import {task} from 'ember-concurrency';
|
2015-10-28 14:36:45 +03:00
|
|
|
|
|
|
|
export default Controller.extend(ValidationEngine, {
|
2015-05-27 03:41:12 +03:00
|
|
|
newPassword: '',
|
|
|
|
ne2Password: '',
|
|
|
|
token: '',
|
2015-08-25 14:32:29 +03:00
|
|
|
flowErrors: '',
|
2015-05-27 03:41:12 +03:00
|
|
|
|
|
|
|
validationType: 'reset',
|
|
|
|
|
2016-07-06 22:47:30 +03:00
|
|
|
ghostPaths: injectService(),
|
|
|
|
notifications: injectService(),
|
|
|
|
session: injectService(),
|
|
|
|
ajax: injectService(),
|
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: ''
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-01-25 16:00:58 +03:00
|
|
|
resetPassword: task(function* () {
|
|
|
|
let credentials = this.getProperties('newPassword', 'ne2Password', 'token');
|
|
|
|
let authUrl = this.get('ghostPaths.url').api('authentication', 'passwordreset');
|
|
|
|
|
|
|
|
this.set('flowErrors', '');
|
|
|
|
this.get('hasValidated').addObjects(['newPassword', 'ne2Password']);
|
2015-10-28 14:36:45 +03:00
|
|
|
|
2017-01-25 16:00:58 +03:00
|
|
|
try {
|
|
|
|
yield this.validate();
|
|
|
|
try {
|
|
|
|
let resp = yield this.get('ajax').put(authUrl, {
|
2015-05-27 03:41:12 +03:00
|
|
|
data: {
|
|
|
|
passwordreset: [credentials]
|
|
|
|
}
|
|
|
|
});
|
2017-01-25 16:00:58 +03:00
|
|
|
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 (error) {
|
|
|
|
this.get('notifications').showAPIError(error, {key: 'password.reset'});
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
if (this.get('errors.newPassword')) {
|
|
|
|
this.set('flowErrors', this.get('errors.newPassword')[0].message);
|
|
|
|
}
|
2015-08-25 14:32:29 +03:00
|
|
|
|
2017-01-25 16:00:58 +03:00
|
|
|
if (this.get('errors.ne2Password')) {
|
|
|
|
this.set('flowErrors', this.get('errors.ne2Password')[0].message);
|
|
|
|
}
|
2015-12-16 00:51:36 +03:00
|
|
|
|
2017-01-25 16:00:58 +03:00
|
|
|
if (error && this.get('errors.length') === 0) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).drop(),
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
submit() {
|
|
|
|
this.get('resetPassword').perform();
|
2015-05-27 03:41:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|