Skip to content

Commit c72d06e

Browse files
committed
fix(Auth): add a step in history for auth form
1 parent 3298895 commit c72d06e

File tree

5 files changed

+63
-33
lines changed

5 files changed

+63
-33
lines changed

src/containers/App/Content.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,18 @@ function ContentWrapper(props) {
7979
<HistoryContext.Consumer>
8080
{(history) => (
8181
<Router history={history}>
82-
<ThemeProvider theme={theme}>
83-
<div className={!singleClusterMode ? b() : b({embedded: true})}>
84-
{isAuthenticated ? props.children : <Authentication />}
85-
</div>
86-
</ThemeProvider>
82+
<Switch>
83+
<Route path={routes.auth}>
84+
<Authentication closable />
85+
</Route>
86+
<Route>
87+
<ThemeProvider theme={theme}>
88+
<div className={b({embedded: singleClusterMode})}>
89+
{isAuthenticated ? props.children : <Authentication />}
90+
</div>
91+
</ThemeProvider>
92+
</Route>
93+
</Switch>
8794
</Router>
8895
)}
8996
</HistoryContext.Consumer>

src/containers/AsideNavigation/AsideNavigation.tsx

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import UserSettings from '../UserSettings/UserSettings';
2323

2424
import routes, {createHref, CLUSTER_PAGES} from '../../routes';
2525

26-
import {logout, setIsNotAuthenticated} from '../../store/reducers/authentication';
26+
import {logout} from '../../store/reducers/authentication';
2727
import {getSettingValue, setSettingValue} from '../../store/reducers/settings';
2828

2929
import {ASIDE_HEADER_COMPACT_KEY} from '../../utils/constants';
@@ -43,10 +43,15 @@ interface MenuItem {
4343
interface YbdInternalUserProps {
4444
ydbUser?: string;
4545
logout: VoidFunction;
46-
setIsNotAuthenticated: VoidFunction;
4746
}
4847

49-
function YbdInternalUser({ydbUser, logout, setIsNotAuthenticated}: YbdInternalUserProps) {
48+
function YbdInternalUser({ydbUser, logout}: YbdInternalUserProps) {
49+
const history = useHistory();
50+
51+
const handleLoginClick = () => {
52+
history.push(createHref(routes.auth, undefined, {returnUrl: encodeURI(location.href)}));
53+
};
54+
5055
return (
5156
<div className={b('internal-user')}>
5257
<div className={b('user-info-wrapper')}>
@@ -58,7 +63,7 @@ function YbdInternalUser({ydbUser, logout, setIsNotAuthenticated}: YbdInternalUs
5863
<Icon data={signOutIcon} size={16} />
5964
</Button>
6065
) : (
61-
<Button view="flat-secondary" onClick={setIsNotAuthenticated} title="login">
66+
<Button view="flat-secondary" title="login" onClick={handleLoginClick}>
6267
<Icon data={signInIcon} size={16} />
6368
</Button>
6469
)}
@@ -71,7 +76,6 @@ interface YdbUserDropdownProps {
7176
ydbUser: {
7277
login?: string;
7378
logout: VoidFunction;
74-
setIsNotAuthenticated: VoidFunction;
7579
};
7680
popupAnchor: React.RefObject<HTMLDivElement>;
7781
}
@@ -96,11 +100,7 @@ function YdbUserDropdown({isCompact, popupAnchor, ydbUser}: YdbUserDropdownProps
96100
onClosePopup={() => setIsUserDropdownVisible(false)}
97101
renderPopupContent={() => (
98102
<div className={b('ydb-user-wrapper')}>
99-
<YbdInternalUser
100-
ydbUser={ydbUser.login}
101-
logout={ydbUser.logout}
102-
setIsNotAuthenticated={ydbUser.setIsNotAuthenticated}
103-
/>
103+
<YbdInternalUser ydbUser={ydbUser.login} logout={ydbUser.logout} />
104104
</div>
105105
)}
106106
/>
@@ -112,7 +112,6 @@ interface AsideNavigationProps {
112112
ydbUser: string;
113113
compact: boolean;
114114
logout: VoidFunction;
115-
setIsNotAuthenticated: VoidFunction;
116115
setSettingValue: (name: string, value: string) => void;
117116
}
118117

@@ -260,7 +259,6 @@ function AsideNavigation(props: AsideNavigationProps) {
260259
ydbUser={{
261260
login: props.ydbUser,
262261
logout: props.logout,
263-
setIsNotAuthenticated: props.setIsNotAuthenticated,
264262
}}
265263
/>
266264
</React.Fragment>
@@ -291,7 +289,6 @@ const mapStateToProps = (state: any) => {
291289

292290
const mapDispatchToProps = {
293291
logout,
294-
setIsNotAuthenticated,
295292
setSettingValue,
296293
};
297294

src/containers/Authentication/Authentication.scss

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,10 @@
8383
&__show-password-button {
8484
margin-left: 4px;
8585
}
86+
87+
&__close {
88+
position: absolute;
89+
top: 40px;
90+
right: 40px;
91+
}
8692
}

src/containers/Authentication/Authentication.tsx

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,33 @@
11
import {KeyboardEvent, useEffect, useState} from 'react';
2+
import {useDispatch} from 'react-redux';
3+
import {useHistory} from 'react-router';
24
import cn from 'bem-cn-lite';
3-
import {connect} from 'react-redux';
5+
46
import {Button, TextInput, Icon, Link as ExternalLink} from '@gravity-ui/uikit';
5-
//@ts-ignore
7+
68
import {authenticate} from '../../store/reducers/authentication';
9+
import {useTypedSelector} from '../../utils/hooks';
710

811
import ydbLogoIcon from '../../assets/icons/ydb.svg';
912
import showIcon from '../../assets/icons/show.svg';
1013
import hideIcon from '../../assets/icons/hide.svg';
14+
import closeIcon from '../../assets/icons/close.svg';
1115

1216
import './Authentication.scss';
1317

1418
const b = cn('authentication');
1519

16-
function Authentication({authenticate, error}: any) {
20+
interface AuthenticationProps {
21+
returnUrl?: string;
22+
closable?: boolean;
23+
}
24+
25+
function Authentication({returnUrl, closable = false}: AuthenticationProps) {
26+
const dispatch = useDispatch();
27+
const history = useHistory();
28+
29+
const {error} = useTypedSelector((state) => state.authentication);
30+
1731
const [login, setLogin] = useState('');
1832
const [pass, setPass] = useState('');
1933
const [loginError, setLoginError] = useState('');
@@ -40,7 +54,13 @@ function Authentication({authenticate, error}: any) {
4054
};
4155

4256
const onLoginClick = () => {
43-
authenticate(login, pass);
57+
// @ts-expect-error
58+
// typed dispatch required, remove error expectation after adding it
59+
dispatch(authenticate(login, pass)).then(() => {
60+
if (returnUrl) {
61+
history.replace(decodeURI(returnUrl));
62+
}
63+
});
4464
};
4565

4666
const onEnterClick = (e: KeyboardEvent<HTMLInputElement | HTMLTextAreaElement>) => {
@@ -49,6 +69,10 @@ function Authentication({authenticate, error}: any) {
4969
}
5070
};
5171

72+
const onClose = () => {
73+
history.go(-1);
74+
};
75+
5276
const onTogglePasswordVisibility = () => {
5377
setShowPassword((prev) => !prev);
5478
};
@@ -106,18 +130,13 @@ function Authentication({authenticate, error}: any) {
106130
Sign in
107131
</Button>
108132
</form>
133+
{closable && history.length > 1 && (
134+
<Button onClick={onClose} className={b('close')}>
135+
<Icon data={closeIcon} size={24} />
136+
</Button>
137+
)}
109138
</section>
110139
);
111140
}
112141

113-
function mapStateToProps(state: any) {
114-
return {
115-
error: state.authentication.error,
116-
};
117-
}
118-
119-
const mapDispatchToProps = {
120-
authenticate,
121-
};
122-
123-
export default connect(mapStateToProps, mapDispatchToProps)(Authentication);
142+
export default Authentication;

src/routes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const routes = {
1616
tablet: '/tablet/:id',
1717
tabletsFilters: '/tabletsFilters',
1818
clusterPage: '/clusters/:name',
19+
auth: '/auth',
1920
};
2021

2122
export const CLUSTER_PAGES = {

0 commit comments

Comments
 (0)