generated from Code-the-Dream-School/react-node-practicum-front-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from Code-the-Dream-School/reset-password-page…
…-feature Reset password page feature
- Loading branch information
Showing
4 changed files
with
282 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,200 @@ | ||
import React, { useState } from "react"; | ||
import { VStack, Box, Stack, Button, Text, Group } from "@chakra-ui/react"; | ||
import { PasswordInput } from "./ui/password-input"; | ||
import { Field } from "./ui/field"; | ||
import { Toaster, toaster } from "../components/ui/toaster"; | ||
import { Alert } from "../components/ui/alert"; | ||
import { useNavigate } from "react-router-dom"; | ||
import "../styles/ResetPassword.css"; | ||
|
||
const ResetPassword = () => { | ||
const [loading, setLoading] = useState(false); | ||
const [message, setMessage] = useState(""); | ||
const navigate = useNavigate(); | ||
const token = new URLSearchParams(window.location.search).get("token"); | ||
const [errorMessage, setErrorMessage] = useState(""); | ||
const [userData, setUserData] = useState({ | ||
password_hash: "", | ||
confirmPassword: "", | ||
}); | ||
const [errors, setErrors] = useState({ | ||
password_hash: false, | ||
confirmPassword: false, | ||
}); | ||
const handleChange = (e) => { | ||
const { name, value } = e.target; | ||
setUserData((prevData) => ({ | ||
...prevData, | ||
[name]: value, | ||
})); | ||
|
||
// Clear error message on input change | ||
if (name === "password_hash" || name === "confirmPassword") { | ||
setErrorMessage(""); | ||
} | ||
}; | ||
const handleBlur = (e) => { | ||
const { name, value } = e.target; | ||
// Clear the error if the input is not empty | ||
if (value.trim()) { | ||
setErrors((prevErrors) => ({ | ||
...prevErrors, | ||
[name]: false, | ||
})); | ||
} | ||
}; | ||
const handleSubmit = async (e) => { | ||
e.preventDefault(); | ||
setLoading(true); | ||
setMessage(""); | ||
// check if all fields are filled | ||
const { password_hash, confirmPassword } = userData; | ||
const newErrors = { | ||
password_hash: !password_hash, | ||
confirmPassword: !confirmPassword, | ||
}; | ||
setErrors(newErrors); | ||
|
||
// Check if passwords match | ||
if (password_hash !== confirmPassword) { | ||
setErrors((prevErrors) => ({ | ||
...prevErrors, | ||
confirmPassword: true, | ||
})); | ||
setErrorMessage("Passwords do not match!"); | ||
setLoading(false); | ||
return; | ||
} | ||
// If any field is missing, prevent user form submission | ||
if (Object.values(newErrors).some((error) => error)) { | ||
setLoading(false); | ||
setMessage("all field is required"); | ||
return; | ||
} | ||
try { | ||
console.log(token); | ||
console.log(userData.password_hash); | ||
const response = await fetch( | ||
`http://localhost:8001/api/v1/reset-password/${token}`, | ||
{ | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ password: userData.password_hash }), | ||
} | ||
); | ||
|
||
if (response.ok) { | ||
setMessage( | ||
"Password reset successful, You can now login using the correct credentials!" | ||
); | ||
toaster.create({ | ||
title: "Password reset", | ||
description: message, | ||
type: "success", | ||
duration: 4000, | ||
action: { | ||
label: "x", | ||
}, | ||
}); | ||
navigate("/Home"); | ||
} else { | ||
const errorData = await response.json(); | ||
setMessage(errorData.message || "Error resetting password."); | ||
console.error("Error resetting password"); | ||
toaster.create({ | ||
title: "Error resetting password", | ||
description: message, | ||
type: "error", | ||
action: { | ||
label: "x", | ||
}, | ||
}); | ||
} | ||
} catch (error) { | ||
setMessage("An unexpected error occurred."); | ||
console.error("An unexpected error occurred."); | ||
toaster.create({ | ||
title: "An unexpected error occurred.", | ||
type: "error", | ||
action: { | ||
label: "x", | ||
}, | ||
}); | ||
} finally { | ||
setLoading(false); | ||
} | ||
}; | ||
|
||
return ( | ||
<> | ||
<Group grow marginTop={"200px"}> | ||
<VStack> | ||
<Box> | ||
<Text | ||
fontWeight="800" | ||
fontSize="30px" | ||
letterSpacing="0.9px" | ||
color="white" | ||
marginTop={10} | ||
> | ||
Reset Password | ||
</Text> | ||
</Box> | ||
<Box> | ||
<Field w="308px" marginTop="10px" label="New Password" required> | ||
<PasswordInput | ||
placeholder="Password" | ||
name="password_hash" | ||
value={userData.password_hash} | ||
onChange={handleChange} | ||
onBlur={handleBlur} | ||
className={errors.password_hash ? "error" : ""} | ||
/> | ||
</Field> | ||
<Field w="308px" marginTop="10px" label="Confirm Password" required> | ||
<PasswordInput | ||
placeholder="Confirm Password" | ||
name="confirmPassword" | ||
value={userData.confirmPassword} | ||
onChange={handleChange} | ||
onBlur={handleBlur} | ||
className={errors.confirmPassword ? "error" : ""} | ||
/> | ||
</Field> | ||
<br /> | ||
{errorMessage && ( | ||
<Stack gap="2" width="308px"> | ||
<Alert status="error" title={errorMessage} /> | ||
</Stack> | ||
)} | ||
{/* Display error message */} | ||
<br /> | ||
<Button | ||
backgroundColor="#CF64EE" | ||
width="308px" | ||
h="50px" | ||
borderRadius="100px" | ||
marginBottom="25px" | ||
onClick={handleSubmit} | ||
> | ||
<Text | ||
fontWeight="800" | ||
fontSize="16px" | ||
letterSpacing="0.9px" | ||
color="white" | ||
> | ||
{loading ? "Resetting..." : "Reset Password"} | ||
</Text> | ||
</Button> | ||
</Box> | ||
</VStack> | ||
</Group> | ||
|
||
<Toaster /> | ||
</> | ||
); | ||
}; | ||
|
||
export default ResetPassword; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
.reset-password-container { | ||
top: 50%; | ||
left: 50%; | ||
transform: translate(-50%, -50%); | ||
} | ||
|
||
.reset-password-container h2 { | ||
margin-bottom: 20px; | ||
font-size: 1.5rem; | ||
} | ||
|
||
.reset-password-container form { | ||
display: flex; | ||
flex-direction: column; | ||
width: 100%; | ||
} | ||
|
||
.reset-password-container input { | ||
margin-bottom: 15px; | ||
padding: 10px; | ||
font-size: 1rem; | ||
border: 1px solid #ccc; | ||
border-radius: 4px; | ||
} | ||
|
||
.reset-password-container button { | ||
padding: 10px; | ||
font-size: 1rem; | ||
color: white; | ||
background-color: #007bff; | ||
border: none; | ||
border-radius: 4px; | ||
cursor: pointer; | ||
} | ||
|
||
.reset-password-container button:disabled { | ||
background-color: #ccc; | ||
cursor: not-allowed; | ||
} | ||
|
||
.reset-password-container p { | ||
margin-top: 10px; | ||
color: green; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters