Skip to content
This repository was archived by the owner on May 13, 2024. It is now read-only.

Commit 6e8824f

Browse files
authored
Merge pull request #244 from sanjam-deriv/reqfix
fix: redirect issue
2 parents ab1484d + add090a commit 6e8824f

File tree

9 files changed

+39
-10
lines changed

9 files changed

+39
-10
lines changed

src/features/Apiexplorer/LoginDialog/__tests__/LoginDialog.test.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import LoginDialog from '..';
33
import userEvent from '@testing-library/user-event';
44
import { screen, render } from '@testing-library/react';
55

6+
jest.mock('@docusaurus/router', () => ({
7+
useLocation: jest.fn(),
8+
}));
9+
610
describe('LoginDialog', () => {
711
test('if sign up button is clickable', async () => {
812
location.assign = jest.fn();

src/features/Apiexplorer/SubscribeRenderer/__tests__/SubscribeRenderer.test.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import { IPlaygroundContext } from '@site/src/contexts/playground/playground.con
1111
import { TSocketEndpointNames } from '@site/src/configs/websocket/types';
1212

1313
jest.mock('@site/src/hooks/useScrollTo');
14-
14+
jest.mock('@docusaurus/router', () => ({
15+
useLocation: jest.fn(),
16+
}));
1517
jest.mock('@site/src/hooks/useAuthContext');
1618

1719
const mockUseAuthContext = useAuthContext as jest.MockedFunction<() => Partial<IAuthContext>>;

src/features/dashboard/__tests__/AppManager.test.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import { AppManager } from '..';
77
import { useTable } from 'react-table';
88

99
jest.mock('@site/src/hooks/useAuthContext');
10+
jest.mock('@docusaurus/router', () => ({
11+
useLocation: jest.fn(),
12+
}));
1013

1114
const mockUseAuthContext = useAuthContext as jest.MockedFunction<
1215
() => Partial<ReturnType<typeof useAuthContext>>

src/hooks/useAuthParams/index.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import useAuthContext from '../useAuthContext';
44

55
const useAuthParams = () => {
66
const { updateLoginAccounts } = useAuthContext();
7-
87
const checkUrlParams = useCallback(
98
(searchParams: string) => {
109
// if we got something in the search params, start processing it otherwise do nothing!

src/hooks/useLoginUrl/__tests__/useLoginUrl.test.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ jest
77
.spyOn(utils, 'getServerConfig')
88
.mockReturnValue({ appId: '1234', serverUrl: 'test.binary.sx', oauth: 'test.oauth.sx' });
99

10+
jest.mock('@docusaurus/router', () => ({
11+
useLocation: jest.fn(),
12+
}));
13+
1014
describe('Use Login URL', () => {
1115
afterEach(() => {
1216
jest.clearAllMocks();

src/hooks/useLoginUrl/index.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
import { generateLoginUrl, getServerConfig } from '@site/src/utils';
22
import { useCallback } from 'react';
3+
import { useLocation } from '@docusaurus/router';
34

45
const useLoginUrl = () => {
5-
const getUrl = useCallback((language = 'en') => {
6-
const { appId, oauth } = getServerConfig();
7-
return generateLoginUrl(language, oauth, appId);
8-
}, []);
6+
const location = useLocation();
7+
const getUrl = useCallback(
8+
(language = 'en') => {
9+
const { appId, oauth } = getServerConfig();
10+
const pathname = window.location.pathname;
11+
const route = pathname.replace(/\//g, '%2F'); //replacement is done for backend to understand the route
12+
13+
return generateLoginUrl(language, oauth, appId, route);
14+
},
15+
// eslint-disable-next-line react-hooks/exhaustive-deps
16+
[location],
17+
);
918

1019
return { getUrl };
1120
};

src/pages/auth.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ export default function Auth(): JSX.Element {
1616
}, [checkUrlParams, search]);
1717

1818
if (is_logged_in) {
19-
return <Redirect to={'/'} />;
19+
const params = new URLSearchParams(search);
20+
const redirect_route = params.get('route').replace(/%2F/g, '/') || '/';
21+
return <Redirect to={redirect_route} />;
2022
}
2123

2224
return (

src/utils/__tests__/utils.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,8 @@ describe('Get Server Config', () => {
188188

189189
describe('Generate Login Url', () => {
190190
it('Should generate correct url', () => {
191-
const result = utils.generateLoginUrl('es', 'test.server.ws', '5544');
191+
const route = window.location.pathname;
192+
const result = utils.generateLoginUrl('es', 'test.server.ws', '5544', route);
192193
expect(result).toMatch(/l=es/);
193194
expect(result).toMatch(/https:\/\/test.server.ws/);
194195
expect(result).toMatch(/app_id=5544/);

src/utils/index.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,13 @@ export const getServerConfig = () => {
158158
}
159159
};
160160

161-
export const generateLoginUrl = (language: string, oauthUrl: string, appId: string) => {
162-
return `https://${oauthUrl}/oauth2/authorize?app_id=${appId}&l=${language}`;
161+
export const generateLoginUrl = (
162+
language: string,
163+
oauthUrl: string,
164+
appId: string,
165+
route: string,
166+
) => {
167+
return `https://${oauthUrl}/oauth2/authorize?app_id=${appId}&l=${language}&route=${route}`;
163168
};
164169

165170
interface IScopesLike {

0 commit comments

Comments
 (0)