2018-01-11 01:57:43 +03:00
|
|
|
/* eslint-disable ghost/ember/alias-model-in-controller */
|
2017-08-22 10:53:26 +03:00
|
|
|
import Controller from '@ember/controller';
|
2017-05-29 21:50:03 +03:00
|
|
|
import ValidationEngine from 'ghost-admin/mixins/validation-engine';
|
2017-08-22 10:53:26 +03:00
|
|
|
import {computed} from '@ember/object';
|
2017-10-30 12:38:01 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
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, {
|
2018-01-11 20:43:23 +03:00
|
|
|
ghostPaths: service(),
|
|
|
|
notifications: service(),
|
|
|
|
session: service(),
|
|
|
|
ajax: service(),
|
|
|
|
config: service(),
|
|
|
|
|
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',
|
|
|
|
|
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.
|
2019-03-06 16:53:54 +03:00
|
|
|
return atob(this.token).split('|')[1];
|
2015-05-27 03:41:12 +03:00
|
|
|
}),
|
|
|
|
|
2018-01-11 20:43:23 +03:00
|
|
|
actions: {
|
|
|
|
submit() {
|
2019-03-06 16:53:54 +03:00
|
|
|
return this.resetPassword.perform();
|
2018-01-11 20:43:23 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-05-27 03:41:12 +03:00
|
|
|
// 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', '');
|
2019-03-06 16:53:54 +03:00
|
|
|
this.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 {
|
2019-03-06 16:53:54 +03:00
|
|
|
let resp = yield this.ajax.put(authUrl, {
|
2015-05-27 03:41:12 +03:00
|
|
|
data: {
|
|
|
|
passwordreset: [credentials]
|
|
|
|
}
|
|
|
|
});
|
2019-03-06 16:53:54 +03:00
|
|
|
this.notifications.showAlert(resp.passwordreset[0].message, {type: 'warn', delayed: true, key: 'password.reset'});
|
|
|
|
this.session.authenticate('authenticator:cookie', this.email, credentials.newPassword);
|
2017-07-20 15:12:33 +03:00
|
|
|
return true;
|
2017-01-25 16:00:58 +03:00
|
|
|
} catch (error) {
|
2019-03-06 16:53:54 +03:00
|
|
|
this.notifications.showAPIError(error, {key: 'password.reset'});
|
2017-01-25 16:00:58 +03:00
|
|
|
}
|
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
}
|
2018-01-11 20:43:23 +03:00
|
|
|
}).drop()
|
2015-05-27 03:41:12 +03:00
|
|
|
});
|