Skip to content

Commit

Permalink
Bug/fix for redirect to referrer (#2205)
Browse files Browse the repository at this point in the history
* add fix for bug for not redirecting to the referrer after apple login

* fix tests
  • Loading branch information
accbjt authored Aug 29, 2024
1 parent bc8933f commit a88797c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
10 changes: 9 additions & 1 deletion blocks/identity-block/components/login/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ const useLogin = ({
}
}, [appleCode, Identity]);

const isReferrerFromHost = () => {
if (!document?.referrer) return false;

const referrerURL = new URL(document.referrer);

return referrerURL.origin === window.location.origin;
};

useEffect(() => {
const searchParams = new URLSearchParams(window.location.search.substring(1));

Expand All @@ -63,7 +71,7 @@ const useLogin = ({
setRedirectQueryParam(validatedRedirectParam);
}

if (redirectToPreviousPage && document?.referrer) {
if (redirectToPreviousPage && document?.referrer && isReferrerFromHost()) {
const redirectUrlLocation = new URL(document.referrer);
let newRedirectUrl = redirectUrlLocation.pathname.includes('/pagebuilder/')
? redirectURL
Expand Down
2 changes: 1 addition & 1 deletion blocks/identity-block/components/login/index.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe("useLogin()", () => {
});

it("uses document referrer", async () => {
const referrerURL = "http://referrer.com/article/1234";
const referrerURL = "http://localhost/article/1234";
Object.defineProperty(document, "referrer", {
value: referrerURL,
configurable: true,
Expand Down
4 changes: 4 additions & 0 deletions blocks/identity-block/utils/validate-redirect-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ const validateURL = (url) => {
return `${window.location.origin}${url}`;
}

if (url === "/") {
return url;
}

const urlLocation = new URL(url);

if (urlLocation.origin === window.location.origin) {
Expand Down

0 comments on commit a88797c

Please sign in to comment.