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
|
import React, { useState } from "react";
import PropTypes from "prop-types";
import Button from "@mui/material/Button";
import Paper from "@mui/material/Paper";
import TextField from "@mui/material/TextField";
function Login(props) {
let [email, setEmail] = useState("");
let [password, setPassword] = useState("");
let [loginFailed, setLoginFailed] = useState(false);
// NOTE: btoa() limits email, password to ASCII
function login(email, password) {
fetch(window.injectedEnv.JWT_URL, {
method: "POST",
headers: { Authorization: "Basic " + btoa(email + ":" + password) }
})
.then(resp => {
if (resp.status === 401) {
// TODO: CORS fails on requests with bad credentials, so
// this path is never taken. Fix this in auth-server-poc.
// Unauthorized: Wrong email/password
setLoginFailed(true);
return;
}
if (resp.status !== 200)
throw new Error(
`Unexpected HTTP response code from JWT server: ${resp.status} ${resp.statusText}`
);
return resp;
})
.then(resp => resp.json())
.then(data => {
props.setToken(data.access_token);
});
}
function logout() {
props.clearToken();
}
return (
<div id="login-container">
<Paper id="login" variant="outlined" color="green">
<TextField
type="text"
name="email"
placeholder="Username..."
onChange={event => {
setEmail(event.target.value);
}}
onKeyDown={event => {
if (event.key === "Enter") login(email, password);
}}
/>
<TextField
type="password"
placeholder="Password..."
name="password"
onChange={event => {
setPassword(event.target.value);
}}
onKeyDown={event => {
if (event.key === "Enter") login(email, password);
}}
sx={{
marginTop: "1em"
}}
/>
<Button
variant="contained"
className="submit"
onClick={e => {
login(email, password);
}}
sx={{
marginTop: "1em"
}}
>
Sign in
</Button>
{loginFailed && (
<p className="error">Wrong username or password</p>
)}
</Paper>
</div>
);
}
Login.propTypes = {
setToken: PropTypes.func.isRequired,
setError: PropTypes.func.isRequired
};
export default Login;
|