-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathAuthentication.tsx
173 lines (152 loc) · 6.02 KB
/
Authentication.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import React from 'react';
import {Eye, EyeSlash, Xmark} from '@gravity-ui/icons';
import {Button, Link as ExternalLink, Icon, TextInput} from '@gravity-ui/uikit';
import {useHistory, useLocation} from 'react-router-dom';
import {parseQuery} from '../../routes';
import {authenticationApi} from '../../store/reducers/authentication/authentication';
import {useLoginWithDatabase} from '../../store/reducers/capabilities/hooks';
import {cn} from '../../utils/cn';
import {isDatabaseError, isPasswordError, isUserError} from './utils';
import ydbLogoIcon from '../../assets/icons/ydb.svg';
import './Authentication.scss';
const b = cn('authentication');
interface AuthenticationProps {
closable?: boolean;
}
function Authentication({closable = false}: AuthenticationProps) {
const history = useHistory();
const location = useLocation();
const needDatabase = useLoginWithDatabase();
const [authenticate, {isLoading}] = authenticationApi.useAuthenticateMutation(undefined);
const {returnUrl, database: databaseFromQuery} = parseQuery(location);
const [login, setLogin] = React.useState('');
const [database, setDatabase] = React.useState(databaseFromQuery?.toString() ?? '');
const [password, setPass] = React.useState('');
const [loginError, setLoginError] = React.useState('');
const [passwordError, setPasswordError] = React.useState('');
const [databaseError, setDatabaseError] = React.useState('');
const [showPassword, setShowPassword] = React.useState(false);
const onLoginUpdate = (value: string) => {
setLogin(value);
setLoginError('');
};
const onDatabaseUpdate = (value: string) => {
setDatabase(value);
setDatabaseError('');
};
const onPassUpdate = (value: string) => {
setPass(value);
setPasswordError('');
};
const onLoginClick = () => {
authenticate({user: login, password, database})
.unwrap()
.then(() => {
if (returnUrl) {
const decodedUrl = decodeURIComponent(returnUrl.toString());
// to prevent page reload we use router history
// history navigates relative to origin
// so we remove origin to make it work properly
const url = new URL(decodedUrl);
const path = url.pathname + url.search;
history.replace(path);
}
})
.catch((error) => {
if (isUserError(error)) {
setLoginError(error.data.error);
}
if (isPasswordError(error)) {
setPasswordError(error.data.error);
}
if (isDatabaseError(error)) {
setDatabaseError(error.data.error);
}
});
};
const onEnterClick = (e: React.KeyboardEvent<HTMLInputElement | HTMLTextAreaElement>) => {
if (e.keyCode === 13) {
onLoginClick();
}
};
const onClose = () => {
history.go(-1);
};
const onTogglePasswordVisibility = () => {
setShowPassword((prev) => !prev);
};
return (
<section className={b()}>
<form className={b('form-wrapper')}>
<div className={b('header')}>
<div className={b('logo')}>
<Icon data={ydbLogoIcon} size={24} />
YDB
</div>
<ExternalLink href="https://ydb.tech/docs" target="_blank">
Documentation
</ExternalLink>
</div>
<h2 className={b('title')}>Sign in</h2>
<div className={b('field-wrapper')}>
<TextInput
value={login}
onUpdate={onLoginUpdate}
placeholder={'Username'}
error={loginError}
onKeyDown={onEnterClick}
size="l"
autoFocus
/>
</div>
<div className={b('field-wrapper')}>
<TextInput
value={password}
onUpdate={onPassUpdate}
type={showPassword ? 'text' : 'password'}
placeholder={'Password'}
error={passwordError}
onKeyDown={onEnterClick}
size="l"
/>
<Button
onClick={onTogglePasswordVisibility}
size="l"
className={b('show-password-button')}
>
<Icon data={showPassword ? EyeSlash : Eye} size={16} />
</Button>
</div>
{needDatabase && (
<div className={b('field-wrapper')}>
<TextInput
value={database}
onUpdate={onDatabaseUpdate}
placeholder={'Database'}
error={databaseError}
onKeyDown={onEnterClick}
size="l"
/>
</div>
)}
<Button
view="action"
onClick={onLoginClick}
width="max"
size="l"
disabled={Boolean(!login || loginError || passwordError)}
loading={isLoading}
className={b('button-sign-in')}
>
Sign in
</Button>
</form>
{closable && history.length > 1 && (
<Button onClick={onClose} className={b('close')}>
<Icon data={Xmark} size={24} />
</Button>
)}
</section>
);
}
export default Authentication;