-
Notifications
You must be signed in to change notification settings - Fork 75
/
reset-password.jsx
153 lines (134 loc) · 3.87 KB
/
reset-password.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
import PropTypes from 'prop-types';
import React from 'react';
import auth from 'panoptes-client/lib/auth';
import Translate from 'react-translate-component';
import alert from '../../lib/alert';
import LoginDialog from '../../partials/login-dialog';
import SubmitEmailForm from './submit-email-form';
import NewPasswordForm from './new-password-form';
class ResetPasswordPage extends React.Component {
constructor() {
super();
this.state = {
inProgress: false,
resetSuccess: false,
resetError: null,
emailSuccess: false,
emailError: null,
emailIsValid: false
};
this.handlePasswordResetSubmit = this.handlePasswordResetSubmit.bind(this);
this.handleEmailSubmit = this.handleEmailSubmit.bind(this);
this.handleEmailChange = this.handleEmailChange.bind(this);
}
handleEmailChange(event) {
this.setState({ emailIsValid: event.target.checkValidity() });
}
handleEmailSubmit(event) {
event.preventDefault();
this.setState({
inProgress: true,
emailSuccess: false,
emailError: null
});
const email = event.target[0].value;
auth.requestPasswordReset({ email })
.then(() => {
this.setState({
emailSuccess: true,
inProgress: false
});
})
.catch((error) => {
this.setState({ emailError: error });
});
}
handlePasswordResetSubmit(event) {
event.preventDefault();
this.setState({
inProgress: true,
resetSuccess: false,
resetError: null
});
const token = this.props.location.query.reset_password_token;
const password = event.target[0].value;
const confirmation = event.target[1].value;
const passwordMatch = password === confirmation
if (!passwordMatch) {
this.setState({
inProgress: false,
resetError: <Translate
component="p"
content="resetPassword.passwordsDoNotMatch"
/>
});
return;
}
auth.resetPassword({ password, confirmation, token })
.then(() => {
this.setState({
resetSuccess: true,
});
alert(resolve => <LoginDialog onSuccess={resolve} />);
this.context.router.push('/projects');
})
.catch((error) => {
this.setState({
resetError: error.message
});
})
.then(() => {
this.setState({
inProgress: false
});
});
}
render() {
return (
<div className="centered-grid">
<Translate
component="h1"
content="resetPassword.heading"
/>
{this.props.location.query && !this.props.location.query.reset_password_token &&
<SubmitEmailForm
user={this.props.user}
onSubmit={this.handleEmailSubmit}
onChange={this.handleEmailChange}
disabled={!this.state.emailIsValid}
inProgress={this.state.inProgress}
emailSuccess={this.state.emailSuccess}
emailError={this.state.emailError}
/>}
{this.props.location.query && this.props.location.query.reset_password_token && !this.state.resetSuccess &&
<NewPasswordForm
onSubmit={this.handlePasswordResetSubmit}
disabled={this.state.resetSuccess}
inProgress={this.state.inProgress}
resetSuccess={this.state.resetSuccess}
resetError={this.state.resetError}
/>}
</div>
);
}
}
ResetPasswordPage.propTypes = {
location: PropTypes.shape({
query: PropTypes.shape({
reset_password_token: PropTypes.string
})
}),
user: PropTypes.object // eslint-disable-line react/forbid-prop-types
};
ResetPasswordPage.defaultProps = {
location: {
query: {
reset_password_token: null
}
},
user: null
};
ResetPasswordPage.contextTypes = {
router: PropTypes.object.isRequired
};
export default ResetPasswordPage;