1
1
import { KeyboardEvent , useEffect , useState } from 'react' ;
2
2
import { useDispatch } from 'react-redux' ;
3
- import { useHistory } from 'react-router' ;
3
+ import { useHistory , useLocation } from 'react-router' ;
4
4
import cn from 'bem-cn-lite' ;
5
5
6
6
import { Button , TextInput , Icon , Link as ExternalLink } from '@gravity-ui/uikit' ;
7
7
8
8
import { authenticate } from '../../store/reducers/authentication/authentication' ;
9
9
import { useTypedSelector } from '../../utils/hooks' ;
10
+ import { parseQuery } from '../../routes' ;
10
11
11
12
import ydbLogoIcon from '../../assets/icons/ydb.svg' ;
12
13
import showIcon from '../../assets/icons/show.svg' ;
@@ -18,13 +19,15 @@ import './Authentication.scss';
18
19
const b = cn ( 'authentication' ) ;
19
20
20
21
interface AuthenticationProps {
21
- returnUrl ?: string ;
22
22
closable ?: boolean ;
23
23
}
24
24
25
- function Authentication ( { returnUrl , closable = false } : AuthenticationProps ) {
25
+ function Authentication ( { closable = false } : AuthenticationProps ) {
26
26
const dispatch = useDispatch ( ) ;
27
27
const history = useHistory ( ) ;
28
+ const location = useLocation ( ) ;
29
+
30
+ const { returnUrl} = parseQuery ( location ) ;
28
31
29
32
const { error} = useTypedSelector ( ( state ) => state . authentication ) ;
30
33
@@ -58,7 +61,14 @@ function Authentication({returnUrl, closable = false}: AuthenticationProps) {
58
61
// typed dispatch required, remove error expectation after adding it
59
62
dispatch ( authenticate ( login , pass ) ) . then ( ( ) => {
60
63
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 ) ;
62
72
}
63
73
} ) ;
64
74
} ;
0 commit comments