diff --git a/backend/routes/users.js b/backend/routes/users.js index 7a1945c04..e57ee8722 100644 --- a/backend/routes/users.js +++ b/backend/routes/users.js @@ -112,6 +112,10 @@ router.put("/:id", async (req, res) => { user.emailVerified = req.body.emailVerified; } + if (req.body.updatePasswordToken) { + user.updatePasswordToken = req.body.updatePasswordToken; + } + await user.save(); res.send({ message: "User role updated" }); } catch (error) { diff --git a/client/src/components/NavBar/index.jsx b/client/src/components/NavBar/index.jsx index 09916722f..dbde8a0f5 100644 --- a/client/src/components/NavBar/index.jsx +++ b/client/src/components/NavBar/index.jsx @@ -160,7 +160,7 @@ const CompleteNavbar = () => { {/* Mapped Route for Changing Passwords */} {users.map((user) => ( - } /> ))} diff --git a/client/src/views/ChangePassword.jsx b/client/src/views/ChangePassword.jsx index 4319a864d..c87855183 100644 --- a/client/src/views/ChangePassword.jsx +++ b/client/src/views/ChangePassword.jsx @@ -1,23 +1,114 @@ import axios from "axios"; import React from "react"; import swal from 'sweetalert2'; -import { Button } from '@mui/material' export default class verifyEmail extends React.Component { constructor(props) { super(props); this.state = { newPassword: "", + newPassword2: "" }; } + handleChange = (e) => { + const { name, value } = e.target; + this.setState((state) => ({ + [name]: value + })); + }; + handleSubmit = e => { e.preventDefault(); - // this is where the change to TRUE for validated emails goes + try { + // temporary for right now but later redo this to use put commands instead of post + const url = "/api/users/" + this.props.user._id; + + // hash the new password here + let tempUser = { password: this.state.newPassword }; + if (tempUser.password.trim().length < 6) { + swal.fire({ + icon: 'error', + title: 'Password too short' + }); + return; + } + + if (this.state.newPassword !== this.state.newPassword2) { + swal.fire({ + icon: 'error', + title: 'Passwords do not match' + }); + return; + } + + // axios call to update user + axios.put(url, tempUser).then(res => { + if (res.status === 200) { + swal.fire({ + icon: 'success', + title: 'Successfully Updated User' + }); + } + }); + + } catch (error) { + // show error + console.log(error); + swal.fire({ + icon: 'error', + title: 'Try Again Later', + text: error.text, + }); + } } render() { return ( +
+
+ Update Password +
+ +
+
+
+ + +
+
+
+
+ + +
+
+ +
+ +
+
+
) } diff --git a/client/src/views/Login.jsx b/client/src/views/Login.jsx index a29ef1ab8..3b47a0d0f 100644 --- a/client/src/views/Login.jsx +++ b/client/src/views/Login.jsx @@ -74,16 +74,24 @@ const Login = () => { const putURL = '/api/users/' + res.data._id; axios.put(putURL, res.data).then( emailjs.send( - "serviceID", - "templateID", + "service_trb6232", + "template_jetptvm", { - link: "http://bookstore-app.herokuapp.com/forgot/" + res.data.updatePasswordToken; + pw_link: "http://bookstore-app.herokuapp.com/forgot/" + res.data.updatePasswordToken, + to_email: email, + to_name: res.data.firstName, }, - "password/key for email js" + "0v71YVpIY79kGX06h" ).then( - // navigate them back to the home page + swal.fire( + 'Email successfully sent', + "Check your email for the link to reset your password", + 'success' + ) ) - ) + ).catch((error) => { + console.log(error); + }) }).catch((error) => { if (error.response.status === 404) { swal.fire(