Skip to content

Commit

Permalink
Forgot password mail check exist or not
Browse files Browse the repository at this point in the history
  • Loading branch information
VinayLodhi1712 committed Oct 30, 2024
1 parent b6adaee commit de68473
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
7 changes: 4 additions & 3 deletions client/src/component/ForgotPassword.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const ForgotPassword = ({ showAlert, mode }) => {
const [forgotEmail, setForgotEmail] = useState("");
const [loading, setLoading] = useState(false);
const navigate = useNavigate();

const handleForgotPassword = async () => {
setLoading(true);
try {
Expand All @@ -21,14 +20,15 @@ const ForgotPassword = ({ showAlert, mode }) => {
},
body: JSON.stringify({ email: forgotEmail }),
});

const data = await response.json();

if (response.ok) {
toast.success(data.message || "Reset email sent successfully!");
setForgotEmail("");
navigate("/login");
} else {
showAlert(data.message || "Failed to send reset email", "danger");
showAlert(data.error || "Failed to send reset email", "danger");
}
} catch (error) {
console.error("Error during password reset:", error);
Expand All @@ -37,6 +37,7 @@ const ForgotPassword = ({ showAlert, mode }) => {
setLoading(false);
}
};


return (
<div className="flex justify-center h-[85vh] items-center mt-[7rem]">
Expand Down
31 changes: 21 additions & 10 deletions server/Controllers/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,22 @@ const verifyToken = async (req, res) => {
}
};

async function ResetPasswordByEmail(req, resp) {
const VITE_CLIENT_PORT = process.env.VITE_CLIENT_PORT || "https://bitbox-in.netlify.app";
async function ResetPasswordByEmail(req, res) {

const VITE_CLIENT_PORT = process.env.VITE_CLIENT_PORT || "https://bitbox-in.netlify.app";
const { email } = req.body;

// Check if email is provided
if (!email) {
return res.status(400).json({ error: 'Email is required' });
}

const user = await User.findOne({ email });

if (!user) {
return res.status(404).json({ error: 'User does not exist' });
}

const transporter = nodemailer.createTransport({
service: "gmail",
auth: {
Expand All @@ -112,24 +124,23 @@ async function ResetPasswordByEmail(req, resp) {
const mailOptions = {
from: process.env.EMAIL_USER,
to: email,
subject: "Reset Your password on BitBox",
subject: "Reset Your Password on BitBox",
html: `
<p>Reset your password using the link below:</p>
<a href="${VITE_CLIENT_PORT}/reset-password"><button>Click here</button></a> to reset your password
`,
<p>Reset your password using the link below:</p>
<a href="${VITE_CLIENT_PORT}/reset-password"><button>Click here</button></a> to reset your password
`,
};

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


const forgetpassword = async (req, res) => {
try {
const { email, password } = req.body;
Expand Down

0 comments on commit de68473

Please sign in to comment.