Skip to content

Commit 119fd94

Browse files
committed
Implement resetPassword
1 parent 71c02fa commit 119fd94

1 file changed

Lines changed: 62 additions & 1 deletion

File tree

src/ResetPassword/resetPassword.js

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@ import accountForm from "../AccountForm/accountForm.module.css";
33
import Layout from "../Layout/layout";
44
import FormField from "../FormField/formField";
55
import { useState } from "react";
6+
import { CONFIRMED_STATE } from "../lib/states";
7+
import { useHistory } from "react-router";
68

79
export default function ResetPassword(props) {
810
const [newPassword, setNewPassword] = useState("");
911
const [confirmNewPassword, setConfirmNewPassword] = useState("");
12+
const getParams = new URLSearchParams(
13+
document.location.search.substring(1)
14+
);
15+
const history = useHistory("");
1016

1117
const handleNewPasswordChange = e => {
1218
setNewPassword(e.target.value);
@@ -16,9 +22,64 @@ export default function ResetPassword(props) {
1622
setConfirmNewPassword(e.target.value);
1723
};
1824

25+
const handleResetPasswordSubmit = (e, setConfirmed, setErrorMessage) => {
26+
if (newPassword !== confirmNewPassword) {
27+
setErrorMessage(
28+
"The new password and confirm new password fields must match"
29+
);
30+
setConfirmed(CONFIRMED_STATE.FAILURE);
31+
return;
32+
}
33+
34+
const apiKey = getParams.get("key");
35+
const activationCode = getParams.get("code");
36+
const email = getParams.get("email");
37+
38+
fetch(`https://api.borumtech.com/api/login`, {
39+
method: "PUT",
40+
headers: {
41+
"content-type": "application/x-www-form-urlencoded",
42+
authorization: `Basic ${apiKey}`,
43+
},
44+
body: `new_password=${newPassword}&code=${activationCode}&email=${email}`,
45+
})
46+
.then(response => response.json())
47+
.then(response => {
48+
if (response.statusCode >= 200 && response.statusCode < 300) {
49+
setConfirmed(CONFIRMED_STATE.SUCCESS);
50+
history.push("/login");
51+
} else if (response.statusCode === 500) {
52+
throw new Error(
53+
"The email could not be sent at this time. Please contact support to change your password"
54+
);
55+
} else {
56+
throw new Error(response.error.message);
57+
}
58+
})
59+
.catch(err => {
60+
let { message } = err;
61+
62+
if (err.name !== "Error") {
63+
message =
64+
"A system error occurred and you could not be logged in at this time. Please try again another time.";
65+
}
66+
67+
console.error(err);
68+
setErrorMessage(message);
69+
setConfirmed(CONFIRMED_STATE.FAILURE);
70+
71+
setConfirmNewPassword("");
72+
setNewPassword("");
73+
});
74+
};
75+
1976
return (
2077
<Layout>
21-
<AccountForm heading="Reset your Password">
78+
<AccountForm
79+
heading="Reset your Password"
80+
handleSubmit={handleResetPasswordSubmit}
81+
failedAction=" and your password could not be changed"
82+
>
2283
<FormField
2384
autofocus
2485
label="newpass"

0 commit comments

Comments
 (0)