1
0
Fork 0
mirror of https://github.com/TryGhost/Ghost-Admin.git synced 2023-12-14 02:33:04 +01:00
Ghost-Admin/app/controllers/reset.js
Kevin Ansfield 238983a5df Match service/controller import to ember-modules-codemod style for consistency
no issue

Automated tools, code generators, and editor integrations are increasingly standardising on the import style used in `ember-modules-codemod`. Our import style differed a little with regards to service/controller injection imports which meant we were starting to see inconsistent naming.
2017-10-30 09:38:01 +00:00

77 lines
2.5 KiB
JavaScript

import Controller from '@ember/controller';
import ValidationEngine from 'ghost-admin/mixins/validation-engine';
import {computed} from '@ember/object';
import {inject as service} from '@ember/service';
import {task} from 'ember-concurrency';
export default Controller.extend(ValidationEngine, {
newPassword: '',
ne2Password: '',
token: '',
flowErrors: '',
validationType: 'reset',
ghostPaths: service(),
notifications: service(),
session: service(),
ajax: service(),
config: service(),
email: 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() {
this.setProperties({
newPassword: '',
ne2Password: '',
token: ''
});
},
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']);
try {
yield this.validate();
try {
let resp = yield this.get('ajax').put(authUrl, {
data: {
passwordreset: [credentials]
}
});
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);
return true;
} 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);
}
if (this.get('errors.ne2Password')) {
this.set('flowErrors', this.get('errors.ne2Password')[0].message);
}
if (error && this.get('errors.length') === 0) {
throw error;
}
}
}).drop(),
actions: {
submit() {
return this.get('resetPassword').perform();
}
}
});