-
Notifications
You must be signed in to change notification settings - Fork 12
/
Login.jsx
168 lines (157 loc) · 6.37 KB
/
Login.jsx
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
import React from 'react';
import { Link } from 'react-router-dom';
import alertifyjs from 'alertifyjs';
import { Paper, TextField, Button, Divider, InputAdornment, IconButton } from '@mui/material';
import {
PermIdentity as UserIcon, LockOutlined as LockIcon,
Visibility as VisibilityIcon, VisibilityOff as VisibilityOffIcon,
} from '@mui/icons-material';
import { values, map, get } from 'lodash';
import APIService from '../../services/APIService';
import {
refreshCurrentUserCache, getAppliedServerConfig
} from '../../common/utils';
import VerifyEmailMessage from './VerifyEmailMessage';
class Login extends React.Component {
constructor(props) {
super(props)
this.state = {
email: null,
verificationMsg: null,
username: '',
password: '',
serverError: null,
showPassword: false,
}
}
handleSubmit = event => {
event.preventDefault()
const form = document.getElementsByTagName('form')[0];
form.reportValidity()
const { username, password } = this.state;
if(username && password)
this.handleLogin(username, password)
return false
}
onFieldChange = event => {
this.setState({[event.target.id]: event.target.value})
}
handleLogin(username, password) {
this.setState({serverError: null}, () => {
APIService.users().login().post({username: username, password: password}).then(response => {
if(get(response, 'status') === 200 && get(response, 'data.token')) {
this.afterLoginSuccess(response.data.token)
} else if(get(response, 'detail', '').match('verification email')) {
this.setState({verificationMsg: response.detail, email: get(response, 'email')})
} else if(get(response, 'non_field_errors')) {
this.setState({serverError: values(response)})
} else {
this.setState({serverError: ['Something bad happened!']})
}
})
})
}
afterLoginSuccess(token) {
localStorage.removeItem('server_configs')
localStorage.setItem('token', token)
this.cacheUserData()
}
cacheUserData() {
refreshCurrentUserCache(response => {
alertifyjs.success(`Successfully signed in as ${this.state.username}.`)
let returnToURL = response.data.url
if(get(this.props, 'location.search')) {
const queryParams = new URLSearchParams(this.props.location.search)
if(queryParams && queryParams.get('returnTo'))
returnToURL = queryParams.get('returnTo')
}
window.location.hash = '#' + returnToURL
})
}
handleClickShowPassword = () => this.setState({showPassword: !this.state.showPassword})
getTitle = () => `Sign In - ${getAppliedServerConfig().name}`
render() {
const { serverError, verificationMsg, email, showPassword } = this.state;
return (
<div className='col-md-12' style={{marginTop: '25px'}}>
<div className='col-md-3' />
<div className='col-md-6'>
<Paper style={{padding: '10px'}} className='login-paper'>
{
verificationMsg ?
<VerifyEmailMessage message={verificationMsg} email={email} />:
<React.Fragment>
<h1 style={{textAlign: 'center'}}>{this.getTitle()}</h1>
{
serverError &&
<div className='col-md-12 alert-danger'>
<ul> {map(serverError, error => (<li key={error}>{error}</li>))} </ul>
</div>
}
<div className='col-md-12 no-side-padding'>
<form>
<div className='col-md-12 no-side-padding'>
<TextField
required
id="username"
label="Username"
variant="outlined"
onChange={this.onFieldChange}
fullWidth
InputProps={{
startAdornment: (
<InputAdornment position="start">
<UserIcon style={{color: "#999999"}} />
</InputAdornment>
),
}}
/>
</div>
<div className='col-md-12 no-side-padding' style={{marginTop: '10px'}}>
<TextField
required
id="password"
label="Password"
variant="outlined"
type={showPassword ? 'text' : 'password'}
onChange={this.onFieldChange}
fullWidth
InputProps={{
startAdornment: (
<InputAdornment position="start">
<LockIcon style={{color: "#999999"}} />
</InputAdornment>
),
endAdornment:(
<InputAdornment position="end">
<IconButton
aria-label="Toggle password visibility"
onClick={this.handleClickShowPassword}
size="large">
{showPassword ? <VisibilityIcon /> : <VisibilityOffIcon />}
</IconButton>
</InputAdornment>
)
}}
/>
</div>
<div className='col-md-12 no-side-padding' style={{marginTop: '20px', textAlign: 'center', marginBottom: '20px'}}>
<Button onClick={this.handleSubmit} type='submit' color='primary' variant='contained'>Sign In</Button>
</div>
<div className='col-md-12 no-side-padding flex-vertical-center' style={{justifyContent: 'center'}}>
<Link to="/accounts/signup">Sign Up</Link>
<Divider orientation="vertical" style={{height: '16px', margin: '0 10px'}}/>
<Link to="/accounts/password/reset">Forgot Password?</Link>
</div>
</form>
</div>
</React.Fragment>
}
</Paper>
</div>
<div className='col-md-3' />
</div>
);
}
}
export default Login;