-
Notifications
You must be signed in to change notification settings - Fork 16
/
LoginModal.js
94 lines (88 loc) · 3.26 KB
/
LoginModal.js
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
import React, { useState } from 'react'
import i18n from '../locales'
import {
Modal,
ModalTitle,
ModalContent,
ModalActions,
Button,
InputField,
} from '@dhis2/ui-core'
const staticUrl = process.env.REACT_APP_DHIS2_BASE_URL
export const LoginModal = () => {
const [server, setServer] = useState(
staticUrl || window.localStorage.DHIS2_BASE_URL || ''
)
const [username, setUsername] = useState('')
const [password, setPassword] = useState('')
const [isDirty, setIsDirty] = useState(false)
const isValid = val => val && val.length > 3
const onSubmit = async e => {
e.preventDefault()
setIsDirty(true)
if (isValid(server) && isValid(username) && isValid(password)) {
window.localStorage.DHIS2_BASE_URL = server
try {
await fetch(
`${server}/dhis-web-commons-security/login.action`,
{
method: 'POST',
credentials: 'include',
body: `j_username=${username}&j_password=${password}`,
headers: {
'X-Requested-With': 'XMLHttpRequest',
Accept: 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
},
}
)
} catch (e) {
console.log(
'TODO: This will always error and cancel the request until we get a real login endpoint!'
)
}
// TODO: Hacky solution... this shouldn't require a reload
window.location.reload()
}
}
return (
<Modal open small>
<form onSubmit={onSubmit}>
<ModalTitle>{i18n.t('Please sign in')}</ModalTitle>
<ModalContent>
{!staticUrl && (
<InputField
error={isDirty && !isValid(server)}
label={i18n.t('Server')}
name="server"
type="text"
value={server}
onChange={input => setServer(input.value)}
/>
)}
<InputField
error={isDirty && !isValid(username)}
label={i18n.t('Username')}
name="j_username"
type="text"
value={username}
onChange={input => setUsername(input.value)}
/>
<InputField
error={isDirty && !isValid(password)}
label={i18n.t('Password')}
name="j_password"
type="password"
value={password}
onChange={input => setPassword(input.value)}
/>
</ModalContent>
<ModalActions>
<Button primary type="submit">
{i18n.t('Sign in')}
</Button>
</ModalActions>
</form>
</Modal>
)
}