diff --git a/blocks/identity-block/utils/validate-redirect-url.js b/blocks/identity-block/utils/validate-redirect-url.js index 84f8a5fea..49212341f 100644 --- a/blocks/identity-block/utils/validate-redirect-url.js +++ b/blocks/identity-block/utils/validate-redirect-url.js @@ -1,24 +1,32 @@ const validateURL = (url) => { if (!url) return null; - const validationRegEx = /^\/[^/].*$/; - const valid = validationRegEx.test(url); - if (valid) { - return `${window.location.origin}${url}`; - } + try { + const urlObject = new URL(url, window.location.origin); - if (url === "/") { - return url; - } + if (urlObject.origin === window.location.origin) { + + if(urlObject.pathname === "/"){ + return urlObject.pathname + } - const urlLocation = new URL(url); + if(urlObject.pathname !== "/"){ + return `${urlObject.origin}${urlObject.pathname}` + } + } + sessionStorage.setItem("ArcXP_redirectUrl", "/"); + return "/"; + + } catch (error) { + const storedRedirect = sessionStorage.getItem("ArcXP_redirectUrl"); + if (storedRedirect && storedRedirect.startsWith("/")) { + return storedRedirect; + } - if (urlLocation.origin === window.location.origin) { - return url; + // Default to "/" + sessionStorage.setItem("ArcXP_redirectUrl", "/"); + return "/"; } - - sessionStorage.setItem("ArcXP_redirectUrl", "/"); - return "/"; }; -export default validateURL; +export default validateURL; \ No newline at end of file