Ghost/ghost/admin/app/controllers/reset.js
Kevin Ansfield 9bdb25d184
Fixed hosting management screen not loading after sign-in process (#15763)
refs https://github.com/TryGhost/Team/issues/2110

- dynamically defined properties on the config service did not have
autotracking set up properly if they were accessed in any way before the
property was defined, this caused problems in a number of areas because
we have both "unauthed" and "authed" sets of config and when not logged
in we had parts of the app checking for authed config properties that
don't exist until after sign-in and subsequent config re-fetch
- renamed `config` service to `configManager` and updated to only
contain methods for fetching config data
- added a `config` instance initializer that sets up a `TrackedObject`
instance with some custom properties/methods and registers it on
`config:main`
- uses application instance initializer rather than a standard
initializer because standard initializers are only called once when
setting up the test suite so we'd end up with config leaking across
tests
- added an `@inject` decorator that when used takes the property name
and injects whatever is registered at `${propertyName}:main`, this
allows us to use dependency injection for any object rather than just
services or controllers
- using `application.inject()` in the initializer was initially used but
that only works for objects that extend from `EmberObject`, the
injections weren't available in native-class glimmer components so this
decorator keeps the injection syntax consistent
  - swapped all `@service config` uses to `@inject config`
2022-11-03 11:14:36 +00:00

86 lines
2.7 KiB
JavaScript

/* eslint-disable ghost/ember/alias-model-in-controller */
import Controller from '@ember/controller';
import ValidationEngine from 'ghost-admin/mixins/validation-engine';
import {action} from '@ember/object';
import {inject} from 'ghost-admin/decorators/inject';
import {inject as service} from '@ember/service';
import {task} from 'ember-concurrency';
import {tracked} from '@glimmer/tracking';
export default class ResetController extends Controller.extend(ValidationEngine) {
@service ghostPaths;
@service notifications;
@service session;
@service ajax;
@inject config;
@tracked newPassword = '';
@tracked ne2Password = '';
@tracked token = '';
@tracked flowErrors = '';
validationType = 'reset';
get email() {
// The token base64 encodes the email (and some other stuff),
// each section is divided by a '|'. Email comes second.
return atob(this.token).split('|')[1];
}
// Used to clear sensitive information
clearData() {
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;
}
@task({drop: true})
*resetPasswordTask() {
const {email, newPassword, ne2Password, token} = this;
const authUrl = this.ghostPaths.url.api('authentication', 'password_reset');
this.flowErrors = '';
this.hasValidated.addObjects(['newPassword', 'ne2Password']);
try {
yield this.validate();
try {
let resp = yield this.ajax.put(authUrl, {
data: {
password_reset: [{newPassword, ne2Password, token}]
}
});
this.notifications.showAlert(resp.password_reset[0].message, {type: 'warn', delayed: true, key: 'password.reset'});
this.session.authenticate('authenticator:cookie', email, newPassword);
return true;
} catch (error) {
this.notifications.showAPIError(error, {key: 'password.reset'});
}
} catch (error) {
if (this.errors.errorsFor('newPassword').length) {
this.flowErrors = this.errors.errorsFor('newPassword')[0].message;
}
if (this.errors.errorsFor('ne2Password').length) {
this.flowErrors = this.errors.errorsFor('ne2Password')[0].message;
}
if (error && this.errors.length === 0) {
throw error;
}
}
}
}