From 746ef03bb2ca8ee31c448c7e5dd2559719a3e691 Mon Sep 17 00:00:00 2001 From: Brian Schroer Date: Mon, 21 Oct 2024 18:25:55 -0700 Subject: [PATCH] Reset app auth state if Auth0 session has expired --- app/utils/useAppContext.tsx | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/utils/useAppContext.tsx b/app/utils/useAppContext.tsx index de9e05daa..5eec79b57 100644 --- a/app/utils/useAppContext.tsx +++ b/app/utils/useAppContext.tsx @@ -6,6 +6,7 @@ import React, { useContext, } from "react"; import auth0, { Auth0Result, WebAuth } from "auth0-js"; +import { useHistory } from "react-router-dom"; import * as Sentry from "@sentry/browser"; import * as SessionCacher from "utils/SessionCacher"; import * as AuthService from "utils/AuthService"; @@ -69,6 +70,7 @@ export const AppProvider = ({ userLocation: GeoCoordinates | null; setBookmarksMenuOpen: (open: boolean) => void; }) => { + const history = useHistory(); const authObject = SessionCacher.getAuthObject(); const [authState, setAuthState] = useState(authObject); @@ -133,6 +135,11 @@ export const AppProvider = ({ }) .catch((err) => { Sentry.captureException(err); + if (typeof err === "object" && err.code === "login_required") { + setAuthState(null); + // Redirect user to log-in page now that their auth state has been reset + history.push("/log-in"); + } }); }