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
|
import React, { useState } from 'react';
import { useHistory } from 'react-router-dom';
import Button from '@material-ui/core/Button';
import FormHelperText from '@material-ui/core/FormHelperText';
import FormControl from '@material-ui/core/FormControl';
import Input from '@material-ui/core/Input';
import InputLabel from '@material-ui/core/InputLabel';
import Paper from '@material-ui/core/Paper';
import { makeStyles } from '@material-ui/core';
import t from './common/localization';
const useStyles = makeStyles(theme => ({
root: {
width: 'auto',
display: 'block', // Fix IE11 issue.
marginLeft: theme.spacing(3),
marginRight: theme.spacing(3),
[theme.breakpoints.up(400 + theme.spacing(3 * 2))]: {
width: 400,
marginLeft: 'auto',
marginRight: 'auto',
},
},
paper: {
marginTop: theme.spacing(8),
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
padding: `${theme.spacing(3)}px`,
},
logo: {
margin: `${theme.spacing(2)}px 0 ${theme.spacing(1)}px`
},
buttons: {
width: '100%',
display: 'flex',
flexDirection: 'row'
},
button: {
flex: '1 1 0',
margin: `${theme.spacing(3)}px ${theme.spacing(1)}px 0`
},
}));
const LoginPage = () => {
const [filled, setFilled] = useState(false);
const [loading, setLoading] = useState(false);
const [failed, setFailed] = useState(false);
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const classes = useStyles();
const history = useHistory();
const handleEmailChange = (event) => {
setEmail(event.target.value);
}
const handlePasswordChange = (event) => {
setPassword(event.target.value);
}
const handleRegister = () => {
// TODO: Implement registration
}
const handleLogin = (event) => {
event.preventDefault();
fetch('/api/session', { method: 'POST', body: new URLSearchParams(`email=${email}&password=${password}`) }).then(response => {
if (response.ok) {
history.push('/'); // TODO: Avoid calling sessions twice
} else {
setFailed(true);
setPassword('');
}
});
}
return (
<main className={classes.root}>
<Paper className={classes.paper}>
<img className={classes.logo} src="/logo.svg" alt="Traccar" />
<form onSubmit={handleLogin}>
<FormControl margin="normal" required fullWidth error={failed}>
<InputLabel htmlFor="email">{t('userEmail')}</InputLabel>
<Input
id="email"
name="email"
value={email}
autoComplete="email"
autoFocus
onChange={handleEmailChange} />
{failed && <FormHelperText>Invalid username or password</FormHelperText>}
</FormControl>
<FormControl margin="normal" required fullWidth>
<InputLabel htmlFor="password">{t('userPassword')}</InputLabel>
<Input
id="password"
name="password"
type="password"
value={password}
autoComplete="current-password"
onChange={handlePasswordChange} />
</FormControl>
<div className={classes.buttons}>
<Button
type="button"
variant="contained"
disabled
className={classes.button}
onClick={handleRegister}>
{t('loginRegister')}
</Button>
<Button
type="submit"
variant="contained"
color="primary"
disabled={!email || !password}
className={classes.button}>
{t('loginLogin')}
</Button>
</div>
</form>
</Paper>
</main>
);
}
export default LoginPage;
|