This repository was archived by the owner on Apr 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathConfirmPasswordResetForm.js
135 lines (118 loc) · 3.83 KB
/
ConfirmPasswordResetForm.js
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
import React, { useState, useEffect } from 'react';
import { PasswordSelection, Form, Error } from '../../../components';
import { Title, Column, Button, Message } from 'rbx';
import { useIdentityContext } from 'react-netlify-identity';
import { useHistory } from 'react-router-dom';
import { validate } from '../../../utils/validators';
import { useTranslation } from 'react-i18next';
import * as ROUTES from '../../../constants/routes';
const errors = {
invalidToken: 'invalidToken',
isProvider: 'isProvider',
};
/**
*
* @returns {React.FC<{
* token: string
* }>} ConfirmPasswordResetForm
*/
export default function ConfirmPasswordResetForm({ token }) {
const { t } = useTranslation(['resetPassword', 'error', 'settings']);
const { replace } = useHistory();
const { recoverAccount, setUser, updateUser, user } = useIdentityContext();
const [{ password, confirmPassword }, setPassword] = useState({
password: '',
confirmPassword: '',
});
const [error, setError] = useState(null);
const [userObj, setUserObj] = useState(null);
const handleChange = ({ target: { value, name } }) => {
setPassword(data => ({ ...data, [name]: value }));
};
const handleSubmit = event => {
event.preventDefault();
if (!userObj) {
return;
}
// this is effectively a login, triggering a re-render
setUser(userObj);
};
useEffect(() => {
if (!userObj) {
// only runs if a token exists that hasn't been validated yet
async function verifyToken() {
try {
const user = await recoverAccount(token);
if (user.app_metadata.provider !== 'email') {
setError(errors.isProvider);
} else {
setUserObj(user);
}
} catch (error) {
setError(errors.invalidToken);
}
}
verifyToken();
} else if (user) {
// changes the password after re-render with login
async function doPasswordChange() {
try {
// update the password
await updateUser({ password });
// kill internal react-netlify-identity state
setUser(undefined);
// move use to login
replace(ROUTES.LOGIN.clientPath);
} catch (error) {
// ignore errors here - react-netlify-identity
}
}
doPasswordChange();
}
// react-netlify-identity functions are excluded because they trigger a re-run of verifyToken
// eslint-disable-next-line
}, [password, userObj, replace, token, user]);
const isDisabled =
!validate.password(password) || password !== confirmPassword;
return (
<Form onSubmit={handleSubmit}>
<Column.Group centered>
<Column
className="has-content-spaced-between"
widescreen={{ size: 11 }}
>
<legend>
<Title textAlign="centered" id="section-title">
{t('title')}
</Title>
</legend>
<br />
<Column.Group centered>
<Column size={11}>
{error ? (
<Message color="danger">
<Message.Header>{t('error:title')}</Message.Header>
<Message.Body>
<Error>{t(`error:${error}`)}</Error>
</Message.Body>
</Message>
) : (
<fieldset>
<PasswordSelection
isLoading={false}
password={password}
confirmPassword={confirmPassword}
handleChange={handleChange}
/>
<Button type="submit" color="primary" disabled={isDisabled}>
{t('settings:changePassword')}
</Button>
</fieldset>
)}
</Column>
</Column.Group>
</Column>
</Column.Group>
</Form>
);
}