Skip to content

Commit

Permalink
Merge pull request #1537 from AyushSharma72/reset
Browse files Browse the repository at this point in the history
added reset password functionality
  • Loading branch information
ANSHIKA-26 authored Oct 29, 2024
2 parents 52c28d1 + 2be99b3 commit a709a44
Show file tree
Hide file tree
Showing 6 changed files with 449 additions and 109 deletions.
123 changes: 123 additions & 0 deletions backend/controllers/userController.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import User from "../models/user.js";
import jwt from "jsonwebtoken";
import dotenv from "dotenv";
import nodemailer from "nodemailer";
import bcrypt from "bcryptjs";
dotenv.config();

// Generate JWT token
Expand Down Expand Up @@ -99,3 +101,124 @@ export const getAllUsers = async (req, res) => {
res.status(500).json({ message: error.message });
}
};

export function ForgotPassWordEmail(req, resp) {
const transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: "your email id",
pass: "your password",
},
});

const mailOptions = {
from: "your email id",
to: req.body.Email,
subject: "WordWise Reset Password",
html: `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Password Reset</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f7f7f7;
color: #333;
margin: 0;
padding: 0;
}
.email-container {
width: 100%;
max-width: 600px;
margin: 0 auto;
background-color: #ffffff;
padding: 20px;
border-radius: 8px;
box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1);
}
h2 {
color: #333;
}
p {
font-size: 16px;
line-height: 1.5;
color: #666;
}
.button-container {
text-align: center;
margin-top: 20px;
}
.button {
background-color: #4CAF50;
color: #ffffff;
padding: 12px 20px;
border: none;
border-radius: 4px;
text-decoration: none;
font-size: 16px;
cursor: pointer;
transition: background-color 0.3s ease;
}
.button:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<div class="email-container">
<h2>Password Reset Request</h2>
<p>Hello,</p>
<p>We received a request to reset your password. Click the button below to proceed with resetting your password.</p>
<div class="button-container">
<a href="https://wordwise-k9ho.onrender.com/resetpass.html" class="button">Reset Password</a>
</div>
<p>Thank you,<br>Team WordWise</p>
</div>
</body>
</html>
`,
};

transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log("Error sending email: " + error);
resp.status(500).send("Error sending email");
} else {
console.log("Email sent: " + info.response);
resp.status(200).send("Form data sent successfully");
}
});
}

export async function ResetPassword(req, resp) {
try {
const { password, email } = req.body;

const user = await User.findOne({ email });
if (!user) {
return resp
.status(404)
.json({ success: false, message: "User not found" });
}

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

user.password = hashedPassword;
await user.save();

resp
.status(200)
.json({ success: true, message: "Password reset successfully" });
} catch (error) {
console.error("Error resetting password:", error);
resp.status(500).json({
success: false,
message: "An error occurred. Please try again later.",
});
}
}
12 changes: 11 additions & 1 deletion backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"express-validator": "^7.2.0",
"jsonwebtoken": "^9.0.2",
"mongoose": "^8.7.1",
"multer": "^1.4.5-lts.1"
"multer": "^1.4.5-lts.1",
"nodemailer": "^6.9.16"
},
"devDependencies": {
"nodemon": "^3.1.7"
Expand Down
6 changes: 6 additions & 0 deletions backend/routes/userRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
loginUser,
getUserProfile,
getAllUsers,
ForgotPassWordEmail,
ResetPassword,
} from "../controllers/userController.js";
import authMiddleware from "../middlewares/authMiddleware.js";
import adminMiddleware from "../middlewares/adminMiddleware.js";
Expand All @@ -20,4 +22,8 @@ router.get("/profile", authMiddleware, getUserProfile);
// Admin route to get all users
router.get("/", authMiddleware, adminMiddleware, getAllUsers);

router.post("/forgotpassword", ForgotPassWordEmail);

router.post("/resetpassword", ResetPassword);

export default router;
Loading

1 comment on commit a709a44

@vercel
Copy link

@vercel vercel bot commented on a709a44 Oct 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.