11import { KeyboardEvent , useEffect , useState } from 'react' ;
22import { useDispatch } from 'react-redux' ;
3- import { useHistory } from 'react-router' ;
3+ import { useHistory , useLocation } from 'react-router' ;
44import cn from 'bem-cn-lite' ;
55
66import { Button , TextInput , Icon , Link as ExternalLink } from '@gravity-ui/uikit' ;
77
88import { authenticate } from '../../store/reducers/authentication/authentication' ;
99import { useTypedSelector } from '../../utils/hooks' ;
10+ import { parseQuery } from '../../routes' ;
1011
1112import ydbLogoIcon from '../../assets/icons/ydb.svg' ;
1213import showIcon from '../../assets/icons/show.svg' ;
@@ -18,13 +19,15 @@ import './Authentication.scss';
1819const b = cn ( 'authentication' ) ;
1920
2021interface 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