diff --git a/src/__integration_tests__/authentication.integration.test.ts b/src/__integration_tests__/authentication.integration.test.ts index a646f8e7..f29e72f9 100644 --- a/src/__integration_tests__/authentication.integration.test.ts +++ b/src/__integration_tests__/authentication.integration.test.ts @@ -4,6 +4,8 @@ const CHECKOUT_URL = `http://localhost:1234`; const MOCK_AUTH_REDIRECT_URL = `http://localhost:8080/auth-redirect-url`; +const CALLBACK_URL = `http://localhost:1234/auth-callback?auth-code=1234`; +const PAGE_LOGIN_URL = `http://localhost:1234/inserisci-email`; jest.setTimeout(80000); @@ -39,5 +41,16 @@ describe("Checkout authentication tests", () => { expect(currentUrl).toBe(MOCK_AUTH_REDIRECT_URL); }); + it.only("Should correctly come back to login origin url", async () => { + await page.evaluate(() => { + //set item into sessionStorage and localStorage for pass the route Guard + sessionStorage.setItem('loginOriginPage', 'http://localhost:1234/inserisci-email'); + }, "it"); + await page.goto(CALLBACK_URL); + await page.waitForNavigation(); + const currentUrl = await page.evaluate(() => location.href); + console.log("Current url: " + currentUrl); + expect(currentUrl).toBe(PAGE_LOGIN_URL); + }); }); diff --git a/src/routes/AuthCallbackPage.tsx b/src/routes/AuthCallbackPage.tsx index 22058413..a97d29ed 100644 --- a/src/routes/AuthCallbackPage.tsx +++ b/src/routes/AuthCallbackPage.tsx @@ -19,11 +19,9 @@ export default function AuthCallback() { const [error, setError] = React.useState(""); const [errorModalOpen, setErrorModalOpen] = React.useState(false); - const onError = (m: string) => { + const onError = (m: string | undefined) => { window.removeEventListener("popstate", onBrowserBackEvent); window.removeEventListener("beforeunload", onBrowserUnload); - setError(m); - setErrorModalOpen(true); }; // navigate to last page from session storage @@ -49,7 +47,7 @@ export default function AuthCallback() { void (async (authCode) => { void authentication({ authCode, - onResponse: (authToken: string) => { + onResponse: (authToken: string | undefined) => { setSessionItem(SessionItems.authToken, authToken); returnToOriginPage(); }, diff --git a/src/utils/api/helper.ts b/src/utils/api/helper.ts index c647a42f..05512afe 100644 --- a/src/utils/api/helper.ts +++ b/src/utils/api/helper.ts @@ -653,8 +653,8 @@ export const authentication = async ({ onError, }: { authCode: string | null; - onResponse: (e: string) => void; - onError: (e: string) => void; + onResponse: (e: string | undefined) => void; + onError: (e: string | undefined) => void; }) => { await pipe( O.fromNullable(authCode), @@ -693,7 +693,7 @@ export const authentication = async ({ () => onError, () => onResponse ) - ); + )(myRes?.value.authToken); } else { onError(ErrorsType.GENERIC_ERROR); }