From 3641eba5d4ed220504777ed30f961b6d9657662c Mon Sep 17 00:00:00 2001 From: Adam Butterworth Date: Thu, 5 Dec 2019 17:07:46 -0500 Subject: [PATCH] fix: do not publish APP_READY or APP_INIT_ERROR when failing to ensure authentication (#33) --- src/auth/index.js | 4 ++++ src/auth/tests/AuthenticatedHttpClient.test.jsx | 5 +++-- src/initialize.js | 8 +++++--- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/auth/index.js b/src/auth/index.js index cd57c825f..6a6d32a28 100644 --- a/src/auth/index.js +++ b/src/auth/index.js @@ -143,6 +143,10 @@ export const ensureAuthenticatedUser = async (redirectUrl = config.appBaseUrl) = // The user is not authenticated, send them to the login page. redirectToLogin(redirectUrl); + + const unauthorizedError = new Error('Failed to ensure the user is authenticated'); + unauthorizedError.isRedirecting = true; + throw unauthorizedError; } return getAuthenticatedUser(); diff --git a/src/auth/tests/AuthenticatedHttpClient.test.jsx b/src/auth/tests/AuthenticatedHttpClient.test.jsx index bc8e2ac85..77b182311 100644 --- a/src/auth/tests/AuthenticatedHttpClient.test.jsx +++ b/src/auth/tests/AuthenticatedHttpClient.test.jsx @@ -711,8 +711,9 @@ describe('ensureAuthenticatedUser', () => { it('attempts to refresh a missing jwt token and redirects user to login', () => { setJwtCookieTo(null); - return ensureAuthenticatedUser(`${process.env.BASE_URL}/route`).then((authenticatedUserAccessToken) => { - expect(authenticatedUserAccessToken).toBeNull(); + expect.hasAssertions(); + return ensureAuthenticatedUser(`${process.env.BASE_URL}/route`).catch((unauthorizedError) => { + expect(unauthorizedError.isRedirecting).toBe(true); expectSingleCallToJwtTokenRefresh(); expectLogin(`${process.env.BASE_URL}/route`); }); diff --git a/src/initialize.js b/src/initialize.js index 56b83981b..fc410594c 100644 --- a/src/initialize.js +++ b/src/initialize.js @@ -127,8 +127,10 @@ export async function initialize({ await handlers.ready(); publish(APP_READY); } catch (error) { - // Initialization Error - await handlers.initError(error); - publish(APP_INIT_ERROR, error); + if (!error.isRedirecting) { + // Initialization Error + await handlers.initError(error); + publish(APP_INIT_ERROR, error); + } } }