2017-09-11 10:41:17 +03:00
|
|
|
import ModalComponent from 'ghost-admin/components/modal-base';
|
2017-05-29 21:50:03 +03:00
|
|
|
import ValidationEngine from 'ghost-admin/mixins/validation-engine';
|
2021-05-12 14:33:36 +03:00
|
|
|
import {htmlSafe} from '@ember/template';
|
2016-06-30 17:45:02 +03:00
|
|
|
import {isVersionMismatchError} from 'ghost-admin/services/ajax';
|
2020-01-16 20:01:12 +03:00
|
|
|
import {reads} from '@ember/object/computed';
|
2017-10-30 12:38:01 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2017-01-19 14:40:31 +03:00
|
|
|
import {task} from 'ember-concurrency';
|
2015-11-18 13:50:48 +03:00
|
|
|
|
|
|
|
export default ModalComponent.extend(ValidationEngine, {
|
2017-10-30 12:38:01 +03:00
|
|
|
config: service(),
|
|
|
|
notifications: service(),
|
|
|
|
session: service(),
|
2015-11-18 13:50:48 +03:00
|
|
|
|
2018-01-11 20:43:23 +03:00
|
|
|
validationType: 'signin',
|
|
|
|
|
|
|
|
authenticationError: null,
|
|
|
|
|
2020-01-16 20:01:12 +03:00
|
|
|
identification: reads('session.user.email'),
|
2015-11-18 13:50:48 +03:00
|
|
|
|
2018-01-11 20:43:23 +03:00
|
|
|
actions: {
|
|
|
|
confirm() {
|
2019-03-06 16:53:54 +03:00
|
|
|
this.reauthenticate.perform();
|
2018-01-11 20:43:23 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-11-18 13:50:48 +03:00
|
|
|
_authenticate() {
|
2019-03-06 16:53:54 +03:00
|
|
|
let session = this.session;
|
2018-10-05 21:46:33 +03:00
|
|
|
let authStrategy = 'authenticator:cookie';
|
2019-03-06 16:53:54 +03:00
|
|
|
let identification = this.identification;
|
|
|
|
let password = this.password;
|
2015-11-18 13:50:48 +03:00
|
|
|
|
|
|
|
session.set('skipAuthSuccessHandler', true);
|
|
|
|
|
|
|
|
this.toggleProperty('submitting');
|
|
|
|
|
|
|
|
return session.authenticate(authStrategy, identification, password).finally(() => {
|
|
|
|
this.toggleProperty('submitting');
|
|
|
|
session.set('skipAuthSuccessHandler', undefined);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2016-09-30 14:43:40 +03:00
|
|
|
_passwordConfirm() {
|
|
|
|
// Manually trigger events for input fields, ensuring legacy compatibility with
|
|
|
|
// browsers and password managers that don't send proper events on autofill
|
2021-07-14 14:27:51 +03:00
|
|
|
const inputs = document.querySelectorAll('#login input');
|
|
|
|
inputs.forEach(input => input.dispatchEvent(new Event('change')));
|
2016-09-30 14:43:40 +03:00
|
|
|
|
|
|
|
this.set('authenticationError', null);
|
|
|
|
|
2018-01-05 18:38:23 +03:00
|
|
|
return this.validate({property: 'signin'}).then(() => this._authenticate().then(() => {
|
2019-03-06 16:53:54 +03:00
|
|
|
this.notifications.closeAlerts();
|
2018-01-05 18:38:23 +03:00
|
|
|
this.send('closeModal');
|
|
|
|
return true;
|
|
|
|
}).catch((error) => {
|
|
|
|
if (error && error.payload && error.payload.errors) {
|
|
|
|
error.payload.errors.forEach((err) => {
|
|
|
|
if (isVersionMismatchError(err)) {
|
2019-03-06 16:53:54 +03:00
|
|
|
return this.notifications.showAPIError(error);
|
2018-01-05 18:38:23 +03:00
|
|
|
}
|
|
|
|
err.message = htmlSafe(err.context || err.message);
|
|
|
|
});
|
2016-09-30 14:43:40 +03:00
|
|
|
|
2019-03-06 16:53:54 +03:00
|
|
|
this.errors.add('password', 'Incorrect password');
|
|
|
|
this.hasValidated.pushObject('password');
|
2018-01-05 18:38:23 +03:00
|
|
|
this.set('authenticationError', error.payload.errors[0].message);
|
|
|
|
}
|
|
|
|
}), () => {
|
2019-03-06 16:53:54 +03:00
|
|
|
this.hasValidated.pushObject('password');
|
2017-07-06 13:18:20 +03:00
|
|
|
return false;
|
2016-09-30 14:43:40 +03:00
|
|
|
});
|
|
|
|
},
|
2015-11-18 13:50:48 +03:00
|
|
|
|
2017-01-19 14:40:31 +03:00
|
|
|
reauthenticate: task(function* () {
|
2017-09-04 22:17:04 +03:00
|
|
|
return yield this._passwordConfirm();
|
2018-01-11 20:43:23 +03:00
|
|
|
}).drop()
|
2015-11-18 13:50:48 +03:00
|
|
|
});
|