From 139208531783f4a75965e445b1c88515192cf12f Mon Sep 17 00:00:00 2001 From: rapoler Date: Wed, 12 Feb 2025 18:20:22 +0530 Subject: [PATCH] fix redirection error on session expiration --- studio/src/components/Authwrapper.js | 59 ++++++++++++++++++---------- 1 file changed, 38 insertions(+), 21 deletions(-) diff --git a/studio/src/components/Authwrapper.js b/studio/src/components/Authwrapper.js index fb1d75c0e..0c0bd377a 100644 --- a/studio/src/components/Authwrapper.js +++ b/studio/src/components/Authwrapper.js @@ -4,44 +4,61 @@ import { getSession } from '../actions/session'; import { login } from '../utils/zitadel'; import { addErrorNotification } from '../actions/notifications'; import { useLocation } from 'react-router-dom'; + const AuthWrapper = ({ children }) => { const dispatch = useDispatch(); const location = useLocation(); + // List of public paths that don't require authentication + const publicPaths = [ + '/auth/login', + '/auth/registration', + '/auth/login/recovery', + '/redirect', + '/auth/verify', + ]; + useEffect(() => { checkAuthenticated(); }, [location.pathname]); + const isPublicPath = (path) => { + return publicPaths.some((publicPath) => path.includes(publicPath)); + }; + const checkAuthenticated = () => { dispatch(getSession()).then((res) => { if (!res.success) { - if (res.noToken) { + // If there's no token or authentication failed + if (res.noToken || !res.success) { const currentURL = window.location.href; const searchParams = new URLSearchParams(window.location.search); const authRequest = searchParams.get('authRequest'); - if ( - (currentURL.includes('/auth/login') || - currentURL.includes('/auth/registration') || - currentURL.includes('/redirect') || - currentURL.includes('/auth/verify') || - currentURL.includes('/auth/login/recovery')) && - authRequest - ) { + + // If current path is public and has authRequest, allow access + if (isPublicPath(currentURL) && authRequest) { return; } - window.localStorage.setItem('return_to', window.location.href); - login().then((d) => { - if (d.error) { - dispatch( - addErrorNotification({ - message: d.error, - }), - ); - return; - } - window.location.href = d.authorizeURL; - }); + // Store the return URL for post-login redirect + if (!isPublicPath(location.pathname)) { + window.localStorage.setItem('return_to', window.location.href); + } + + // Initiate login process + if (!isPublicPath(location.pathname)) { + login().then((d) => { + if (d.error) { + dispatch( + addErrorNotification({ + message: d.error, + }), + ); + return; + } + window.location.href = d.authorizeURL; + }); + } } } });