2018-01-11 01:57:43 +03:00
|
|
|
/* eslint-disable ghost/ember/alias-model-in-controller */
|
2017-08-22 10:53:26 +03:00
|
|
|
import Controller from '@ember/controller';
|
2017-05-29 21:50:03 +03:00
|
|
|
import ValidationEngine from 'ghost-admin/mixins/validation-engine';
|
2022-10-07 21:07:51 +03:00
|
|
|
import {action} from '@ember/object';
|
2022-11-03 14:14:36 +03:00
|
|
|
import {inject} from 'ghost-admin/decorators/inject';
|
2017-10-30 12:38:01 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2017-01-25 16:00:58 +03:00
|
|
|
import {task} from 'ember-concurrency';
|
2022-10-07 21:07:51 +03:00
|
|
|
import {tracked} from '@glimmer/tracking';
|
2015-10-28 14:36:45 +03:00
|
|
|
|
2022-10-07 21:07:51 +03:00
|
|
|
export default class ResetController extends Controller.extend(ValidationEngine) {
|
|
|
|
@service ghostPaths;
|
|
|
|
@service notifications;
|
|
|
|
@service session;
|
|
|
|
@service ajax;
|
2022-11-03 14:14:36 +03:00
|
|
|
|
|
|
|
@inject config;
|
2018-01-11 20:43:23 +03:00
|
|
|
|
2022-10-07 21:07:51 +03:00
|
|
|
@tracked newPassword = '';
|
|
|
|
@tracked ne2Password = '';
|
|
|
|
@tracked token = '';
|
|
|
|
@tracked flowErrors = '';
|
2015-05-27 03:41:12 +03:00
|
|
|
|
2022-10-07 21:07:51 +03:00
|
|
|
validationType = 'reset';
|
2015-05-27 03:41:12 +03:00
|
|
|
|
2022-10-07 21:07:51 +03:00
|
|
|
get email() {
|
2015-05-27 03:41:12 +03:00
|
|
|
// The token base64 encodes the email (and some other stuff),
|
|
|
|
// each section is divided by a '|'. Email comes second.
|
2019-03-06 16:53:54 +03:00
|
|
|
return atob(this.token).split('|')[1];
|
2022-10-07 21:07:51 +03:00
|
|
|
}
|
2018-01-11 20:43:23 +03:00
|
|
|
|
2015-05-27 03:41:12 +03:00
|
|
|
// Used to clear sensitive information
|
2015-10-28 14:36:45 +03:00
|
|
|
clearData() {
|
2022-10-07 21:07:51 +03:00
|
|
|
this.newPassword = '';
|
|
|
|
this.ne2Password = '';
|
|
|
|
this.token = '';
|
|
|
|
|
|
|
|
document.querySelector('form#reset')?.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
handleInput(event) {
|
|
|
|
this.flowErrors = '';
|
|
|
|
this.errors.clear();
|
|
|
|
this.hasValidated.addObjects(['newPassword', 'ne2Password']);
|
|
|
|
|
|
|
|
this[event.currentTarget.name] = event.target.value;
|
|
|
|
}
|
2015-05-27 03:41:12 +03:00
|
|
|
|
2022-10-07 21:07:51 +03:00
|
|
|
@task({drop: true})
|
|
|
|
*resetPasswordTask() {
|
|
|
|
const {email, newPassword, ne2Password, token} = this;
|
|
|
|
const authUrl = this.ghostPaths.url.api('authentication', 'password_reset');
|
2017-01-25 16:00:58 +03:00
|
|
|
|
2022-10-07 21:07:51 +03:00
|
|
|
this.flowErrors = '';
|
2019-03-06 16:53:54 +03:00
|
|
|
this.hasValidated.addObjects(['newPassword', 'ne2Password']);
|
2015-10-28 14:36:45 +03:00
|
|
|
|
2017-01-25 16:00:58 +03:00
|
|
|
try {
|
|
|
|
yield this.validate();
|
|
|
|
try {
|
2019-03-06 16:53:54 +03:00
|
|
|
let resp = yield this.ajax.put(authUrl, {
|
2015-05-27 03:41:12 +03:00
|
|
|
data: {
|
2022-10-07 21:07:51 +03:00
|
|
|
password_reset: [{newPassword, ne2Password, token}]
|
2015-05-27 03:41:12 +03:00
|
|
|
}
|
|
|
|
});
|
2022-04-25 18:14:58 +03:00
|
|
|
this.notifications.showAlert(resp.password_reset[0].message, {type: 'warn', delayed: true, key: 'password.reset'});
|
2022-10-07 21:07:51 +03:00
|
|
|
this.session.authenticate('authenticator:cookie', email, newPassword);
|
2017-07-20 15:12:33 +03:00
|
|
|
return true;
|
2017-01-25 16:00:58 +03:00
|
|
|
} catch (error) {
|
2019-03-06 16:53:54 +03:00
|
|
|
this.notifications.showAPIError(error, {key: 'password.reset'});
|
2017-01-25 16:00:58 +03:00
|
|
|
}
|
|
|
|
} catch (error) {
|
2022-10-07 21:07:51 +03:00
|
|
|
if (this.errors.errorsFor('newPassword').length) {
|
|
|
|
this.flowErrors = this.errors.errorsFor('newPassword')[0].message;
|
2017-01-25 16:00:58 +03:00
|
|
|
}
|
2015-08-25 14:32:29 +03:00
|
|
|
|
2022-10-07 21:07:51 +03:00
|
|
|
if (this.errors.errorsFor('ne2Password').length) {
|
|
|
|
this.flowErrors = this.errors.errorsFor('ne2Password')[0].message;
|
2017-01-25 16:00:58 +03:00
|
|
|
}
|
2015-12-16 00:51:36 +03:00
|
|
|
|
2022-10-07 21:07:51 +03:00
|
|
|
if (error && this.errors.length === 0) {
|
2017-01-25 16:00:58 +03:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
2022-10-07 21:07:51 +03:00
|
|
|
}
|
|
|
|
}
|