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

fix: redirect issue #244

Merged
merged 8 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<IAuthContext>>;
Expand Down
3 changes: 3 additions & 0 deletions src/features/dashboard/__tests__/AppManager.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<ReturnType<typeof useAuthContext>>
Expand Down
1 change: 0 additions & 1 deletion src/hooks/useAuthParams/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
4 changes: 4 additions & 0 deletions src/hooks/useLoginUrl/__tests__/useLoginUrl.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
17 changes: 13 additions & 4 deletions src/hooks/useLoginUrl/index.tsx
Original file line number Diff line number Diff line change
@@ -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 };
};
Expand Down
4 changes: 3 additions & 1 deletion src/pages/auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ export default function Auth(): JSX.Element {
}, [checkUrlParams, search]);

if (is_logged_in) {
return <Redirect to={'/'} />;
const params = new URLSearchParams(search);
const redirect_route = params.get('route').replace(/%2F/g, '/') || '/';
return <Redirect to={redirect_route} />;
}

return (
Expand Down
3 changes: 2 additions & 1 deletion src/utils/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/);
Expand Down
9 changes: 7 additions & 2 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading