diff --git a/src/components/Dashboard/context/AuthContext.jsx b/src/components/Dashboard/context/AuthContext.jsx index c9e88dc4..639c9059 100644 --- a/src/components/Dashboard/context/AuthContext.jsx +++ b/src/components/Dashboard/context/AuthContext.jsx @@ -43,6 +43,13 @@ const AuthContext = createContext() const { Text } = Typography +const pendingLoginTokenRequests = new Map() + +const getSlicerAppKey = (location) => { + if (location?.pathname !== '/slicer') return null + return new URLSearchParams(location.search).get('authCode') || null +} + const extractUserFromAuthData = (authData) => { if (!authData || typeof authData !== 'object') return null if (authData.user && typeof authData.user === 'object') return authData.user @@ -113,6 +120,10 @@ const AuthProvider = ({ children }) => { } = useContext(ElectronContext) const location = useLocation() const navigate = useNavigate() + const slicerAuthPendingRef = useRef(Boolean(getSlicerAppKey(location))) + const [slicerAuthPending, setSlicerAuthPending] = useState( + slicerAuthPendingRef.current + ) const locationRef = useRef(location) locationRef.current = location const sessionRef = useRef({ @@ -123,6 +134,9 @@ const AuthProvider = ({ children }) => { }) sessionRef.current = { token, expiresAt, userProfile, authenticated } const isDesktopHelperRoute = location.pathname.startsWith('/desktop') + const slicerAppKey = getSlicerAppKey(location) + const suppressUnauthorizedModal = + isDesktopHelperRoute || Boolean(slicerAppKey) || slicerAuthPending var redirectType = 'web' @@ -458,10 +472,17 @@ const AuthProvider = ({ children }) => { setShowUnauthorizedModal(false) setShowSessionExpiredModal(false) setAuthError(null) - try { - const response = await axios.get( + + let request = pendingLoginTokenRequests.get(code) + if (!request) { + request = axios.get( `${config.backendUrl}/auth/${redirectType}/token?code=${code}` ) + pendingLoginTokenRequests.set(code, request) + } + + try { + const response = await request if (response.status === 200 && response.data) { logger.debug('Got auth token!') @@ -513,6 +534,8 @@ const AuthProvider = ({ children }) => { } setAuthenticated(false) } finally { + slicerAuthPendingRef.current = false + setSlicerAuthPending(false) setLoading(false) setRetreivedTokenFromCookies(true) } @@ -582,7 +605,11 @@ const AuthProvider = ({ children }) => { if (hadSession) { clearPersistedSession() } - if (showSessionExpiredModal == false) { + if ( + showSessionExpiredModal == false && + !slicerAuthPendingRef.current && + !getSlicerAppKey(locationRef.current) + ) { setShowUnauthorizedModal(true) } }, [clearPersistedSession, showSessionExpiredModal]) @@ -768,13 +795,25 @@ const AuthProvider = ({ children }) => { } const authCode = new URLSearchParams(location.search).get('authCode') || null + const slicerKey = getSlicerAppKey(location) if ( authCode != null && processedAuthCodeRef.current !== authCode ) { processedAuthCodeRef.current = authCode + if (slicerKey) { + slicerAuthPendingRef.current = true + setSlicerAuthPending(true) + setShowUnauthorizedModal(false) + setInitialized(true) + } getLoginToken(authCode) - } else if ( + return + } + if (slicerAuthPendingRef.current || slicerKey) { + return + } + if ( token == null && retreivedTokenFromCookies == true && initialized == false && @@ -786,10 +825,9 @@ const AuthProvider = ({ children }) => { } }, [ checkAuthStatus, - location.search, + location, getLoginToken, initialized, - location.pathname, navigate, token, retreivedTokenFromCookies @@ -870,7 +908,7 @@ const AuthProvider = ({ children }) => { } open={ showUnauthorizedModal && - !isDesktopHelperRoute && + !suppressUnauthorizedModal && !authenticated && !loading && !showAuthErrorModal