From 48ff9b83e8618dfaf8410718f066ee78240b91e0 Mon Sep 17 00:00:00 2001 From: Tom Butcher Date: Mon, 20 Jul 2026 02:08:44 +0100 Subject: [PATCH] Enhance AuthContext component by adding support for processing authentication codes from URL parameters. Introduce a reference to track processed auth codes, ensuring proper handling of new logins and session management. Update modal visibility logic for unauthorized access and improve loading state handling. --- .../Dashboard/context/AuthContext.jsx | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/components/Dashboard/context/AuthContext.jsx b/src/components/Dashboard/context/AuthContext.jsx index 753d686..4df271e 100644 --- a/src/components/Dashboard/context/AuthContext.jsx +++ b/src/components/Dashboard/context/AuthContext.jsx @@ -69,6 +69,7 @@ const AuthProvider = ({ children }) => { const [userProfile, setUserProfile] = useState(null) const [profileImageUrl, setProfileImageUrl] = useState(null) const profileImageUrlRef = useRef(null) + const processedAuthCodeRef = useRef(null) const [showSessionExpiredModal, setShowSessionExpiredModal] = useState(false) const [showUnauthorizedModal, setShowUnauthorizedModal] = useState(false) const [showAuthErrorModal, setShowAuthErrorModal] = useState(false) @@ -165,6 +166,8 @@ const AuthProvider = ({ children }) => { // Read token from cookies (web) or electron session storage if present useEffect(() => { let cancelled = false + const authCode = + new URLSearchParams(location.search).get('authCode') || null const load = async () => { try { @@ -227,7 +230,9 @@ const AuthProvider = ({ children }) => { } } - if (location.pathname === '/applaunch') { + // An auth code represents a newly completed login and must take precedence + // over any existing persisted session. + if (location.pathname === '/applaunch' || authCode != null) { return } @@ -431,6 +436,7 @@ const AuthProvider = ({ children }) => { setExpiresAt(nextExpiresAt) setUserProfile(nextUser) setAuthenticated(true) + setShowUnauthorizedModal(false) // Persist session (cookies on web, electron storage on desktop) const persisted = await persistSession({ @@ -469,6 +475,7 @@ const AuthProvider = ({ children }) => { setAuthenticated(false) } finally { setLoading(false) + setRetreivedTokenFromCookies(true) } }, [ @@ -723,7 +730,11 @@ const AuthProvider = ({ children }) => { useEffect(() => { const authCode = new URLSearchParams(location.search).get('authCode') || null - if (authCode != null) { + if ( + authCode != null && + processedAuthCodeRef.current !== authCode + ) { + processedAuthCodeRef.current = authCode getLoginToken(authCode) } else if ( token == null && @@ -800,7 +811,12 @@ const AuthProvider = ({ children }) => { Please log in to continue } - open={showUnauthorizedModal && !loading && !showAuthErrorModal} + open={ + showUnauthorizedModal && + !authenticated && + !loading && + !showAuthErrorModal + } onOk={() => { setShowUnauthorizedModal(false) loginWithSSO()