2016-06-30 13:21:47 +03:00
|
|
|
import $ from 'jquery';
|
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';
|
2017-08-22 10:53:26 +03:00
|
|
|
import {computed} from '@ember/object';
|
|
|
|
import {htmlSafe} from '@ember/string';
|
2016-06-30 17:45:02 +03:00
|
|
|
import {isVersionMismatchError} from 'ghost-admin/services/ajax';
|
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, {
|
|
|
|
validationType: 'signin',
|
|
|
|
|
|
|
|
authenticationError: null,
|
|
|
|
|
2017-10-30 12:38:01 +03:00
|
|
|
config: service(),
|
|
|
|
notifications: service(),
|
|
|
|
session: service(),
|
2015-11-18 13:50:48 +03:00
|
|
|
|
|
|
|
identification: computed('session.user.email', function () {
|
|
|
|
return this.get('session.user.email');
|
|
|
|
}),
|
|
|
|
|
|
|
|
_authenticate() {
|
|
|
|
let session = this.get('session');
|
|
|
|
let authStrategy = 'authenticator:oauth2';
|
|
|
|
let identification = this.get('identification');
|
|
|
|
let password = this.get('password');
|
|
|
|
|
|
|
|
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
|
|
|
|
$('#login').find('input').trigger('change');
|
|
|
|
|
|
|
|
this.set('authenticationError', null);
|
|
|
|
|
2017-01-19 14:40:31 +03:00
|
|
|
return this.validate({property: 'signin'}).then(() => {
|
2017-07-06 13:18:20 +03:00
|
|
|
return this._authenticate().then(() => {
|
2016-09-30 14:43:40 +03:00
|
|
|
this.get('notifications').closeAlerts();
|
|
|
|
this.send('closeModal');
|
2017-07-06 13:18:20 +03:00
|
|
|
return true;
|
2016-09-30 14:43:40 +03:00
|
|
|
}).catch((error) => {
|
|
|
|
if (error && error.errors) {
|
|
|
|
error.errors.forEach((err) => {
|
|
|
|
if (isVersionMismatchError(err)) {
|
|
|
|
return this.get('notifications').showAPIError(error);
|
|
|
|
}
|
2017-07-06 13:18:20 +03:00
|
|
|
err.message = htmlSafe(err.context || err.message);
|
2016-09-30 14:43:40 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
this.get('errors').add('password', 'Incorrect password');
|
|
|
|
this.get('hasValidated').pushObject('password');
|
|
|
|
this.set('authenticationError', error.errors[0].message);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, () => {
|
|
|
|
this.get('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();
|
2017-01-19 14:40:31 +03:00
|
|
|
}).drop(),
|
|
|
|
|
2016-09-30 14:43:40 +03:00
|
|
|
actions: {
|
|
|
|
confirm() {
|
2017-01-19 14:40:31 +03:00
|
|
|
this.get('reauthenticate').perform();
|
2015-11-18 13:50:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|