From 02300ab822ff22aac91e09fa61748e9b0272a425 Mon Sep 17 00:00:00 2001 From: madeline-lei Date: Sat, 7 Dec 2024 16:34:33 -0500 Subject: [PATCH 1/5] progress 12/7 --- package-lock.json | 7 ++++--- src/api/user-wrapper.js | 11 +++++++++++ src/components/Form/Form.jsx | 2 +- src/components/Form/FormInput.jsx | 2 +- src/components/Form/FormSubmit.jsx | 2 +- src/components/NavBar/NavBar.jsx | 4 +++- src/pages/ForgotPassword.jsx | 17 +++++++++++++++++ src/pages/Login.jsx | 12 +++++++----- 8 files changed, 45 insertions(+), 12 deletions(-) create mode 100644 src/pages/ForgotPassword.jsx diff --git a/package-lock.json b/package-lock.json index a56bb88..baa2360 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4261,9 +4261,10 @@ } }, "node_modules/mongoose": { - "version": "8.8.2", - "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-8.8.2.tgz", - "integrity": "sha512-jCTSqDANfRzk909v4YoZQi7jlGRB2MTvgG+spVBc/BA4tOs1oWJr//V6yYujqNq9UybpOtsSfBqxI0dSOEFJHQ==", + "version": "8.8.3", + "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-8.8.3.tgz", + "integrity": "sha512-/I4n/DcXqXyIiLRfAmUIiTjj3vXfeISke8dt4U4Y8Wfm074Wa6sXnQrXN49NFOFf2mM1kUdOXryoBvkuCnr+Qw==", + "license": "MIT", "dependencies": { "bson": "^6.7.0", "kareem": "2.6.3", diff --git a/src/api/user-wrapper.js b/src/api/user-wrapper.js index 74650b0..8859841 100644 --- a/src/api/user-wrapper.js +++ b/src/api/user-wrapper.js @@ -37,6 +37,17 @@ const getUsers = async () => { } } +//added 12/7 +const getUserPassword = async () => { + try { + const response = await fetch('http://localhost:4000/api/users/password') + const jsonData = await response.json() + return jsonData + } catch (error) { + console.error('Error fetching data:', error); + } +} + export { postUser, postLogin, diff --git a/src/components/Form/Form.jsx b/src/components/Form/Form.jsx index 8cb6802..0966745 100644 --- a/src/components/Form/Form.jsx +++ b/src/components/Form/Form.jsx @@ -1,7 +1,7 @@ const Form = ({ children, width }) => { return ( -
+
{children}
) diff --git a/src/components/Form/FormInput.jsx b/src/components/Form/FormInput.jsx index 771b5f9..d0eb76c 100644 --- a/src/components/Form/FormInput.jsx +++ b/src/components/Form/FormInput.jsx @@ -1,6 +1,6 @@ const FormInput = ({ type, name, placeholder, value, onChange, isRequired }) => { - const styles = "w-full p-3 border border-gray-400 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-300 placeholder-gray-500"; + const styles = "w-full py-3 px-4 border border-gray-400 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-300 placeholder-gray-500 placeholder:text-lg"; return ( type === "textarea" ? diff --git a/src/components/Form/FormSubmit.jsx b/src/components/Form/FormSubmit.jsx index dfa54ee..5f49b6c 100644 --- a/src/components/Form/FormSubmit.jsx +++ b/src/components/Form/FormSubmit.jsx @@ -4,7 +4,7 @@ const FormSubmit = ({ label, isDisabled }) => { return (
+

Forgot password?

+ From 23a307f969b6a409c60178d5ef9a1d6c3cd46968 Mon Sep 17 00:00:00 2001 From: madeline-lei Date: Tue, 10 Dec 2024 19:14:53 -0500 Subject: [PATCH 3/5] finished --- api/index.js | 25 +++++++++++++ src/api/user-wrapper.js | 20 ++++++++++- src/pages/ForgotPassword.jsx | 69 +++++++++++++++++++++++++----------- src/pages/Login.jsx | 3 +- src/pages/SignUp.jsx | 7 ++-- 5 files changed, 99 insertions(+), 25 deletions(-) diff --git a/api/index.js b/api/index.js index ff2e5ee..c7567f6 100644 --- a/api/index.js +++ b/api/index.js @@ -311,3 +311,28 @@ app.put('/api/users/:id/unenroll', async (req, res) => { res.status(500).json({ message: 'Error unenrolling into class' }) } }) + +//Forgot Password +app.post('/api/users/reset-password', async (req, res) => { + const { username, password } = req.body; + const user = await User.findOne({ username }); + try{ + if (user) { + const user = { username: username }; + const updatedPassword = { password: password }; + const options = { returnDocument: 'after' }; + await User.findOneAndUpdate(user, updatedPassword, options); + + res.status(200).send("Password updated successfully."); + + } else { + console.log('Login failed: User not found'); + res.status(401).send('Invalid username.'); + } + } catch (err){ + console.error('Error resetting password'); + res.status(500).send("Server error resetting password."); + } + + +}); diff --git a/src/api/user-wrapper.js b/src/api/user-wrapper.js index 8859841..0e941c9 100644 --- a/src/api/user-wrapper.js +++ b/src/api/user-wrapper.js @@ -44,7 +44,23 @@ const getUserPassword = async () => { const jsonData = await response.json() return jsonData } catch (error) { - console.error('Error fetching data:', error); + console.error('Error fetching user password:', error); + } +} + +const resetPassword = async (body) => { + try { + const response = await fetch(apiUrl("/api/users/reset-password"), { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(body), + }) + + return response + } catch (error) { + console.error('Reset password endpoint post error:', error); } } @@ -52,4 +68,6 @@ export { postUser, postLogin, getUsers, + getUserPassword, + resetPassword } \ No newline at end of file diff --git a/src/pages/ForgotPassword.jsx b/src/pages/ForgotPassword.jsx index fa2368e..383eb92 100644 --- a/src/pages/ForgotPassword.jsx +++ b/src/pages/ForgotPassword.jsx @@ -1,6 +1,6 @@ import { useState } from "react"; -import { Link } from "wouter"; -import { postLogin } from "../api/user-wrapper"; +import { resetPassword } from "../api/user-wrapper"; +import PasswordChecklist from "react-password-checklist" import Form from "@/components/Form/Form"; import FormInput from '@/components/Form/FormInput'; import FormSubmit from "../components/Form/FormSubmit"; @@ -25,37 +25,42 @@ export default function ForgotPassword() { const [formData, setFormData] = useState({ username: '', password: '', + retypedPassword: '' }) const handleChange = (e) => { setFormData({ ...formData, [e.target.name]: e.target.value }); }; + const [isValid, setIsValid] = useState(false); const handleSubmit = async (e) => { e.preventDefault(); + const { username, password, retypedPassword } = formData + if (password != retypedPassword) { + alert(`Passwords do not match:\npassword: ${password}\nretyped password: ${retypedPassword}`) + } else { + try { + const response = await resetPassword(formData) - try { - const response = await postLogin(formData) - - if (response.ok) { - const message = await response.text(); - console.log(message); - alert("Login successful!"); - } else { - const errorMessage = await response.text(); - console.error(errorMessage); - alert("Login failed: " + errorMessage); + if (response.ok) { + const message = await response.text(); + console.log(message); + alert("Reset successful!"); + } else { + const errorMessage = await response.text(); + console.error(errorMessage); + alert("Reset failed: " + errorMessage); + } + } catch (error) { + alert('An error occurred while resetting the password.') } - } catch (error) { - console.error('Error during login: ', error); - alert("An error occurred during login."); } }; return ( <>
-
+

Forgot Your Password?

+ placeholder="New Password" + isRequired={true} /> + + + +
+ setIsValid(isValid)} + /> +
- +
diff --git a/src/pages/Login.jsx b/src/pages/Login.jsx index de8a79b..45d7167 100644 --- a/src/pages/Login.jsx +++ b/src/pages/Login.jsx @@ -40,7 +40,8 @@ export default function Login() { return ( <> -
+

Login

Don't have an account? diff --git a/src/pages/SignUp.jsx b/src/pages/SignUp.jsx index 0c43dcb..e8a0648 100644 --- a/src/pages/SignUp.jsx +++ b/src/pages/SignUp.jsx @@ -43,13 +43,14 @@ export default function SignUp() { } return ( -
+

Sign up

Already have an account? - Log In + Log In

{/* Form Values and the Borders */} @@ -119,7 +120,7 @@ export default function SignUp() { onChange={(isValid) => setIsValid(isValid)} />
-

Forgot password?

+

Forgot password?

From 6c7e1bf2426cf2f5d0deb61c995769e2d35cd4d6 Mon Sep 17 00:00:00 2001 From: madeline-lei Date: Tue, 10 Dec 2024 19:22:30 -0500 Subject: [PATCH 4/5] whoops forgot some stuff --- src/pages/SignUp.jsx | 20 +------------------- 1 file changed, 1 insertion(+), 19 deletions(-) diff --git a/src/pages/SignUp.jsx b/src/pages/SignUp.jsx index e8a0648..09908ab 100644 --- a/src/pages/SignUp.jsx +++ b/src/pages/SignUp.jsx @@ -49,30 +49,12 @@ export default function SignUp() { width={"w-2/5"} >

Sign up

-

Already have an account? +

Already have an account? Log In

{/* Form Values and the Borders */}
-
- - -
Date: Wed, 11 Dec 2024 18:41:09 -0500 Subject: [PATCH 5/5] minor style changes --- src/api/user-wrapper.js | 6 +++--- src/components/Form/Form.jsx | 2 +- src/components/Form/FormInput.jsx | 2 +- src/pages/ForgotPassword.jsx | 22 ++++++++++------------ src/pages/SignUp.jsx | 2 -- 5 files changed, 15 insertions(+), 19 deletions(-) diff --git a/src/api/user-wrapper.js b/src/api/user-wrapper.js index 0e941c9..5418d27 100644 --- a/src/api/user-wrapper.js +++ b/src/api/user-wrapper.js @@ -29,7 +29,7 @@ const postLogin = async (body) => { const getUsers = async () => { try { - const response = await fetch('http://localhost:4000/api/users') + const response = await fetch('/api/users') const jsonData = await response.json() // Converting data to json return jsonData } catch (error) { @@ -40,8 +40,8 @@ const getUsers = async () => { //added 12/7 const getUserPassword = async () => { try { - const response = await fetch('http://localhost:4000/api/users/password') - const jsonData = await response.json() + const response = await fetch('/api/users/password') + const jsonData = await response.json() return jsonData } catch (error) { console.error('Error fetching user password:', error); diff --git a/src/components/Form/Form.jsx b/src/components/Form/Form.jsx index 0966745..8cb6802 100644 --- a/src/components/Form/Form.jsx +++ b/src/components/Form/Form.jsx @@ -1,7 +1,7 @@ const Form = ({ children, width }) => { return ( -
+
{children}
) diff --git a/src/components/Form/FormInput.jsx b/src/components/Form/FormInput.jsx index d0eb76c..96b4d05 100644 --- a/src/components/Form/FormInput.jsx +++ b/src/components/Form/FormInput.jsx @@ -1,6 +1,6 @@ const FormInput = ({ type, name, placeholder, value, onChange, isRequired }) => { - const styles = "w-full py-3 px-4 border border-gray-400 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-300 placeholder-gray-500 placeholder:text-lg"; + const styles = "w-full py-3 px-4 border border-gray-400 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-300 placeholder-gray-500"; return ( type === "textarea" ? diff --git a/src/pages/ForgotPassword.jsx b/src/pages/ForgotPassword.jsx index 383eb92..5eba3ef 100644 --- a/src/pages/ForgotPassword.jsx +++ b/src/pages/ForgotPassword.jsx @@ -8,15 +8,13 @@ import FormSubmit from "../components/Form/FormSubmit"; //Fetching first const getUserPassword = async () => { - try { - const response = await fetch('http://localhost:4000/api/users/password') - const jsonData = await response.json() // Converting data to json - return jsonData - } catch (error) { - console.error('Error fetching data:', error); - } + try { + const response = await getUserPassword(); + return response; + } catch (error) { + console.error('Error fetching data:', error); } - +} //Updating the person's password property @@ -52,7 +50,7 @@ export default function ForgotPassword() { alert("Reset failed: " + errorMessage); } } catch (error) { - alert('An error occurred while resetting the password.') + alert('An error occurred while resetting the password.') } } }; @@ -90,7 +88,7 @@ export default function ForgotPassword() { onChange={handleChange} placeholder="Retype New Password" isRequired={true} /> - +
setIsValid(isValid)} /> -
+
- +

diff --git a/src/pages/SignUp.jsx b/src/pages/SignUp.jsx index 09908ab..c4997da 100644 --- a/src/pages/SignUp.jsx +++ b/src/pages/SignUp.jsx @@ -102,8 +102,6 @@ export default function SignUp() { onChange={(isValid) => setIsValid(isValid)} /> -

Forgot password?

-