From 3bc51b325ab43e8b54ced8ef5e10f5393f68bbbd Mon Sep 17 00:00:00 2001 From: sanjam chhatwal Date: Fri, 25 Aug 2023 15:01:41 +0400 Subject: [PATCH 1/8] fix: redirect issue for homepage --- src/contexts/auth/auth.context.tsx | 1 + src/hooks/useAuthParams/index.tsx | 1 - src/hooks/useLoginUrl/index.tsx | 5 ++++- src/pages/auth.tsx | 7 ++++++- src/utils/index.ts | 11 +++++++++-- 5 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/contexts/auth/auth.context.tsx b/src/contexts/auth/auth.context.tsx index 960ff3795..296f8d60a 100644 --- a/src/contexts/auth/auth.context.tsx +++ b/src/contexts/auth/auth.context.tsx @@ -4,6 +4,7 @@ export interface IUserLoginAccount { name: string; token: string; currency: string; + route: string; } export type IUserAccounts = AuthorizeResponse['authorize']['account_list']; 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/index.tsx b/src/hooks/useLoginUrl/index.tsx index 63b31045b..ad06b13af 100644 --- a/src/hooks/useLoginUrl/index.tsx +++ b/src/hooks/useLoginUrl/index.tsx @@ -4,7 +4,10 @@ import { useCallback } from 'react'; const useLoginUrl = () => { const getUrl = useCallback((language = 'en') => { const { appId, oauth } = getServerConfig(); - return generateLoginUrl(language, oauth, appId); + const pathname = window.location.pathname; + const route = pathname.replace(/\//g, '%2F'); + + return generateLoginUrl(language, oauth, appId, route); }, []); return { getUrl }; diff --git a/src/pages/auth.tsx b/src/pages/auth.tsx index ce218cbbe..95002c706 100644 --- a/src/pages/auth.tsx +++ b/src/pages/auth.tsx @@ -8,15 +8,20 @@ import useAuthContext from '../hooks/useAuthContext'; export default function Auth(): JSX.Element { const { search } = useLocation(); // to get the search params + console.log('search', search); const { is_logged_in } = useAuthContext(); const { checkUrlParams } = useAuthParams(); + const params = new URLSearchParams(search); + + const redirect_route = params.get('route').replace(/%2F/g, '/'); + useEffect(() => { checkUrlParams(search); }, [checkUrlParams, search]); if (is_logged_in) { - return ; + return ; } return ( diff --git a/src/utils/index.ts b/src/utils/index.ts index fe833f267..70adf0fd6 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -123,9 +123,11 @@ export const getAccountsFromSearchParams = (searchParams: string) => { name: params.get(`acct${queryIndex}`), token: params.get(`token${queryIndex}`), currency: params.get(`cur${queryIndex}`), + route: params.get(`route`), }); } } + console.log('accounts', accounts); return accounts; }; @@ -158,8 +160,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 { From 5b1cccc56e95ca601f343eed8b2860859a749d5d Mon Sep 17 00:00:00 2001 From: sanjam chhatwal Date: Fri, 25 Aug 2023 15:29:39 +0400 Subject: [PATCH 2/8] fix: redirect issue for homepage --- src/contexts/auth/auth.context.tsx | 1 - src/pages/auth.tsx | 1 - src/utils/__tests__/utils.test.ts | 3 ++- src/utils/index.ts | 1 - 4 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/contexts/auth/auth.context.tsx b/src/contexts/auth/auth.context.tsx index 296f8d60a..960ff3795 100644 --- a/src/contexts/auth/auth.context.tsx +++ b/src/contexts/auth/auth.context.tsx @@ -4,7 +4,6 @@ export interface IUserLoginAccount { name: string; token: string; currency: string; - route: string; } export type IUserAccounts = AuthorizeResponse['authorize']['account_list']; diff --git a/src/pages/auth.tsx b/src/pages/auth.tsx index 95002c706..1e9e87fe3 100644 --- a/src/pages/auth.tsx +++ b/src/pages/auth.tsx @@ -8,7 +8,6 @@ import useAuthContext from '../hooks/useAuthContext'; export default function Auth(): JSX.Element { const { search } = useLocation(); // to get the search params - console.log('search', search); const { is_logged_in } = useAuthContext(); const { checkUrlParams } = useAuthParams(); 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 70adf0fd6..42230a99d 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -123,7 +123,6 @@ export const getAccountsFromSearchParams = (searchParams: string) => { name: params.get(`acct${queryIndex}`), token: params.get(`token${queryIndex}`), currency: params.get(`cur${queryIndex}`), - route: params.get(`route`), }); } } From 509156998b2b21c14e08f6423f8b7abeb556f6f9 Mon Sep 17 00:00:00 2001 From: sanjam chhatwal Date: Fri, 25 Aug 2023 15:36:59 +0400 Subject: [PATCH 3/8] fix: removed console --- src/utils/index.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/utils/index.ts b/src/utils/index.ts index 42230a99d..cf225b675 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -126,7 +126,6 @@ export const getAccountsFromSearchParams = (searchParams: string) => { }); } } - console.log('accounts', accounts); return accounts; }; From 8de05e9445ae94e92abbd7661c89b4fcf360837b Mon Sep 17 00:00:00 2001 From: sanjam chhatwal Date: Fri, 25 Aug 2023 15:56:19 +0400 Subject: [PATCH 4/8] fix: vercel fix --- src/pages/auth.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/auth.tsx b/src/pages/auth.tsx index 1e9e87fe3..b6cd264df 100644 --- a/src/pages/auth.tsx +++ b/src/pages/auth.tsx @@ -13,7 +13,7 @@ export default function Auth(): JSX.Element { const params = new URLSearchParams(search); - const redirect_route = params.get('route').replace(/%2F/g, '/'); + const redirect_route = params.get('route').replace(/%2F/g, '/') || '/'; useEffect(() => { checkUrlParams(search); From 025e4a103f845baa6557ef531b44f0eaaae7f8fd Mon Sep 17 00:00:00 2001 From: sanjam chhatwal Date: Fri, 25 Aug 2023 16:06:50 +0400 Subject: [PATCH 5/8] fix: vercel fix --- src/pages/auth.tsx | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/pages/auth.tsx b/src/pages/auth.tsx index b6cd264df..1f92738a6 100644 --- a/src/pages/auth.tsx +++ b/src/pages/auth.tsx @@ -11,15 +11,13 @@ export default function Auth(): JSX.Element { const { is_logged_in } = useAuthContext(); const { checkUrlParams } = useAuthParams(); - const params = new URLSearchParams(search); - - const redirect_route = params.get('route').replace(/%2F/g, '/') || '/'; - useEffect(() => { checkUrlParams(search); }, [checkUrlParams, search]); if (is_logged_in) { + const params = new URLSearchParams(search); + const redirect_route = params.get('route').replace(/%2F/g, '/') || '/'; return ; } From 10d288aec02118e3dc2e0f35b77156d0a7257686 Mon Sep 17 00:00:00 2001 From: sanjam chhatwal Date: Fri, 25 Aug 2023 16:19:49 +0400 Subject: [PATCH 6/8] fix: vercel fix --- src/hooks/useLoginUrl/index.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hooks/useLoginUrl/index.tsx b/src/hooks/useLoginUrl/index.tsx index ad06b13af..e05db4418 100644 --- a/src/hooks/useLoginUrl/index.tsx +++ b/src/hooks/useLoginUrl/index.tsx @@ -5,7 +5,7 @@ const useLoginUrl = () => { const getUrl = useCallback((language = 'en') => { const { appId, oauth } = getServerConfig(); const pathname = window.location.pathname; - const route = pathname.replace(/\//g, '%2F'); + const route = pathname.replace(/\//g, '%2F'); //replacement is done for backend to understand the route return generateLoginUrl(language, oauth, appId, route); }, []); From 03e1368a5e84386f5b0873da75507193bf0e0819 Mon Sep 17 00:00:00 2001 From: sanjam chhatwal Date: Mon, 4 Sep 2023 16:32:47 +0400 Subject: [PATCH 7/8] fix: documentation fix --- src/hooks/useLoginUrl/index.tsx | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/hooks/useLoginUrl/index.tsx b/src/hooks/useLoginUrl/index.tsx index e05db4418..ea50d2326 100644 --- a/src/hooks/useLoginUrl/index.tsx +++ b/src/hooks/useLoginUrl/index.tsx @@ -1,14 +1,19 @@ 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(); - const pathname = window.location.pathname; - const route = pathname.replace(/\//g, '%2F'); //replacement is done for backend to understand the route + 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); - }, []); + return generateLoginUrl(language, oauth, appId, route); + }, + [location], + ); return { getUrl }; }; From add090a2d7f54588956e8d9048886b674211902b Mon Sep 17 00:00:00 2001 From: sanjam chhatwal Date: Mon, 4 Sep 2023 16:51:12 +0400 Subject: [PATCH 8/8] fix: test case fix --- .../Apiexplorer/LoginDialog/__tests__/LoginDialog.test.tsx | 4 ++++ .../SubscribeRenderer/__tests__/SubscribeRenderer.test.tsx | 4 +++- src/features/dashboard/__tests__/AppManager.test.tsx | 3 +++ src/hooks/useLoginUrl/__tests__/useLoginUrl.test.tsx | 4 ++++ src/hooks/useLoginUrl/index.tsx | 1 + 5 files changed, 15 insertions(+), 1 deletion(-) 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/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 ea50d2326..ec8cfba57 100644 --- a/src/hooks/useLoginUrl/index.tsx +++ b/src/hooks/useLoginUrl/index.tsx @@ -12,6 +12,7 @@ const useLoginUrl = () => { return generateLoginUrl(language, oauth, appId, route); }, + // eslint-disable-next-line react-hooks/exhaustive-deps [location], );