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
|
import React, { useState } from 'react';
import { useDispatch } from 'react-redux';
import { useHistory } from 'react-router-dom';
import { sessionActions } from './store';
import Button from '@material-ui/core/Button';
import FormControl from '@material-ui/core/FormControl';
import Paper from '@material-ui/core/Paper';
import { makeStyles } from '@material-ui/core';
import TextField from '@material-ui/core/TextField';
import RegisterDialog from './RegisterDialog';
import { useSelector } from 'react-redux';
import t from './common/localization';
const useStyles = makeStyles(theme => ({
root: {
width: 'auto',
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),
},
logo: {
marginTop: theme.spacing(2)
},
buttons: {
marginTop: theme.spacing(1),
display: 'flex',
justifyContent: 'space-evenly',
'& > *': {
flexBasis: '40%',
},
},
}));
const LoginPage = () => {
const dispatch = useDispatch();
const [failed, setFailed] = useState(false);
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [registerDialogShown, setRegisterDialogShown] = useState(false);
const classes = useStyles();
const history = useHistory();
const registrationEnabled = useSelector(state => state.session.server ? state.session.server['registration'] : false);
const handleEmailChange = (event) => {
setEmail(event.target.value);
}
const handlePasswordChange = (event) => {
setPassword(event.target.value);
}
const handleRegister = () => {
setRegisterDialogShown(true);
}
const handleRegisterResult = () => {
setRegisterDialogShown(false);
}
const handleLogin = async (event) => {
event.preventDefault();
const response = await fetch('/api/session', { method: 'POST', body: new URLSearchParams(`email=${email}&password=${password}`) });
if (response.ok) {
const user = await response.json();
dispatch(sessionActions.updateUser(user));
history.push('/');
} 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}>
<TextField
margin='normal'
required
fullWidth
error={failed}
label={t('userEmail')}
name='email'
value={email}
autoComplete='email'
autoFocus
onChange={handleEmailChange}
helperText={failed && 'Invalid username or password'} />
<TextField
margin='normal'
required
fullWidth
error={failed}
label={t('userPassword')}
name='password'
value={password}
type='password'
autoComplete='current-password'
onChange={handlePasswordChange} />
<FormControl fullWidth margin='normal'>
<div className={classes.buttons}>
<Button type='button' variant='contained' onClick={handleRegister} disabled={!registrationEnabled}>
{t('loginRegister')}
</Button>
<Button type='submit' variant='contained' color='primary' disabled={!email || !password}>
{t('loginLogin')}
</Button>
</div>
</FormControl>
</form>
{registerDialogShown &&
<RegisterDialog showDialog={true} onResult={handleRegisterResult} />
}
</Paper>
</main>
);
}
export default LoginPage;
|