Skip to content

Commit

Permalink
fix: do not publish APP_READY or APP_INIT_ERROR when failing to ensur…
Browse files Browse the repository at this point in the history
…e authentication (#33)
  • Loading branch information
Adam Butterworth authored Dec 5, 2019
1 parent f98aa4f commit 3641eba
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 3 additions & 2 deletions src/auth/tests/AuthenticatedHttpClient.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
});
Expand Down
8 changes: 5 additions & 3 deletions src/initialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

0 comments on commit 3641eba

Please sign in to comment.