diff --git a/app/components/Authenticated.tsx b/app/components/Authenticated.tsx index 8b0ac6fcb6..9a50239400 100644 --- a/app/components/Authenticated.tsx +++ b/app/components/Authenticated.tsx @@ -1,5 +1,5 @@ import { observer } from "mobx-react"; -import { useEffect } from "react"; +import { useEffect, useRef } from "react"; import { useTranslation } from "react-i18next"; import { Redirect } from "react-router-dom"; import useCurrentUser from "~/hooks/useCurrentUser"; @@ -16,6 +16,7 @@ const Authenticated = ({ children }: Props) => { const { i18n } = useTranslation(); const user = useCurrentUser({ rejectOnEmpty: false }); const language = user?.language; + const hasLoggedOut = useRef(false); // Watching for language changes here as this is the earliest point we might have the user // available and means we can start loading translations faster @@ -23,23 +24,36 @@ const Authenticated = ({ children }: Props) => { void changeLanguage(language, i18n); }, [i18n, language]); + const shouldLogout = !auth.authenticated && !auth.isFetching; + + // Passive logout when we land here without an authenticated session – note we + // intentionally do not revoke the server-side token, as that would clobber + // the session in any other tab that may have already re-authenticated. + useEffect(() => { + if (shouldLogout && !hasLoggedOut.current) { + hasLoggedOut.current = true; + void auth.logout({ + savePath: true, + clearCache: false, + revokeToken: false, + }); + } + }, [shouldLogout, auth]); + + useEffect(() => { + if (auth.logoutRedirectUri) { + window.location.href = auth.logoutRedirectUri; + } + }, [auth.logoutRedirectUri]); + if (auth.authenticated) { return children; } - if (auth.isFetching) { + if (auth.isFetching || auth.logoutRedirectUri) { return ; } - void auth.logout({ - savePath: true, - clearCache: false, - }); - - if (auth.logoutRedirectUri) { - window.location.href = auth.logoutRedirectUri; - return null; - } return ; }; diff --git a/app/stores/AuthStore.ts b/app/stores/AuthStore.ts index 7e12ef5c44..77ac60d369 100644 --- a/app/stores/AuthStore.ts +++ b/app/stores/AuthStore.ts @@ -118,7 +118,6 @@ export default class AuthStore extends Store { savePath: false, clearCache: false, revokeToken: false, - userInitiated: true, }); } } else {