Skip to content

Commit 721883c

Browse files
fix(Authentication): enable page redirect (#539)
1 parent 6a47481 commit 721883c

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

Diff for: src/containers/AsideNavigation/AsideNavigation.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ function YbdInternalUser({ydbUser, logout}: YbdInternalUserProps) {
5050
const history = useHistory();
5151

5252
const handleLoginClick = () => {
53-
history.push(createHref(routes.auth, undefined, {returnUrl: encodeURI(location.href)}));
53+
history.push(
54+
createHref(routes.auth, undefined, {returnUrl: encodeURIComponent(location.href)}),
55+
);
5456
};
5557

5658
return (

Diff for: src/containers/Authentication/Authentication.tsx

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import {KeyboardEvent, useEffect, useState} from 'react';
22
import {useDispatch} from 'react-redux';
3-
import {useHistory} from 'react-router';
3+
import {useHistory, useLocation} from 'react-router';
44
import cn from 'bem-cn-lite';
55

66
import {Button, TextInput, Icon, Link as ExternalLink} from '@gravity-ui/uikit';
77

88
import {authenticate} from '../../store/reducers/authentication/authentication';
99
import {useTypedSelector} from '../../utils/hooks';
10+
import {parseQuery} from '../../routes';
1011

1112
import ydbLogoIcon from '../../assets/icons/ydb.svg';
1213
import showIcon from '../../assets/icons/show.svg';
@@ -18,13 +19,15 @@ import './Authentication.scss';
1819
const b = cn('authentication');
1920

2021
interface AuthenticationProps {
21-
returnUrl?: string;
2222
closable?: boolean;
2323
}
2424

25-
function Authentication({returnUrl, closable = false}: AuthenticationProps) {
25+
function Authentication({closable = false}: AuthenticationProps) {
2626
const dispatch = useDispatch();
2727
const history = useHistory();
28+
const location = useLocation();
29+
30+
const {returnUrl} = parseQuery(location);
2831

2932
const {error} = useTypedSelector((state) => state.authentication);
3033

@@ -58,7 +61,14 @@ function Authentication({returnUrl, closable = false}: AuthenticationProps) {
5861
// typed dispatch required, remove error expectation after adding it
5962
dispatch(authenticate(login, pass)).then(() => {
6063
if (returnUrl) {
61-
history.replace(decodeURI(returnUrl));
64+
const decodedUrl = decodeURIComponent(returnUrl.toString());
65+
66+
// to prevent page reload we use router history
67+
// history navigates relative to origin
68+
// so we remove origin to make it work properly
69+
const url = new URL(decodedUrl);
70+
const path = url.pathname + url.search;
71+
history.replace(path);
6272
}
6373
});
6474
};

0 commit comments

Comments
 (0)