diff --git a/src/features/Apiexplorer/LoginDialog/__tests__/LoginDialog.test.tsx b/src/features/Apiexplorer/LoginDialog/__tests__/LoginDialog.test.tsx index 2d8f5b359..2945a0b57 100644 --- a/src/features/Apiexplorer/LoginDialog/__tests__/LoginDialog.test.tsx +++ b/src/features/Apiexplorer/LoginDialog/__tests__/LoginDialog.test.tsx @@ -3,6 +3,10 @@ import LoginDialog from '..'; import userEvent from '@testing-library/user-event'; import { screen, render } from '@testing-library/react'; +jest.mock('@docusaurus/router', () => ({ + useLocation: jest.fn(), +})); + describe('LoginDialog', () => { test('if sign up button is clickable', async () => { location.assign = jest.fn(); diff --git a/src/features/Apiexplorer/SubscribeRenderer/__tests__/SubscribeRenderer.test.tsx b/src/features/Apiexplorer/SubscribeRenderer/__tests__/SubscribeRenderer.test.tsx index dbd0da668..416a38138 100644 --- a/src/features/Apiexplorer/SubscribeRenderer/__tests__/SubscribeRenderer.test.tsx +++ b/src/features/Apiexplorer/SubscribeRenderer/__tests__/SubscribeRenderer.test.tsx @@ -11,7 +11,9 @@ import { IPlaygroundContext } from '@site/src/contexts/playground/playground.con import { TSocketEndpointNames } from '@site/src/configs/websocket/types'; jest.mock('@site/src/hooks/useScrollTo'); - +jest.mock('@docusaurus/router', () => ({ + useLocation: jest.fn(), +})); jest.mock('@site/src/hooks/useAuthContext'); const mockUseAuthContext = useAuthContext as jest.MockedFunction<() => Partial>; diff --git a/src/features/dashboard/__tests__/AppManager.test.tsx b/src/features/dashboard/__tests__/AppManager.test.tsx index f0593774b..1d934c478 100644 --- a/src/features/dashboard/__tests__/AppManager.test.tsx +++ b/src/features/dashboard/__tests__/AppManager.test.tsx @@ -7,6 +7,9 @@ import { AppManager } from '..'; import { useTable } from 'react-table'; jest.mock('@site/src/hooks/useAuthContext'); +jest.mock('@docusaurus/router', () => ({ + useLocation: jest.fn(), +})); const mockUseAuthContext = useAuthContext as jest.MockedFunction< () => Partial> diff --git a/src/hooks/useAuthParams/index.tsx b/src/hooks/useAuthParams/index.tsx index 848887994..129f2e4b6 100644 --- a/src/hooks/useAuthParams/index.tsx +++ b/src/hooks/useAuthParams/index.tsx @@ -4,7 +4,6 @@ import useAuthContext from '../useAuthContext'; const useAuthParams = () => { const { updateLoginAccounts } = useAuthContext(); - const checkUrlParams = useCallback( (searchParams: string) => { // if we got something in the search params, start processing it otherwise do nothing! diff --git a/src/hooks/useLoginUrl/__tests__/useLoginUrl.test.tsx b/src/hooks/useLoginUrl/__tests__/useLoginUrl.test.tsx index d54e80df2..edda0c8ef 100644 --- a/src/hooks/useLoginUrl/__tests__/useLoginUrl.test.tsx +++ b/src/hooks/useLoginUrl/__tests__/useLoginUrl.test.tsx @@ -7,6 +7,10 @@ jest .spyOn(utils, 'getServerConfig') .mockReturnValue({ appId: '1234', serverUrl: 'test.binary.sx', oauth: 'test.oauth.sx' }); +jest.mock('@docusaurus/router', () => ({ + useLocation: jest.fn(), +})); + describe('Use Login URL', () => { afterEach(() => { jest.clearAllMocks(); diff --git a/src/hooks/useLoginUrl/index.tsx b/src/hooks/useLoginUrl/index.tsx index 63b31045b..ec8cfba57 100644 --- a/src/hooks/useLoginUrl/index.tsx +++ b/src/hooks/useLoginUrl/index.tsx @@ -1,11 +1,20 @@ import { generateLoginUrl, getServerConfig } from '@site/src/utils'; import { useCallback } from 'react'; +import { useLocation } from '@docusaurus/router'; const useLoginUrl = () => { - const getUrl = useCallback((language = 'en') => { - const { appId, oauth } = getServerConfig(); - return generateLoginUrl(language, oauth, appId); - }, []); + const location = useLocation(); + const getUrl = useCallback( + (language = 'en') => { + const { appId, oauth } = getServerConfig(); + const pathname = window.location.pathname; + const route = pathname.replace(/\//g, '%2F'); //replacement is done for backend to understand the route + + return generateLoginUrl(language, oauth, appId, route); + }, + // eslint-disable-next-line react-hooks/exhaustive-deps + [location], + ); return { getUrl }; }; diff --git a/src/pages/auth.tsx b/src/pages/auth.tsx index ce218cbbe..1f92738a6 100644 --- a/src/pages/auth.tsx +++ b/src/pages/auth.tsx @@ -16,7 +16,9 @@ export default function Auth(): JSX.Element { }, [checkUrlParams, search]); if (is_logged_in) { - return ; + const params = new URLSearchParams(search); + const redirect_route = params.get('route').replace(/%2F/g, '/') || '/'; + return ; } return ( diff --git a/src/utils/__tests__/utils.test.ts b/src/utils/__tests__/utils.test.ts index 0401e4515..a101a7d3c 100644 --- a/src/utils/__tests__/utils.test.ts +++ b/src/utils/__tests__/utils.test.ts @@ -188,7 +188,8 @@ describe('Get Server Config', () => { describe('Generate Login Url', () => { it('Should generate correct url', () => { - const result = utils.generateLoginUrl('es', 'test.server.ws', '5544'); + const route = window.location.pathname; + const result = utils.generateLoginUrl('es', 'test.server.ws', '5544', route); expect(result).toMatch(/l=es/); expect(result).toMatch(/https:\/\/test.server.ws/); expect(result).toMatch(/app_id=5544/); diff --git a/src/utils/index.ts b/src/utils/index.ts index fe833f267..cf225b675 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -158,8 +158,13 @@ export const getServerConfig = () => { } }; -export const generateLoginUrl = (language: string, oauthUrl: string, appId: string) => { - return `https://${oauthUrl}/oauth2/authorize?app_id=${appId}&l=${language}`; +export const generateLoginUrl = ( + language: string, + oauthUrl: string, + appId: string, + route: string, +) => { + return `https://${oauthUrl}/oauth2/authorize?app_id=${appId}&l=${language}&route=${route}`; }; interface IScopesLike {