-
Notifications
You must be signed in to change notification settings - Fork 12
/
ForgotPasswordForm.jsx
182 lines (168 loc) · 6.29 KB
/
ForgotPasswordForm.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import React from 'react';
import { Link } from 'react-router-dom';
import { withTranslation } from 'react-i18next';
import { Button, Paper, CircularProgress } from '@mui/material';
import {set, get, map, values} from 'lodash';
import APIService from '../../services/APIService';
import ForgotPasswordSuccessMessage from './ForgotPasswordSuccessMessage';
import PasswordFields from '../common/PasswordFields';
import Captcha from '../common/Captcha';
class ForgotPasswordForm extends React.Component {
constructor(props) {
super(props);
this.state = {
captcha: null,
username: props.match.params.user,
token: props.match.params.token,
user: null,
validToken: true,
notFound: false,
isLoading: true,
serverError: null,
new_password: '',
confirm_password: '',
success: false,
validPassword: false,
}
}
componentDidMount() {
if(this.props.forceReset) {
this.setState({user: this.props.user, validToken: true, isLoading: false})
} else {
this.fetchUser()
}
}
fetchUser() {
const { username } = this.state
APIService.users(username).get(null, null, {includeVerificationToken: true})
.then(response => {
if(get(response, 'detail') === 'Not found.')
this.setState({notFound: true, isLoading: false})
else {
this.setState({
user: response.data,
isLoading: false,
validToken: this.state.token === response.data.verification_token
})
}
})
}
setFieldValue(id, value) {
const newState = {...this.state}
set(newState, id, value)
this.setState(newState)
}
onSubmit = event => {
event.preventDefault();
event.stopPropagation();
const { new_password, confirm_password, token, validPassword } = this.state;
const isConfirmPasswordSameAsPassword = confirm_password === new_password;
if(!isConfirmPasswordSameAsPassword) {
this.setState({serverError: [this.props.t('user.auth.forgot_password_mismatch_error')]})
return
}
const form = document.getElementsByTagName('form')[0];
form.reportValidity()
const isFormValid = form.checkValidity()
if(isFormValid && isConfirmPasswordSameAsPassword && new_password && token && validPassword) {
const responseHandler = response => {
if(get(response, 'status') === 200)
this.setState({success: true})
else if (get(response, 'errors'))
this.setState({serverError: values(response.errors)})
}
if(this.props.onSubmit) {
this.props.onSubmit(new_password, responseHandler)
} else {
this.setState({serverError: null}, () => {
APIService.users().password().appendToUrl('reset/')
.put({token: token, new_password: new_password})
.then(responseHandler)
})
}
}
}
getNotFoundDOM() {
const { t } = this.props
return (
<React.Fragment>
<h1 style={{textAlign: 'center'}}>{t('common.not_found')}</h1>
<p>
{t('user.auth.password_reset_link_invalid')} {t('common.please')} <Link to="/accounts/password/reset">{t('user.auth.reset_again')}</Link>.
</p>
</React.Fragment>
)
}
getInvalidLinkDOM() {
const { t } = this.props
return (
<React.Fragment>
<h1 style={{textAlign: 'center'}}>{t('common.bad_token')}</h1>
<p>
{t('user.auth.password_reset_link_invalid')} {t('common.please')} <Link to="/accounts/password/reset">{t('user.auth.reset_again')}</Link>.
</p>
</React.Fragment>
)
}
getDOM() {
const { notFound, validToken, success } = this.state;
if(notFound)
return this.getNotFoundDOM()
if(!validToken)
return this.getInvalidLinkDOM()
if(success)
return <ForgotPasswordSuccessMessage />;
}
onCaptchaChange = value => this.setState({captcha: value})
render() {
const { serverError, success, notFound, validToken, isLoading, new_password, captcha, validPassword } = this.state;
const { t } = this.props
return (
<div className='col-md-12' style={{marginTop: '25px'}}>
<div className='col-md-3' />
<div className='col-md-6'>
{
isLoading ?
<CircularProgress /> :
<Paper style={{padding: '10px'}} className='login-paper'>
{
(success || notFound || !validToken) ?
this.getDOM() :
<React.Fragment>
<h1>{t('user.auth.change_password')}</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>
<PasswordFields
onChange={event => this.setFieldValue(event.target.id, event.target.value)}
passwordFieldId="new_password"
confirmPasswordFieldId="confirm_password"
password={new_password}
onBlur={valid => this.setFieldValue('validPassword', valid)}
/>
<div style={{display: 'flex', justifyContent: 'center'}}>
<Captcha onChange={this.onCaptchaChange} />
</div>
<div style={{marginTop: '20px', textAlign: 'center', marginBottom: '20px'}}>
<Button disabled={!captcha || !validPassword} onClick={this.onSubmit} type='submit' color='primary' variant='contained'>
{t('user.auth.change_password')}
</Button>
</div>
</form>
</div>
</React.Fragment>
}
</Paper>
}
</div>
<div className='col-md-3' />
</div>
)
}
}
export default withTranslation('translations')(ForgotPasswordForm);