1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00

fix: support query params in login redirects (#910)

This commit is contained in:
olav 2022-04-26 12:14:32 +02:00 committed by GitHub
parent af61090e73
commit 06232a5522
4 changed files with 55 additions and 4 deletions

View File

@ -6,9 +6,10 @@ const ProtectedRoute = ({
renderProps = {},
...rest
}) => {
const { pathname } = useLocation();
const loginLink =
pathname.length > 1 ? `/login?redirect=${pathname}` : '/login';
const { pathname, search } = useLocation();
const redirect = encodeURIComponent(pathname + search);
const loginLink = `/login?redirect=${redirect}`;
return (
<Route
{...rest}

View File

@ -8,6 +8,7 @@ import Authentication from '../Authentication/Authentication';
import { useAuthDetails } from 'hooks/api/getters/useAuth/useAuthDetails';
import { useAuthUser } from 'hooks/api/getters/useAuth/useAuthUser';
import { Redirect } from 'react-router-dom';
import { parseRedirectParam } from 'component/user/Login/parseRedirectParam';
const Login = () => {
const styles = useStyles();
@ -18,7 +19,7 @@ const Login = () => {
const redirect = query.get('redirect') || '/';
if (user) {
return <Redirect to={redirect} />;
return <Redirect to={parseRedirectParam(redirect)} />;
}
return (

View File

@ -0,0 +1,33 @@
import { parseRedirectParam } from 'component/user/Login/parseRedirectParam';
test('parseRedirectParam should parse an empty redirect param', async () => {
expect(parseRedirectParam('')).toEqual({
pathname: '/',
search: '',
});
});
test('parseRedirectParam should parse the pathname', async () => {
expect(parseRedirectParam(encodeURIComponent('/foo'))).toEqual({
pathname: '/foo',
search: '',
});
});
test('parseRedirectParam should parse the search query', async () => {
expect(parseRedirectParam(encodeURIComponent('/foo?a=1&b=2'))).toEqual({
pathname: '/foo',
search: '?a=1&b=2',
});
});
test('parseRedirectParam should ignore external domains', async () => {
expect(
parseRedirectParam(
encodeURIComponent('https://example.com/foo?a=1&b=2')
)
).toEqual({
pathname: '/foo',
search: '?a=1&b=2',
});
});

View File

@ -0,0 +1,16 @@
interface IRedirectParam {
pathname: string;
search: string;
}
export const parseRedirectParam = (redirect: string): IRedirectParam => {
const url = new URL(
decodeURIComponent(redirect),
window.location.protocol + '//' + window.location.host
);
return {
pathname: url.pathname,
search: url.search,
};
};