Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate Change Password #126

Merged
merged 2 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 39 additions & 23 deletions back-end/routes/forgotPasswordRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,52 @@ const express = require("express");
const router = express.Router();
const { User } = require("../models/User.js");
const bcrypt = require("bcrypt");
const { body, validationResult } = require("express-validator");

router.post(
"/",
[
body("email").not().isEmpty().withMessage("Email is required"),
body("username").not().isEmpty().withMessage("Username is required"),
body("newPassword").not().isEmpty().withMessage("New Password is required"),
],
async (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ success: false, errors: errors.array() });
}

router.post("/", async (req, res) => {
const username = req.body.username;
const newPassword = req.body.newPassword;
const email = req.body.email;
const username = req.body.username;
const newPassword = req.body.newPassword;
const email = req.body.email;

try {
const user = await User.findOne({
$and: [{ $or: [{ username: username }, { email: email }] }],
});
try {
const user = await User.findOne({
$and: [{ $or: [{ username: username }, { email: email }] }],
});

console.log(user);
console.log(user);

if (!user) {
console.error(`User not found.`);
return res
.status(404)
.json({ success: false, message: "User not found." });
}
if (!user) {
console.error(`User not found.`);
return res
.status(404)
.json({ success: false, message: "User not found." });
}

const hashedPassword = await bcrypt.hash(newPassword, 10);
user.password = hashedPassword;
const hashedPassword = await bcrypt.hash(newPassword, 10);
user.password = hashedPassword;

await user.save();
await user.save();

res.json({ success: true, message: "Password updated successfully." });
} catch (error) {
console.error(`Error updating password: ${error}`);
res.status(500).json({ success: false, message: "Internal server error." });
res.json({ success: true, message: "Password updated successfully." });
} catch (error) {
console.error(`Error updating password: ${error}`);
res
.status(500)
.json({ success: false, message: "Internal server error." });
}
}
});
);

module.exports = router;
5 changes: 5 additions & 0 deletions back-end/routes/signupRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ router.post(
],

async (req, res, next) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ success: false, errors: errors.array() });
}

const username = req.body.username;
const password = req.body.password;
const email = req.body.email;
Expand Down
2 changes: 1 addition & 1 deletion front-end/src/components/ForgotPassword.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const ForgotPassword = () => {
Email
</label>
<input
type="text"
type="email"
id="email"
name="email"
value={formData.email}
Expand Down
3 changes: 2 additions & 1 deletion front-end/src/components/Login.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ const Login = () => {
<h2 className="login-title">Login</h2>
{showNotification && (
<div className="notification">
<p>You have been successfully registered. Please log in.</p>
{/* <p>You have been successfully registered. Please log in.</p> */}
<p>Please log in again!</p>
</div>
)}
{errorMessage ? <p className="error">{errorMessage}</p> : ""}
Expand Down