2016-06-30 13:21:47 +03:00
|
|
|
import $ from 'jquery';
|
|
|
|
import computed from 'ember-computed';
|
|
|
|
import injectService from 'ember-service/inject';
|
|
|
|
import {htmlSafe} from 'ember-string';
|
2016-05-24 15:06:59 +03:00
|
|
|
import ModalComponent from 'ghost-admin/components/modals/base';
|
|
|
|
import ValidationEngine from 'ghost-admin/mixins/validation-engine';
|
2016-06-30 17:45:02 +03:00
|
|
|
import {isVersionMismatchError} from 'ghost-admin/services/ajax';
|
2015-11-18 13:50:48 +03:00
|
|
|
|
|
|
|
export default ModalComponent.extend(ValidationEngine, {
|
|
|
|
validationType: 'signin',
|
|
|
|
|
|
|
|
submitting: false,
|
|
|
|
authenticationError: null,
|
|
|
|
|
2016-09-30 14:43:40 +03:00
|
|
|
config: injectService(),
|
2016-06-30 13:21:47 +03:00
|
|
|
notifications: injectService(),
|
|
|
|
session: injectService(),
|
2016-09-30 14:43:40 +03:00
|
|
|
torii: injectService(),
|
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);
|
|
|
|
|
|
|
|
this.validate({property: 'signin'}).then(() => {
|
|
|
|
this._authenticate().then(() => {
|
|
|
|
this.get('notifications').closeAlerts();
|
|
|
|
this.send('closeModal');
|
|
|
|
}).catch((error) => {
|
|
|
|
if (error && error.errors) {
|
|
|
|
error.errors.forEach((err) => {
|
|
|
|
if (isVersionMismatchError(err)) {
|
|
|
|
return this.get('notifications').showAPIError(error);
|
|
|
|
}
|
|
|
|
err.message = htmlSafe(err.message);
|
|
|
|
});
|
|
|
|
|
|
|
|
this.get('errors').add('password', 'Incorrect password');
|
|
|
|
this.get('hasValidated').pushObject('password');
|
|
|
|
this.set('authenticationError', error.errors[0].message);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}, () => {
|
|
|
|
this.get('hasValidated').pushObject('password');
|
|
|
|
});
|
|
|
|
},
|
2015-11-18 13:50:48 +03:00
|
|
|
|
2016-09-30 14:43:40 +03:00
|
|
|
_oauthConfirm() {
|
|
|
|
// TODO: remove duplication between signin/signup/re-auth
|
|
|
|
let authStrategy = 'authenticator:oauth2-ghost';
|
2015-11-18 13:50:48 +03:00
|
|
|
|
2016-09-30 14:43:40 +03:00
|
|
|
this.toggleProperty('submitting');
|
|
|
|
this.set('authenticationError', '');
|
|
|
|
|
|
|
|
this.get('torii')
|
|
|
|
.open('ghost-oauth2', {type: 'signin'})
|
|
|
|
.then((authentication) => {
|
|
|
|
this.get('session').set('skipAuthSuccessHandler', true);
|
|
|
|
|
|
|
|
this.get('session').authenticate(authStrategy, authentication).finally(() => {
|
|
|
|
this.get('session').set('skipAuthSuccessHandler', undefined);
|
|
|
|
|
|
|
|
this.toggleProperty('submitting');
|
|
|
|
this.get('notifications').closeAlerts();
|
2015-11-18 13:50:48 +03:00
|
|
|
this.send('closeModal');
|
|
|
|
});
|
2016-09-30 14:43:40 +03:00
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
this.toggleProperty('submitting');
|
|
|
|
this.set('authenticationError', 'Authentication with Ghost.org denied or failed');
|
2015-11-18 13:50:48 +03:00
|
|
|
});
|
2016-09-30 14:43:40 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
confirm() {
|
|
|
|
if (this.get('config.ghostOAuth')) {
|
|
|
|
return this._oauthConfirm();
|
|
|
|
} else {
|
|
|
|
return this._passwordConfirm();
|
|
|
|
}
|
2015-11-18 13:50:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|