From d0214b78fa8ab67a6515032965d1f3bb99e4ef25 Mon Sep 17 00:00:00 2001 From: Greg Hoin Date: Mon, 8 Nov 2021 15:51:06 +0100 Subject: [PATCH 1/3] Handle both unauthenticated error messages --- ui/app/routes/application.ts | 10 ++++++++-- ui/tests/acceptance/auth-test.ts | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 ui/tests/acceptance/auth-test.ts diff --git a/ui/app/routes/application.ts b/ui/app/routes/application.ts index aa1a919ac43..633c3f0b81f 100644 --- a/ui/app/routes/application.ts +++ b/ui/app/routes/application.ts @@ -4,7 +4,7 @@ import Transition from '@ember/routing/-private/transition'; import { action } from '@ember/object'; import { inject as service } from '@ember/service'; -const ErrInvalidToken = 'invalid authentication token'; +const ErrsInvalidToken = ['invalid authentication token', 'Authorization token is not supplied']; export default class Application extends Route { @service session!: SessionService; @@ -20,8 +20,14 @@ export default class Application extends Route { @action error(error: Error): boolean | void { console.log(error); + let hasAuthError = false; + ErrsInvalidToken.forEach((msg) => { + if (error.message.includes(msg)) { + hasAuthError = true; + } + }); - if (error.message.includes(ErrInvalidToken)) { + if (hasAuthError) { this.session.invalidate(); this.transitionTo('auth'); } diff --git a/ui/tests/acceptance/auth-test.ts b/ui/tests/acceptance/auth-test.ts new file mode 100644 index 00000000000..cdd492a6854 --- /dev/null +++ b/ui/tests/acceptance/auth-test.ts @@ -0,0 +1,22 @@ +import { currentURL, visit } from '@ember/test-helpers'; +import { module, test } from 'qunit'; + +import login from 'waypoint/tests/helpers/login'; +import { setupApplicationTest } from 'ember-qunit'; +import { setupMirage } from 'ember-cli-mirage/test-support'; + +module('Acceptance | auth', function (hooks: NestedHooks) { + setupApplicationTest(hooks); + setupMirage(hooks); + + test('redirects to /auth from authenticated routes when logged out', async function (assert) { + await visit(`/default`); + assert.equal(currentURL(), `/auth`); + }); + + test('does not redirect to /auth from authenticated routes when logged in', async function (assert) { + await login(); + await visit(`/default`); + assert.equal(currentURL(), `/default`); + }); +}); From 8c9083b98480e050f09224825d5822a2297cdf27 Mon Sep 17 00:00:00 2001 From: Greg Hoin Date: Mon, 8 Nov 2021 17:22:56 +0100 Subject: [PATCH 2/3] add error code check --- ui/app/routes/application.ts | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/ui/app/routes/application.ts b/ui/app/routes/application.ts index 633c3f0b81f..0fb5fec5f21 100644 --- a/ui/app/routes/application.ts +++ b/ui/app/routes/application.ts @@ -6,6 +6,10 @@ import { inject as service } from '@ember/service'; const ErrsInvalidToken = ['invalid authentication token', 'Authorization token is not supplied']; +interface ApiError extends Error { + code: number; +} + export default class Application extends Route { @service session!: SessionService; @@ -18,15 +22,9 @@ export default class Application extends Route { } @action - error(error: Error): boolean | void { + error(error: ApiError): boolean | void { console.log(error); - let hasAuthError = false; - ErrsInvalidToken.forEach((msg) => { - if (error.message.includes(msg)) { - hasAuthError = true; - } - }); - + let hasAuthError = ErrsInvalidToken.some((msg) => error.message.includes(msg)) || error.code === 16; if (hasAuthError) { this.session.invalidate(); this.transitionTo('auth'); From 617e1127eea4f2121b9cdec7e390764fe7bb6c87 Mon Sep 17 00:00:00 2001 From: Greg Hoin Date: Mon, 8 Nov 2021 17:25:40 +0100 Subject: [PATCH 3/3] Changelog --- .changelog/2696.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/2696.txt diff --git a/.changelog/2696.txt b/.changelog/2696.txt new file mode 100644 index 00000000000..05197c52e96 --- /dev/null +++ b/.changelog/2696.txt @@ -0,0 +1,3 @@ +```release-note:bug +ui: Fix edge case issue where users would not be redirected to the authentication screen if no api token was set +```