diff --git a/packages/common/authFunctions/src/authenticationConcurrentAwareMiddleware.ts b/packages/common/authFunctions/src/authenticationConcurrentAwareMiddleware.ts index d6d629233f..711c1d2054 100644 --- a/packages/common/authFunctions/src/authenticationConcurrentAwareMiddleware.ts +++ b/packages/common/authFunctions/src/authenticationConcurrentAwareMiddleware.ts @@ -77,7 +77,7 @@ export const authenticationConcurrentAwareMiddleware = ( logger.info("A session is active but does not match the requestors sessionId", {username, sessionId}) invalidSessionCause = "ConcurrentSession" } else { - logger.error("Request token invalid. No matching session found.", { + logger.warn("Request token invalid. No matching session found.", { tokenMappingSessionId, sessionManagementSessionId }) @@ -88,6 +88,7 @@ export const authenticationConcurrentAwareMiddleware = ( } } catch (error) { logger.error("Authentication failed returning restart login prompt", {error}) + invalidSessionCause = "InvalidSession" } // Handle timeout responses diff --git a/packages/common/authFunctions/src/authenticationMiddleware.ts b/packages/common/authFunctions/src/authenticationMiddleware.ts index 789c5f8eac..836e7cc994 100644 --- a/packages/common/authFunctions/src/authenticationMiddleware.ts +++ b/packages/common/authFunctions/src/authenticationMiddleware.ts @@ -51,17 +51,13 @@ export const authenticationMiddleware = ({ invalidSessionCause = "Timeout" authenticatedResult = null } - } else if (tokenMappingItem !== undefined) { + } else { logger.info("A session is active but does not match the requestors sessionId", {username, sessionId}) invalidSessionCause = "ConcurrentSession" - } else { - logger.error("Request token invalid. No matching session found.", { - tokenMappingSessionId - }) - invalidSessionCause = "InvalidSession" } } catch (error) { logger.error("Authentication failed returning restart login prompt", {error}) + invalidSessionCause = "InvalidSession" } if (!authenticatedResult || "isTimeout" in authenticatedResult) { request.earlyResponse = { diff --git a/packages/common/authFunctions/tests/test_authenticationConcurrentAwareMiddleware.test.ts b/packages/common/authFunctions/tests/test_authenticationConcurrentAwareMiddleware.test.ts index 77064f99bb..0b36afb56a 100644 --- a/packages/common/authFunctions/tests/test_authenticationConcurrentAwareMiddleware.test.ts +++ b/packages/common/authFunctions/tests/test_authenticationConcurrentAwareMiddleware.test.ts @@ -453,7 +453,8 @@ describe("authenticationConcurrentAwareMiddleware", () => { statusCode: 401, body: JSON.stringify({ message: "Session expired or invalid. Please log in again.", - restartLogin: true + restartLogin: true, + invalidSessionCause: "InvalidSession" }) }) expect(result).toEqual(mockRequest.earlyResponse) @@ -483,7 +484,8 @@ describe("authenticationConcurrentAwareMiddleware", () => { statusCode: 401, body: JSON.stringify({ message: "Session expired or invalid. Please log in again.", - restartLogin: true + restartLogin: true, + invalidSessionCause: "InvalidSession" }) }) expect(result).toEqual(mockRequest.earlyResponse) @@ -516,7 +518,8 @@ describe("authenticationConcurrentAwareMiddleware", () => { statusCode: 401, body: JSON.stringify({ message: "Session expired or invalid. Please log in again.", - restartLogin: true + restartLogin: true, + invalidSessionCause: "InvalidSession" }) }) expect(result).toEqual(mockRequest.earlyResponse) @@ -553,7 +556,8 @@ describe("authenticationConcurrentAwareMiddleware", () => { statusCode: 401, body: JSON.stringify({ message: "Session expired or invalid. Please log in again.", - restartLogin: true + restartLogin: true, + invalidSessionCause: "InvalidSession" }) }) expect(result).toEqual(mockRequest.earlyResponse) @@ -585,7 +589,8 @@ describe("authenticationConcurrentAwareMiddleware", () => { statusCode: 401, body: JSON.stringify({ message: "Session expired or invalid. Please log in again.", - restartLogin: true + restartLogin: true, + invalidSessionCause: "InvalidSession" }) }) expect(result).toEqual(mockRequest.earlyResponse) diff --git a/packages/common/authFunctions/tests/test_authenticationMiddleware.test.ts b/packages/common/authFunctions/tests/test_authenticationMiddleware.test.ts index f66ec7d4cb..c02f51cdb8 100644 --- a/packages/common/authFunctions/tests/test_authenticationMiddleware.test.ts +++ b/packages/common/authFunctions/tests/test_authenticationMiddleware.test.ts @@ -182,14 +182,14 @@ describe("authenticationMiddleware", () => { expect(result).toEqual(mockRequest.earlyResponse) }) - it("should return 401 when token mapping is undefined", async () => { + it("should return 401 when getTokenMapping throws an error", async () => { // Arrange const username = "test-user" const sessionId = "test-session-id" mockGetUsernameFromEvent.mockReturnValue(username) mockGetSessionIdFromEvent.mockReturnValue(sessionId) - mockGetTokenMapping.mockResolvedValue(undefined) + mockGetTokenMapping.mockRejectedValue(new Error("No matching session found")) const middleware = authenticationMiddleware({axiosInstance, ddbClient, authOptions, logger}) @@ -230,7 +230,8 @@ describe("authenticationMiddleware", () => { statusCode: 401, body: JSON.stringify({ message: "Session expired or invalid. Please log in again.", - restartLogin: true + restartLogin: true, + invalidSessionCause: "InvalidSession" }) }) expect(result).toEqual(mockRequest.earlyResponse) @@ -301,7 +302,8 @@ describe("authenticationMiddleware", () => { statusCode: 401, body: JSON.stringify({ message: "Session expired or invalid. Please log in again.", - restartLogin: true + restartLogin: true, + invalidSessionCause: "InvalidSession" }) }) expect(result).toEqual(mockRequest.earlyResponse) @@ -331,7 +333,8 @@ describe("authenticationMiddleware", () => { statusCode: 401, body: JSON.stringify({ message: "Session expired or invalid. Please log in again.", - restartLogin: true + restartLogin: true, + invalidSessionCause: "InvalidSession" }) }) expect(result).toEqual(mockRequest.earlyResponse) @@ -364,7 +367,8 @@ describe("authenticationMiddleware", () => { statusCode: 401, body: JSON.stringify({ message: "Session expired or invalid. Please log in again.", - restartLogin: true + restartLogin: true, + invalidSessionCause: "InvalidSession" }) }) expect(result).toEqual(mockRequest.earlyResponse) diff --git a/packages/cpt-ui/__tests__/PrescriptionListPage.test.tsx b/packages/cpt-ui/__tests__/PrescriptionListPage.test.tsx index fd563a57f3..b5ae6adb66 100644 --- a/packages/cpt-ui/__tests__/PrescriptionListPage.test.tsx +++ b/packages/cpt-ui/__tests__/PrescriptionListPage.test.tsx @@ -726,9 +726,8 @@ describe("PrescriptionListPage", () => { expect(mockNavigate).toHaveBeenCalledWith(FRONTEND_PATHS.NO_PRESCRIPTIONS_FOUND) }) - expect(logger.error).toHaveBeenCalledWith( - "A patient was returned, but they do not have any prescriptions.", - emptyResponse + expect(logger.info).toHaveBeenCalledWith( + "A patient was returned, but they do not have any prescriptions." ) }) }) diff --git a/packages/cpt-ui/src/helpers/userInfo.tsx b/packages/cpt-ui/src/helpers/userInfo.tsx index e987f9cb24..842cedce96 100644 --- a/packages/cpt-ui/src/helpers/userInfo.tsx +++ b/packages/cpt-ui/src/helpers/userInfo.tsx @@ -81,7 +81,7 @@ export const getTrackerUserInfo = async (): Promise => { } if (!invalidSessionCause) { - logger.error("Error fetching tracker user info:", err) + logger.warn("Error fetching tracker user info:", err) } else { logger.warn("trackerUserInfo triggered restart login due to:", invalidSessionCause) } diff --git a/packages/cpt-ui/src/pages/PrescriptionListPage.tsx b/packages/cpt-ui/src/pages/PrescriptionListPage.tsx index 3c018907ea..588ee04aeb 100644 --- a/packages/cpt-ui/src/pages/PrescriptionListPage.tsx +++ b/packages/cpt-ui/src/pages/PrescriptionListPage.tsx @@ -115,9 +115,8 @@ export default function PrescriptionListPage() { searchResults.pastPrescriptions.length === 0 && searchResults.futurePrescriptions.length === 0 ) { - logger.error( - "A patient was returned, but they do not have any prescriptions.", - searchResults + logger.info( + "A patient was returned, but they do not have any prescriptions." ) setPatientDetails(searchResults.patient) setPatientFallback(searchResults.patientFallback)