From c2ca39030b9a6ed12ab3ce80e235064d5d2781d1 Mon Sep 17 00:00:00 2001 From: Randall Morey Date: Tue, 8 Sep 2020 17:35:02 -0400 Subject: [PATCH 1/2] =?UTF-8?q?chore:=20=F0=9F=A4=96=20handle=20401s=20by?= =?UTF-8?q?=20invalidating=20session?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ui/core/app/routes/application.js | 11 +++++++++++ ui/core/tests/acceptance/authentication-test.js | 14 +++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/ui/core/app/routes/application.js b/ui/core/app/routes/application.js index e67f524e1..0874bb7f0 100644 --- a/ui/core/app/routes/application.js +++ b/ui/core/app/routes/application.js @@ -59,4 +59,15 @@ export default class ApplicationRoute extends Route.extend( invalidateSession() { this.session.invalidate(); } + + @action + error(e) { + const isUnauthenticated = e?.errors[0]?.isUnauthenticated; + if (isUnauthenticated) { + this.session.invalidate(); + return false; + } else { + return true; + } + } } diff --git a/ui/core/tests/acceptance/authentication-test.js b/ui/core/tests/acceptance/authentication-test.js index fa43f2247..f7abb618b 100644 --- a/ui/core/tests/acceptance/authentication-test.js +++ b/ui/core/tests/acceptance/authentication-test.js @@ -1,5 +1,5 @@ import { module, test } from 'qunit'; -import { visit, currentURL, fillIn, click, find, findAll } from '@ember/test-helpers'; +import { visit, currentURL, fillIn, click, find, findAll, setupOnerror } from '@ember/test-helpers'; import { setupApplicationTest } from 'ember-qunit'; import setupMirage from 'ember-cli-mirage/test-support/setup-mirage'; import { Response } from 'miragejs'; @@ -180,4 +180,16 @@ module('Acceptance | authentication', function (hooks) { assert.notOk(currentSession().isAuthenticated); assert.equal(currentURL(), authMethodGlobalAuthenticateURL); }); + + test('401 responses result in deauthentication', async function (assert) { + assert.expect(2); + authenticateSession({ scope: { id: globalScope.id, type: globalScope.type } }); + await visit(orgsURL); + assert.ok(currentSession().isAuthenticated, 'Session begins authenticated, before encountering 401'); + this.server.get('/scopes', () => new Response(401)); + setupOnerror(() => { + assert.notOk(currentSession().isAuthenticated, 'Session is unauthenticated, after encountering 401'); + }); + await visit(projectsURL); + }); }); From 63f3651c44322e20b09696f5e9aa609954d3736e Mon Sep 17 00:00:00 2001 From: Randall Morey Date: Tue, 8 Sep 2020 17:53:58 -0400 Subject: [PATCH 2/2] =?UTF-8?q?chore:=20=F0=9F=A4=96=20add=20code=20commen?= =?UTF-8?q?ts=20and=20simplify=20conditional?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ui/core/app/routes/application.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/ui/core/app/routes/application.js b/ui/core/app/routes/application.js index 0874bb7f0..0a6c58271 100644 --- a/ui/core/app/routes/application.js +++ b/ui/core/app/routes/application.js @@ -60,14 +60,19 @@ export default class ApplicationRoute extends Route.extend( this.session.invalidate(); } + /** + * Invalidates the session if a 401 error occurs and returns false to + * prevent further error handling. + * Returns true in all other cases, allowing error handling to occur (such + * as displaying the `error.hbs` template, if one exists). + */ @action error(e) { const isUnauthenticated = e?.errors[0]?.isUnauthenticated; if (isUnauthenticated) { this.session.invalidate(); return false; - } else { - return true; } + return true; } }