Skip to content

Commit 7a31c02

Browse files
authored
fix: logout-inactivity route gets stuck on loading indicator when inactivity and isLoggedIn are true (#14596)
### What? Updates logic within the `logout-inactivity` route to handle scenarios when `inactivity` and `isLoggedIn` are true, which can occur when the app is open in multiple tabs. ### Why A client found that the `/logout-inactivity` route can get stuck on the loading indicator when you are running Payload in multiple tabs. For example: 1. You have 2 tabs 2. Both tabs logout due to inactivity and redirect to `/logout-inactivity` 3. I open one tab and login without opening the second tab 4. I open the second tab, the `logout-inactivity` route is still present and now receives `inactivity: true` and `isLoggedIn: true` because you logged in using the other tab 5. This combination falls between the gaps in the `/logout-inactivity` handler and does not get redirected ### How On the `/logout-inactivity` we have a `useEffect` that checks if `isLoggedIn: true`, if yes it calls the logout handlers, if false it redirects to the login screen. In this above example, it calls the logout handler. The logout handler immediately checks `!inactivity` and skips the logic. This change moves the `inactivity` check out of the logout handler and into the useEffect condition alongside `isLoggedIn`. Now, if the user is both logged in and inactive, we skip the logout call and instead redirect straight to the login page, allowing that page to handle the redirect logic properly.
1 parent c74a40f commit 7a31c02

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

packages/next/src/views/Logout/LogoutClient.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,35 +45,34 @@ export const LogoutClient: React.FC<{
4545
const [loginRoute] = React.useState(() =>
4646
formatAdminURL({
4747
adminRoute,
48-
path: `/login${
49-
inactivity && redirect && redirect.length > 0
48+
path: `/login${inactivity && redirect && redirect.length > 0
5049
? `?redirect=${encodeURIComponent(redirect)}`
5150
: ''
52-
}`,
51+
}`,
5352
}),
5453
)
5554

5655
const { t } = useTranslation()
5756
const router = useRouter()
5857

5958
const handleLogOut = React.useCallback(async () => {
60-
if (!inactivity && !navigatingToLoginRef.current) {
59+
if (!navigatingToLoginRef.current) {
6160
navigatingToLoginRef.current = true
6261
await logOut()
6362
toast.success(t('authentication:loggedOutSuccessfully'))
6463
startRouteTransition(() => router.push(loginRoute))
6564
return
6665
}
67-
}, [inactivity, logOut, loginRoute, router, startRouteTransition, t])
66+
}, [logOut, loginRoute, router, startRouteTransition, t])
6867

6968
useEffect(() => {
70-
if (isLoggedIn) {
69+
if (isLoggedIn && !inactivity) {
7170
void handleLogOut()
7271
} else if (!navigatingToLoginRef.current) {
7372
navigatingToLoginRef.current = true
7473
startRouteTransition(() => router.push(loginRoute))
7574
}
76-
}, [handleLogOut, isLoggedIn, loginRoute, router, startRouteTransition])
75+
}, [handleLogOut, isLoggedIn, loginRoute, router, startRouteTransition, inactivity])
7776

7877
if (!isLoggedIn && inactivity) {
7978
return (

0 commit comments

Comments
 (0)