0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-03-11 02:12:21 -05:00

Fixed flash of button error state when redirected to 2fa screen

closes https://linear.app/tryghost/issue/ENG-1652/

- returning `undefined` from a task is equivalent to failing
- switched to returning `true` when we get the 2fa required error so the button stays in the neutral/success state
- added `SUCCESS` and `FAILURE` consts to better reflect control flow when returning from tasks and ensured we always return a value
This commit is contained in:
Kevin Ansfield 2024-10-16 18:07:59 +01:00
parent 3ed1f6a8ca
commit df6eb7bfda

View file

@ -1,6 +1,3 @@
// TODO: bump lint rules to be able to take advantage of https://github.com/ember-cli/eslint-plugin-ember/issues/560
/* eslint-disable ghost/ember/alias-model-in-controller */
import Controller, {inject as controller} from '@ember/controller'; import Controller, {inject as controller} from '@ember/controller';
import ValidationEngine from 'ghost-admin/mixins/validation-engine'; import ValidationEngine from 'ghost-admin/mixins/validation-engine';
import {action} from '@ember/object'; import {action} from '@ember/object';
@ -13,6 +10,10 @@ import {inject as service} from '@ember/service';
import {task} from 'ember-concurrency'; import {task} from 'ember-concurrency';
import {tracked} from '@glimmer/tracking'; import {tracked} from '@glimmer/tracking';
const SUCCESS = true;
const FAILURE = false;
/* eslint-disable ghost/ember/alias-model-in-controller */
export default class SigninController extends Controller.extend(ValidationEngine) { export default class SigninController extends Controller.extend(ValidationEngine) {
@controller application; @controller application;
@ -53,17 +54,18 @@ export default class SigninController extends Controller.extend(ValidationEngine
@task({drop: true}) @task({drop: true})
*authenticateTask(authStrategy, {identification, password}) { *authenticateTask(authStrategy, {identification, password}) {
try { try {
return yield this.session yield this.session.authenticate(authStrategy, {identification, password});
.authenticate(authStrategy, {identification, password}) return SUCCESS;
.then(() => true); // ensure task button transitions to "success" state
} catch (error) { } catch (error) {
if (isForbiddenError(error)) { if (isForbiddenError(error)) {
// login was successful, but 2FA verification is required
this.router.transitionTo('signin-verify'); this.router.transitionTo('signin-verify');
return; return true;
} }
if (isVersionMismatchError(error)) { if (isVersionMismatchError(error)) {
return this.notifications.showAPIError(error); this.notifications.showAPIError(error);
return FAILURE;
} }
this.signin.errors.clear(); this.signin.errors.clear();
@ -101,7 +103,7 @@ export default class SigninController extends Controller.extend(ValidationEngine
); );
} }
return false; return FAILURE;
} }
} }
@ -120,6 +122,7 @@ export default class SigninController extends Controller.extend(ValidationEngine
.perform('authenticator:cookie', {identification, password}); .perform('authenticator:cookie', {identification, password});
} catch (error) { } catch (error) {
this.flowErrors = 'Please fill out the form to sign in.'; this.flowErrors = 'Please fill out the form to sign in.';
return FAILURE;
} }
} }
@ -138,15 +141,17 @@ export default class SigninController extends Controller.extend(ValidationEngine
yield this.validate({property: 'forgotPassword'}); yield this.validate({property: 'forgotPassword'});
yield this.ajax.post(forgottenUrl, {data: {password_reset: [{email}]}}); yield this.ajax.post(forgottenUrl, {data: {password_reset: [{email}]}});
this.flowNotification = 'An email with password reset instructions has been sent.'; this.flowNotification = 'An email with password reset instructions has been sent.';
return true; return SUCCESS;
} catch (error) { } catch (error) {
// ValidationEngine throws "undefined" for failed validation // ValidationEngine throws "undefined" for failed validation
if (!error) { if (!error) {
return this.flowErrors = 'We need your email address to reset your password.'; this.flowErrors = 'We need your email address to reset your password.';
return FAILURE;
} }
if (isVersionMismatchError(error)) { if (isVersionMismatchError(error)) {
return notifications.showAPIError(error); notifications.showAPIError(error);
return FAILURE;
} }
if (error && error.payload && error.payload.errors && isEmberArray(error.payload.errors)) { if (error && error.payload && error.payload.errors && isEmberArray(error.payload.errors)) {
@ -160,6 +165,8 @@ export default class SigninController extends Controller.extend(ValidationEngine
} else { } else {
notifications.showAPIError(error, {defaultErrorText: 'There was a problem with the reset, please try again.', key: 'forgot-password.send'}); notifications.showAPIError(error, {defaultErrorText: 'There was a problem with the reset, please try again.', key: 'forgot-password.send'});
} }
return FAILURE;
} }
} }
} }