Skip to content

Modular logistics and supply chain operating system (LSOS)

fix(2fa): stop "Unauthenticated." errors after signing out on a 2FA page

Ronald A. Richardson2b0fe4699ca2
diff --git a/console/app/controllers/console/account/auth.js b/console/app/controllers/console/account/auth.js
index 0800aacc..e1d86aa3 100644
--- a/console/app/controllers/console/account/auth.js
+++ b/console/app/controllers/console/account/auth.js
@@ -75,12 +75,16 @@ export default class ConsoleAccountAuthController extends Controller {
@tracked methods = getTwoFaMethods();
/**
- * Constructor method for the ConsoleAccountAuthController.
+ * Load the 2FA settings this page shows. Called by the route on entry.
*
- * @constructor
+ * Not done in the constructor: the router looks a route's controller up to read its
+ * query params before the parent route's authentication check has run. After a
+ * sign-out reloads the page on this URL, that lookup alone would fire these requests
+ * with no session, and each 401 would surface as an "Unauthenticated." error.
+ *
+ * @method load
*/
- constructor() {
- super(...arguments);
+ @action load() {
this.loadSystemTwoFaConfig.perform();
this.loadUserTwoFaSettings.perform();
}
diff --git a/console/app/controllers/console/admin/two-fa-settings.js b/console/app/controllers/console/admin/two-fa-settings.js
index 65a18969..6d800418 100644
--- a/console/app/controllers/console/admin/two-fa-settings.js
+++ b/console/app/controllers/console/admin/two-fa-settings.js
@@ -70,12 +70,16 @@ export default class ConsoleAdminTwoFaSettingsController extends Controller {
@tracked isLoading = false;
/**
- * Constructor method for the ConsoleAccountAuthController.
+ * Load the system 2FA config this page shows. Called by the route on entry.
*
- * @constructor
+ * Not done in the constructor: the router looks a route's controller up to read its
+ * query params before the parent route's authentication check has run. After a
+ * sign-out reloads the page on this URL, that lookup alone would fire these requests
+ * with no session, and each 401 would surface as an "Unauthenticated." error.
+ *
+ * @method load
*/
- constructor() {
- super(...arguments);
+ @action load() {
this.loadSystemTwoFaConfig.perform();
}
diff --git a/console/app/controllers/console/settings/two-fa.js b/console/app/controllers/console/settings/two-fa.js
index f1fc4a28..cce1e40e 100644
--- a/console/app/controllers/console/settings/two-fa.js
+++ b/console/app/controllers/console/settings/two-fa.js
@@ -63,12 +63,16 @@ export default class ConsoleSettingsTwoFaController extends Controller {
@tracked methods = getTwoFaMethods();
/**
- * Constructor method for the ConsoleAccountAuthController.
+ * Load the 2FA settings this page shows. Called by the route on entry.
*
- * @constructor
+ * Not done in the constructor: the router looks a route's controller up to read its
+ * query params before the parent route's authentication check has run. After a
+ * sign-out reloads the page on this URL, that lookup alone would fire these requests
+ * with no session, and each 401 would surface as an "Unauthenticated." error.
+ *
+ * @method load
*/
- constructor() {
- super(...arguments);
+ @action load() {
this.loadSystemTwoFaConfig.perform();
this.loadCompanyTwoFaSettings.perform();
this.loadUserTwoFaSettings.perform();
diff --git a/console/app/routes/console/account/auth.js b/console/app/routes/console/account/auth.js
index bad9dc73..bec1bd55 100644
--- a/console/app/routes/console/account/auth.js
+++ b/console/app/routes/console/account/auth.js
@@ -1,3 +1,9 @@
import Route from '@ember/routing/route';
-export default class ConsoleAccountAuthRoute extends Route {}
+export default class ConsoleAccountAuthRoute extends Route {
+ // Loaded here rather than in the controller's constructor — see controller.load().
+ setupController(controller) {
+ super.setupController(...arguments);
+ controller.load();
+ }
+}
diff --git a/console/app/routes/console/admin/two-fa-settings.js b/console/app/routes/console/admin/two-fa-settings.js
index 234cddf0..c6d6268d 100644
--- a/console/app/routes/console/admin/two-fa-settings.js
+++ b/console/app/routes/console/admin/two-fa-settings.js
@@ -1,3 +1,9 @@
import Route from '@ember/routing/route';
-export default class ConsoleAdminTwoFaSettingsRoute extends Route {}
+export default class ConsoleAdminTwoFaSettingsRoute extends Route {
+ // Loaded here rather than in the controller's constructor — see controller.load().
+ setupController(controller) {
+ super.setupController(...arguments);
+ controller.load();
+ }
+}
diff --git a/console/app/routes/console/settings/two-fa.js b/console/app/routes/console/settings/two-fa.js
index 1de798c3..da4ad688 100644
--- a/console/app/routes/console/settings/two-fa.js
+++ b/console/app/routes/console/settings/two-fa.js
@@ -1,3 +1,9 @@
import Route from '@ember/routing/route';
-export default class ConsoleSettingsTwoFaRoute extends Route {}
+export default class ConsoleSettingsTwoFaRoute extends Route {
+ // Loaded here rather than in the controller's constructor — see controller.load().
+ setupController(controller) {
+ super.setupController(...arguments);
+ controller.load();
+ }
+}
diff --git a/console/tests/unit/controllers/console/account/auth-test.js b/console/tests/unit/controllers/console/account/auth-test.js
index ca60d27e..21984dce 100644
--- a/console/tests/unit/controllers/console/account/auth-test.js
+++ b/console/tests/unit/controllers/console/account/auth-test.js
@@ -2,8 +2,8 @@ import { module, test } from 'qunit';
import { setupTest } from '@fleetbase/console/tests/helpers';
import Service from '@ember/service';
-// The controller's constructor immediately performs loadSystemTwoFaConfig and
-// loadUserTwoFaSettings, so service:fetch must be registered before any lookup.
+// load() — called by the route on entry — performs loadSystemTwoFaConfig and
+// loadUserTwoFaSettings, so service:fetch must be registered before it runs.
function stubServices(owner, { get = () => Promise.resolve(null), post = () => Promise.resolve({}) } = {}) {
const posts = [];
const notified = { success: [], errors: [] };
@@ -194,6 +194,7 @@ module('Unit | Controller | console/account/auth | credentials and 2FA', functio
this.build = async () => {
const controller = this.owner.lookup('controller:console/account/auth');
+ controller.load();
await controller.loadSystemTwoFaConfig.last;
await controller.loadUserTwoFaSettings.last;
return controller;
@@ -201,7 +202,15 @@ module('Unit | Controller | console/account/auth | credentials and 2FA', functio
this.notifications = () => this.owner.lookup('service:notifications');
});
- test('it loads the system config and the user 2FA settings on construction', async function (assert) {
+ test('looking the controller up requests nothing', function (assert) {
+ // The router does this before authentication is checked — e.g. when a sign-out
+ // reloads the page on this URL — so it must not fire authenticated requests.
+ this.owner.lookup('controller:console/account/auth');
+
+ assert.deepEqual(this.requests, []);
+ });
+
+ test('it loads the system config and the user 2FA settings when the route is entered', async function (assert) {
const controller = await this.build();
assert.deepEqual(this.requests.map((request) => request.path).sort(), ['two-fa/config', 'users/two-fa']);
diff --git a/console/tests/unit/controllers/console/admin/two-fa-settings-test.js b/console/tests/unit/controllers/console/admin/two-fa-settings-test.js
index cfda3e77..73d52c38 100644
--- a/console/tests/unit/controllers/console/admin/two-fa-settings-test.js
+++ b/console/tests/unit/controllers/console/admin/two-fa-settings-test.js
@@ -36,11 +36,22 @@ module('Unit | Controller | console/admin/two-fa-settings', function (hooks) {
this.owner.register('service:fetch', FetchStub);
this.owner.register('service:notifications', NotificationsStub);
- this.build = () => this.owner.lookup('controller:console/admin/two-fa-settings');
+ this.build = () => {
+ const controller = this.owner.lookup('controller:console/admin/two-fa-settings');
+ controller.load();
+ return controller;
+ };
this.notifications = () => this.owner.lookup('service:notifications');
});
- test('it loads the system 2FA config on construction', async function (assert) {
+ test('looking the controller up requests nothing', function (assert) {
+ // The router does this before authentication is checked; see controller.load().
+ this.owner.lookup('controller:console/admin/two-fa-settings');
+
+ assert.deepEqual(this.requests, []);
+ });
+
+ test('it loads the system 2FA config when the route is entered', async function (assert) {
const controller = this.build();
await controller.loadSystemTwoFaConfig.last;
diff --git a/console/tests/unit/controllers/console/settings/two-fa-test.js b/console/tests/unit/controllers/console/settings/two-fa-test.js
index ddfcebd2..c2c0b268 100644
--- a/console/tests/unit/controllers/console/settings/two-fa-test.js
+++ b/console/tests/unit/controllers/console/settings/two-fa-test.js
@@ -70,6 +70,7 @@ module('Unit | Controller | console/settings/two-fa | loading and saving', funct
this.build = async () => {
const controller = this.owner.lookup('controller:console/settings/two-fa');
+ controller.load();
await controller.loadSystemTwoFaConfig.last;
await controller.loadCompanyTwoFaSettings.last;
await controller.loadUserTwoFaSettings.last;
@@ -78,7 +79,14 @@ module('Unit | Controller | console/settings/two-fa | loading and saving', funct
this.notifications = () => this.owner.lookup('service:notifications');
});
- test('it loads the system, company and user settings on construction', async function (assert) {
+ test('looking the controller up requests nothing', function (assert) {
+ // The router does this before authentication is checked; see controller.load().
+ this.owner.lookup('controller:console/settings/two-fa');
+
+ assert.deepEqual(this.requests, []);
+ });
+
+ test('it loads the system, company and user settings when the route is entered', async function (assert) {
const controller = await this.build();
assert.deepEqual(this.requests.map((request) => request.path).sort(), ['companies/two-fa', 'two-fa/config', 'users/two-fa'], 'all three sources are consulted');
diff --git a/console/tests/unit/routes/console/account/auth-test.js b/console/tests/unit/routes/console/account/auth-test.js
index 07d77cd8..ecc9a0f2 100644
--- a/console/tests/unit/routes/console/account/auth-test.js
+++ b/console/tests/unit/routes/console/account/auth-test.js
@@ -8,4 +8,13 @@ module('Unit | Route | console/account/auth', function (hooks) {
let route = this.owner.lookup('route:console/account/auth');
assert.ok(route);
});
+
+ test('entering the route loads its settings', function (assert) {
+ const route = this.owner.lookup('route:console/account/auth');
+ let loads = 0;
+
+ route.setupController({ load: () => loads++ }, undefined);
+
+ assert.strictEqual(loads, 1);
+ });
});
diff --git a/console/tests/unit/routes/console/admin/two-fa-settings-test.js b/console/tests/unit/routes/console/admin/two-fa-settings-test.js
index 72e0d16a..153e87af 100644
--- a/console/tests/unit/routes/console/admin/two-fa-settings-test.js
+++ b/console/tests/unit/routes/console/admin/two-fa-settings-test.js
@@ -8,4 +8,13 @@ module('Unit | Route | console/admin/two-fa-settings', function (hooks) {
let route = this.owner.lookup('route:console/admin/two-fa-settings');
assert.ok(route);
});
+
+ test('entering the route loads its settings', function (assert) {
+ const route = this.owner.lookup('route:console/admin/two-fa-settings');
+ let loads = 0;
+
+ route.setupController({ load: () => loads++ }, undefined);
+
+ assert.strictEqual(loads, 1);
+ });
});
diff --git a/console/tests/unit/routes/console/settings/two-fa-test.js b/console/tests/unit/routes/console/settings/two-fa-test.js
index 6b157b94..a8ab4d2c 100644
--- a/console/tests/unit/routes/console/settings/two-fa-test.js
+++ b/console/tests/unit/routes/console/settings/two-fa-test.js
@@ -8,4 +8,13 @@ module('Unit | Route | console/settings/two-fa', function (hooks) {
let route = this.owner.lookup('route:console/settings/two-fa');
assert.ok(route);
});
+
+ test('entering the route loads its settings', function (assert) {
+ const route = this.owner.lookup('route:console/settings/two-fa');
+ let loads = 0;
+
+ route.setupController({ load: () => loads++ }, undefined);
+
+ assert.strictEqual(loads, 1);
+ });
});