mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-21 01:41:46 +03:00
09bcbb9463
ref #5652 - validations can be in default, success or error state - adds check for 'hasValidated' if the validations haven't fired yet, the field is in the default state - hasValidated is an Ember.Array which tracks the state for each field
69 lines
2.5 KiB
JavaScript
69 lines
2.5 KiB
JavaScript
import Ember from 'ember';
|
|
import {request as ajax} from 'ic-ajax';
|
|
import ValidationEngine from 'ghost/mixins/validation-engine';
|
|
|
|
export default Ember.Controller.extend(ValidationEngine, {
|
|
newPassword: '',
|
|
ne2Password: '',
|
|
token: '',
|
|
submitting: false,
|
|
flowErrors: '',
|
|
|
|
validationType: 'reset',
|
|
|
|
ghostPaths: Ember.inject.service('ghost-paths'),
|
|
notifications: Ember.inject.service(),
|
|
|
|
email: Ember.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: function () {
|
|
this.setProperties({
|
|
newPassword: '',
|
|
ne2Password: '',
|
|
token: ''
|
|
});
|
|
},
|
|
|
|
actions: {
|
|
submit: function () {
|
|
var credentials = this.getProperties('newPassword', 'ne2Password', 'token'),
|
|
self = this;
|
|
this.set('flowErrors', '');
|
|
this.get('hasValidated').addObjects((['newPassword', 'ne2Password']));
|
|
this.validate().then(function () {
|
|
self.toggleProperty('submitting');
|
|
ajax({
|
|
url: self.get('ghostPaths.url').api('authentication', 'passwordreset'),
|
|
type: 'PUT',
|
|
data: {
|
|
passwordreset: [credentials]
|
|
}
|
|
}).then(function (resp) {
|
|
self.toggleProperty('submitting');
|
|
self.get('notifications').showAlert(resp.passwordreset[0].message, {type: 'warn', delayed: true});
|
|
self.get('session').authenticate('ghost-authenticator:oauth2-password-grant', {
|
|
identification: self.get('email'),
|
|
password: credentials.newPassword
|
|
});
|
|
}).catch(function (response) {
|
|
self.get('notifications').showAPIError(response);
|
|
self.toggleProperty('submitting');
|
|
});
|
|
}).catch(function () {
|
|
if (self.get('errors.newPassword')) {
|
|
self.set('flowErrors', self.get('errors.newPassword')[0].message);
|
|
}
|
|
|
|
if (self.get('errors.ne2Password')) {
|
|
self.set('flowErrors', self.get('errors.ne2Password')[0].message);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
});
|