Skip to content

Commit

Permalink
Merge pull request #129 from Laxelspal/origine-main
Browse files Browse the repository at this point in the history
added the contact us page functionality in the user dashboard
  • Loading branch information
Sahil1786 committed Jun 5, 2024
2 parents b3aa5f3 + b6627f4 commit 6fd9945
Show file tree
Hide file tree
Showing 13 changed files with 407 additions and 20 deletions.
2 changes: 2 additions & 0 deletions model/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const AdminSchema = new mongoose.Schema({
password: String,
fullName: String,
Mobile: Number,
resetTokenExpiration: Date,
resetToken:String
})

const Admin = mongoose.model('Admin', AdminSchema);
Expand Down
4 changes: 3 additions & 1 deletion model/ngo.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ const ngoRegisterSchema = new mongoose.Schema({
Mobile: { type: String, required: true },
NgoID: { type: String, required: true },
NgoLocation: { type: String, required: true },
approved: { type: Boolean, default: false } // New field for approval status
approved: { type: Boolean, default: false },
resetTokenExpiration: Date,
resetToken:String // New field for approval status
});


Expand Down
163 changes: 162 additions & 1 deletion routers/NgoRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const bcrypt = require("bcrypt");
const saltRounds = 10;
const jwt = require("jsonwebtoken");

const transporter = require("../helpers/emailHelpers");
const {transporter} = require("../helpers/emailHelpers");

const User = require("../model/user");
const Admin = require("../model/admin");
Expand Down Expand Up @@ -51,6 +51,7 @@ router.post("/NGO-login", async (req, res) => {
router.post("/NGO-Registarion", async (req, res) => {
// Check if the NGO already exists
const existingNGO = await NGO.findOne({ username: req.body.username });
console.log("existed ngo",existingNGO);
if (existingNGO) {
return res.status(400).json({ error: "NGO already exists" });
}
Expand Down Expand Up @@ -112,6 +113,8 @@ router.post("/NGO-Registarion", async (req, res) => {
console.error("Error creating NGO:", err);
res.status(500).json({ error: "Internal server error" });
}


// try {
// await newNGO.save();

Expand Down Expand Up @@ -188,4 +191,162 @@ router.post("/NGO-Registarion", async (req, res) => {
// });
});

router.route("/forgot-password-ngo").get(async (req, res) => {
res.render("forget-password",{role:"ngo"});
});

//send Email for the reset password
router.route("/forgot-password-ngo").post(async (req, res) => {
const { email } = req.body;
try {
const ngo = await NGO.findOne({ username:email });

if (!ngo) {
return res.send("Ngo Not Exist");
}

// Generate a reset token and save it to the user
const resetToken = jwt.sign(
{ email: ngo.username },
process.env.ACCESS_TOKEN_SECRET,
{ expiresIn: "1h" }
);

ngo.resetTokenExpiration = Date.now() + 300000; // 5 minutes
ngo.resetToken = resetToken;

console.log("ngo after setting ", ngo);
await ngo.save();

// Send the reset link to the user via email
const resetLink = `http://localhost:3000/reset-password-ngo?email=${encodeURIComponent(
ngo.username
)}&token=${encodeURIComponent(resetToken)}`; // Replace with the actual path to your logo
console.log(resetLink);
const mailOptions = {
to: ngo.username,
subject: "Password Reset",
template: "reset-password", // Use the Handlebars template
context: {
ngo: {
fname: ngo.NGOName,
_id: ngo._id,
username: ngo.NGOName,
email: ngo.username,
},
resetLink,
},
attachments: [
{
filename: "logo.png",
path: path.join("public", "img", "logo.png"),
cid: "logo",
},
],
};
console.log("Ngo email:", ngo.username);

transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.error(error);
return res.status(500).send("Error sending reset email");
}
console.log(`Reset email sent: ${info.response}`);
res.send("Password reset link sent successfully");
});
} catch (error) {
console.error(error);
res.status(500).send("Internal Server Error");
}
});

// verify Email and render reset password page
router.route("/reset-password-ngo").get(async (req, res) => {
const { email, token } = req.query;
try {
const ngo = await NGO.findOne({
username:email,
resetToken: token,
resetTokenExpiration: { $gt: Date.now() },
});

if (!ngo) {
return res.status(400).send("Invalid or expired reset token");
}

// Verify the token
try {
const decodedToken = jwt.verify(token, process.env.ACCESS_TOKEN_SECRET);
// Process the decoded token (e.g., extract information from it)
console.log(decodedToken);
// Continue with the reset-password logic
res.render("set_password", { email, token,role:"ngo" });
} catch (error) {
// Handle JWT verification errors
console.error("JWT verification error:", error.message);
// You might want to send an error response or redirect the user
res.status(401).send("Unauthorized");
}
} catch (error) {
console.error(error);
res.status(500).send("Internal Server Error");
}
});

//verify the password
router.route("/reset-password-ngo").post(async (req, res) => {
const { email, token } = req.query;
const { newPassword } = req.body;
// console.log(" User Info",email,token,newPassword);

try {
// Verify the token again
const ngo = await NGO.findOne({
username:email,
resetToken: token,
resetTokenExpiration: { $gt: Date.now() },
});

if (!ngo) {
return res.status(400).send("Invalid or expired reset token");
}

try {
const decodedToken = jwt.verify(token, process.env.ACCESS_TOKEN_SECRET);
// Process the decoded token (e.g., extract information from it)
console.log(decodedToken);

// Update the user's password and reset the resetToken fields
const hash = await bcrypt.hash(newPassword, saltRounds);
ngo.password = hash;
ngo.resetToken = null;
ngo.resetTokenExpiration = null;
await ngo.save();

const dooner = await User.find();

return res.render("NGO-DashBoard", {
fullName: ngo.NGOName,
email: ngo.username,
id: ngo.NGOID,
phoneNo: ngo.Mobile,
address: ngo.NGOLocation,
Donation: dooner,
Pickup: dooner,
complain: "",
});

// Redirect to login page or any other desired page
} catch (error) {
// Handle JWT verification errors
console.error("JWT verification error:", error.message);
// You might want to send an error response or redirect the user
res.status(401).send("Unauthorized");
}
} catch (error) {
console.error(error);
res.status(500).send("Internal Server Error");
}
});

module.exports = router;
161 changes: 161 additions & 0 deletions routers/adminRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,4 +417,165 @@ router.post("/complains-response/:email/:userId/:id", async (req, res) => {
}
});

router.route("/forgot-password-admin").get(async (req, res) => {
res.render("forget-password",{role:"admin"});
});

//send Email for the reset password
router.route("/forgot-password-admin").post(async (req, res) => {
const { email } = req.body;
try {
const admin = await Admin.findOne({ email });
if (!admin) {
return res.send("Admin Not Exist");
}

// Generate a reset token and save it to the user
const resetToken = jwt.sign(
{ email: admin.email },
process.env.ACCESS_TOKEN_SECRET,
{ expiresIn: "1h" }
);

admin.resetTokenExpiration = Date.now() + 300000; // 5 minutes
admin.resetToken = resetToken;

console.log("use after setting ", admin);
await admin.save();

// Send the reset link to the user via email
const resetLink = `http://localhost:3000/reset-password-admin?email=${encodeURIComponent(
admin.email
)}&token=${encodeURIComponent(resetToken)}`; // Replace with the actual path to your logo
console.log(resetLink);
const mailOptions = {
to: admin.email,
subject: "Password Reset",
template: "reset-password", // Use the Handlebars template
context: {
admin: {
fname: admin.fullName,
_id: admin._id,
username: admin.username,
email: admin.email,
},
resetLink,
},
attachments: [
{
filename: "logo.png",
path: path.join("public", "img", "logo.png"),
cid: "logo",
},
],
};
console.log("Admin email:", admin.email);

transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.error(error);
return res.status(500).send("Error sending reset email");
}
console.log(`Reset email sent: ${info.response}`);
res.send("Password reset link sent successfully");
});
} catch (error) {
console.error(error);
res.status(500).send("Internal Server Error");
}
});

// verify Email and render reset password page
router.route("/reset-password-admin").get(async (req, res) => {
const { email, token } = req.query;
try {
const admin = await Admin.findOne({
email,
resetToken: token,
resetTokenExpiration: { $gt: Date.now() },
});

if (!admin) {
return res.status(400).send("Invalid or expired reset token");
}

// Verify the token
try {
const decodedToken = jwt.verify(token, process.env.ACCESS_TOKEN_SECRET);
// Process the decoded token (e.g., extract information from it)
console.log(decodedToken);
// Continue with the reset-password logic
res.render("set_password", { email, token,role:"admin"});
} catch (error) {
// Handle JWT verification errors
console.error("JWT verification error:", error.message);
// You might want to send an error response or redirect the user
res.status(401).send("Unauthorized");
}
} catch (error) {
console.error(error);
res.status(500).send("Internal Server Error");
}
});

//verify the password
router.route("/reset-password-admin").post(async (req, res) => {
const { email, token } = req.query;
const { newPassword } = req.body;
// console.log(" User Info",email,token,newPassword);

try {
// Verify the token again
const admin = await Admin.findOne({
email,
resetToken: token,
resetTokenExpiration: { $gt: Date.now() },
});

if (!admin) {
return res.status(400).send("Invalid or expired reset token");
}

try {
const decodedToken = jwt.verify(token, process.env.ACCESS_TOKEN_SECRET);
// Process the decoded token (e.g., extract information from it)
console.log(decodedToken);

// Update the user's password and reset the resetToken fields
const hash = await bcrypt.hash(newPassword, saltRounds);
admin.password = hash;
admin.resetToken = null;
admin.resetTokenExpiration = null;
await admin.save();

const dooner = await User.find(); // Assuming User is your Mongoose model for users
const ngo = await NGO.find();

//return UNRESOLVED query
const query1 = await problem.find({ answere: { $exists: false } });

return res.render("Admin_DashBoard", {
fullName: admin.fullName,
email: admin.email,
phoneNo: admin.Mobile,
address: admin.address,
NGOname: ngo,
Donername: dooner,
UserName: "sahil114",
complain: query1,
});

// Redirect to login page or any other desired page
} catch (error) {
// Handle JWT verification errors
console.error("JWT verification error:", error.message);
// You might want to send an error response or redirect the user
res.status(401).send("Unauthorized");
}
} catch (error) {
console.error(error);
res.status(500).send("Internal Server Error");
}
});

module.exports = router;
Loading

0 comments on commit 6fd9945

Please sign in to comment.