Skip to content

Commit 6d714a9

Browse files
VIA-172 AJ Handle change of authentication status to log the user out
1 parent 070d0d4 commit 6d714a9

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

src/app/_components/inactivity/InactivityDialog.test.tsx

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,6 @@ jest.mock("next-auth/react", () => ({
1818
jest.mock("@src/utils/auth/inactivity-timer");
1919
jest.mock("@src/utils/auth/user-logout");
2020

21-
const mockRouterPush = jest.fn();
22-
jest.mock("next/navigation", () => ({
23-
useRouter: () => ({
24-
push: mockRouterPush,
25-
}),
26-
}));
27-
2821
let idleSession = false;
2922
let timedOutSession = false;
3023

@@ -132,11 +125,6 @@ describe("InactivityDialog", () => {
132125
idleSession = timedOutSession = false;
133126
});
134127

135-
it("should redirect to session logout page", async () => {
136-
render(<InactivityDialog />);
137-
expect(mockRouterPush).toHaveBeenCalledWith(SESSION_LOGOUT_ROUTE);
138-
});
139-
140128
it("should not show warning when user is idle", async () => {
141129
idleSession = true;
142130

@@ -160,4 +148,19 @@ describe("InactivityDialog", () => {
160148
expect(userLogout).not.toHaveBeenCalled();
161149
});
162150
});
151+
152+
describe("when user goes from authenticated to unauthenticated", () => {
153+
beforeEach(() => {
154+
mockSession = { data: mockSessionValue, status: "authenticated" };
155+
idleSession = timedOutSession = false;
156+
});
157+
158+
it("should try to log the user out", async () => {
159+
const { rerender } = render(<InactivityDialog />);
160+
expect(userLogout).not.toHaveBeenCalled();
161+
mockSession = { data: mockSessionValue, status: "unauthenticated" };
162+
rerender(<InactivityDialog />);
163+
expect(userLogout).toHaveBeenCalledWith(true);
164+
})
165+
})
163166
});

src/app/_components/inactivity/InactivityDialog.tsx

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
"use client";
22

3-
import { SESSION_LOGOUT_ROUTE } from "@src/app/session-logout/constants";
4-
import { createRef, JSX, useEffect } from "react";
3+
import { createRef, JSX, useEffect, useRef } from "react";
54
import styles from "./styles.module.css";
65
import { userLogout } from "@src/utils/auth/user-logout";
76
import useInactivityTimer from "@src/utils/auth/inactivity-timer";
87
import { useSession } from "next-auth/react";
9-
import { useRouter } from "next/navigation";
108

119
const InactivityDialog = (): JSX.Element => {
12-
const router = useRouter();
1310
const { status } = useSession();
1411
const { isIdle, isTimedOut } = useInactivityTimer();
1512
const dialogRef = createRef<HTMLDialogElement>();
13+
const previousStatusRef = useRef<string | null>(null);
1614

1715
useEffect(() => {
1816
if (status === "authenticated") {
@@ -22,11 +20,19 @@ const InactivityDialog = (): JSX.Element => {
2220
} else if (isIdle) {
2321
dialogRef.current?.showModal();
2422
}
25-
} else if (status === "unauthenticated") {
23+
}
24+
}, [dialogRef, isIdle, isTimedOut, status]);
25+
26+
useEffect(() => {
27+
const prevStatus = previousStatusRef.current;
28+
29+
// only trigger logout if the user has been authenticated and then unauthenticated
30+
if (prevStatus === "authenticated" && status === "unauthenticated") {
2631
dialogRef.current?.close();
27-
router.push(SESSION_LOGOUT_ROUTE);
32+
userLogout(true);
2833
}
29-
}, [dialogRef, isIdle, isTimedOut, router, status]);
34+
previousStatusRef.current = status;
35+
}, [dialogRef, status]);
3036

3137
return (
3238
<dialog ref={dialogRef} className={styles.warningDialog}>

0 commit comments

Comments
 (0)