Skip to content

Commit aee1078

Browse files
Eshan PatelPrabhakar Kumar
authored andcommitted
Fixes an issue with the handling of multiple query parameters during token authentication workflows in the front end.
1 parent 233acd5 commit aee1078

File tree

2 files changed

+37
-15
lines changed

2 files changed

+37
-15
lines changed

gui/src/components/App/App.spec.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
// Copyright (c) 2020-2023 The MathWorks, Inc.
1+
// Copyright 2020-2023 The MathWorks, Inc.
22

33
import React from 'react';
44
import { render, fireEvent } from '../../test/utils/react-test';
55
import App from './index';
6+
import * as actionCreators from '../../actionCreators';
67

78
describe('App Component', () => {
89
let initialState;
@@ -249,4 +250,24 @@ describe('App Component', () => {
249250
// Check if href has been set to loadUrl by the useEffect
250251
expect(window.location.href).toBe(url);
251252
});
252-
});
253+
254+
const tokenInQuery = '12345'
255+
it.each([
256+
[`?mwi_auth_token=${tokenInQuery}&test1=1&test2=2`, tokenInQuery],
257+
[`?test1=1&mwi_auth_token=${tokenInQuery}&test2=2`, tokenInQuery],
258+
[`?test1=1&test2=2&mwi_auth_token=${tokenInQuery}`, tokenInQuery],
259+
])
260+
("should pick the token correctly when the query parameters are '%s'", (queryParams, expectedToken) => {
261+
const url = `http://localhost.com:5555`
262+
const mockUpdateAuthStatus = jest.spyOn(actionCreators, 'updateAuthStatus');
263+
delete window.location;
264+
window.location = {
265+
origin: '/',
266+
href: url,
267+
search: queryParams
268+
};
269+
render(<App />, { initialState: initialState });
270+
expect(mockUpdateAuthStatus).toHaveBeenCalledWith(expectedToken);
271+
mockUpdateAuthStatus.mockRestore();
272+
});
273+
});

gui/src/components/App/index.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2020-2022 The MathWorks, Inc.
1+
// Copyright 2020-2022 The MathWorks, Inc.
22

33
import React, { useState, useCallback, useEffect, useMemo } from 'react';
44
import { useSelector, useDispatch } from 'react-redux';
@@ -62,6 +62,11 @@ function App() {
6262
return url.split(window.location.origin)[1].split('index.html')[0]
6363
}, [])
6464

65+
const parseQueryParams = (url) => {
66+
const queryParams = new URLSearchParams(url.search);
67+
return queryParams;
68+
}
69+
6570
const toggleOverlayVisible = useCallback(
6671
() => dispatch(setOverlayVisibility(!overlayVisible)),
6772
[overlayVisible, dispatch]
@@ -142,18 +147,14 @@ function App() {
142147
}, [loadUrl]);
143148

144149
useEffect(() => {
145-
const url = document.URL;
146-
147-
if(url.includes("?mwi_auth_token=")){
148-
var token = url.split("?mwi_auth_token=")[1];
149-
150-
if(token){
151-
dispatch(updateAuthStatus(token))
152-
}
153-
window.history.replaceState(null, '', `${baseUrl}index.html`);
154-
}
155-
}
156-
, [dispatch, baseUrl]);
150+
const queryParams = parseQueryParams(window.location);
151+
const token = queryParams.get("mwi_auth_token");
152+
153+
if(token){
154+
dispatch(updateAuthStatus(token));
155+
}
156+
window.history.replaceState(null, '', `${baseUrl}index.html`);
157+
}, [dispatch, baseUrl]);
157158

158159
// Display one of:
159160
// * Confirmation

0 commit comments

Comments
 (0)