mirror of
https://github.com/Unleash/unleash.git
synced 2025-07-07 01:16:28 +02:00
fix: support query params in login redirects (#910)
This commit is contained in:
parent
af61090e73
commit
06232a5522
@ -6,9 +6,10 @@ const ProtectedRoute = ({
|
|||||||
renderProps = {},
|
renderProps = {},
|
||||||
...rest
|
...rest
|
||||||
}) => {
|
}) => {
|
||||||
const { pathname } = useLocation();
|
const { pathname, search } = useLocation();
|
||||||
const loginLink =
|
const redirect = encodeURIComponent(pathname + search);
|
||||||
pathname.length > 1 ? `/login?redirect=${pathname}` : '/login';
|
const loginLink = `/login?redirect=${redirect}`;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Route
|
<Route
|
||||||
{...rest}
|
{...rest}
|
||||||
|
@ -8,6 +8,7 @@ import Authentication from '../Authentication/Authentication';
|
|||||||
import { useAuthDetails } from 'hooks/api/getters/useAuth/useAuthDetails';
|
import { useAuthDetails } from 'hooks/api/getters/useAuth/useAuthDetails';
|
||||||
import { useAuthUser } from 'hooks/api/getters/useAuth/useAuthUser';
|
import { useAuthUser } from 'hooks/api/getters/useAuth/useAuthUser';
|
||||||
import { Redirect } from 'react-router-dom';
|
import { Redirect } from 'react-router-dom';
|
||||||
|
import { parseRedirectParam } from 'component/user/Login/parseRedirectParam';
|
||||||
|
|
||||||
const Login = () => {
|
const Login = () => {
|
||||||
const styles = useStyles();
|
const styles = useStyles();
|
||||||
@ -18,7 +19,7 @@ const Login = () => {
|
|||||||
const redirect = query.get('redirect') || '/';
|
const redirect = query.get('redirect') || '/';
|
||||||
|
|
||||||
if (user) {
|
if (user) {
|
||||||
return <Redirect to={redirect} />;
|
return <Redirect to={parseRedirectParam(redirect)} />;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
33
frontend/src/component/user/Login/parseRedirectParam.test.ts
Normal file
33
frontend/src/component/user/Login/parseRedirectParam.test.ts
Normal 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',
|
||||||
|
});
|
||||||
|
});
|
16
frontend/src/component/user/Login/parseRedirectParam.ts
Normal file
16
frontend/src/component/user/Login/parseRedirectParam.ts
Normal 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,
|
||||||
|
};
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user