Ghost/ghost/admin/app/components/modal-re-authenticate.js
Kevin Ansfield 352c4af1d7 Refactored usage of .get('property') with es5 getters
no issue
- ran [es5-getter-ember-codemod](https://github.com/rondale-sc/es5-getter-ember-codemod)
- [es5 getters RFC](https://github.com/emberjs/rfcs/blob/master/text/0281-es5-getters.md)
- updates the majority of `object.get('property')` with `object.property` with exceptions:
  - `.get('nested.property')` - it's not possible to determine if this is relying on "safe" path chaining for when `nested` doesn't exist
  - `.get('config.x')` and `.get('settings.x')` - both our `config` and `settings` services are proxy objects which do not support es5 getters
- this PR is not exhaustive, there are still a number of places where `.get('service.foo')` and similar could be replaced but it gets us a long way there in a quick and automated fashion
2019-03-06 13:54:14 +00:00

79 lines
2.6 KiB
JavaScript

import $ from 'jquery';
import ModalComponent from 'ghost-admin/components/modal-base';
import ValidationEngine from 'ghost-admin/mixins/validation-engine';
import {computed} from '@ember/object';
import {htmlSafe} from '@ember/string';
import {isVersionMismatchError} from 'ghost-admin/services/ajax';
import {inject as service} from '@ember/service';
import {task} from 'ember-concurrency';
export default ModalComponent.extend(ValidationEngine, {
config: service(),
notifications: service(),
session: service(),
validationType: 'signin',
authenticationError: null,
identification: computed('session.user.email', function () {
return this.get('session.user.email');
}),
actions: {
confirm() {
this.reauthenticate.perform();
}
},
_authenticate() {
let session = this.session;
let authStrategy = 'authenticator:cookie';
let identification = this.identification;
let password = this.password;
session.set('skipAuthSuccessHandler', true);
this.toggleProperty('submitting');
return session.authenticate(authStrategy, identification, password).finally(() => {
this.toggleProperty('submitting');
session.set('skipAuthSuccessHandler', undefined);
});
},
_passwordConfirm() {
// Manually trigger events for input fields, ensuring legacy compatibility with
// browsers and password managers that don't send proper events on autofill
$('#login').find('input').trigger('change');
this.set('authenticationError', null);
return this.validate({property: 'signin'}).then(() => this._authenticate().then(() => {
this.notifications.closeAlerts();
this.send('closeModal');
return true;
}).catch((error) => {
if (error && error.payload && error.payload.errors) {
error.payload.errors.forEach((err) => {
if (isVersionMismatchError(err)) {
return this.notifications.showAPIError(error);
}
err.message = htmlSafe(err.context || err.message);
});
this.errors.add('password', 'Incorrect password');
this.hasValidated.pushObject('password');
this.set('authenticationError', error.payload.errors[0].message);
}
}), () => {
this.hasValidated.pushObject('password');
return false;
});
},
reauthenticate: task(function* () {
return yield this._passwordConfirm();
}).drop()
});