Skip to content

Commit

Permalink
Merge pull request #154 from KGupta2601/main
Browse files Browse the repository at this point in the history
made changes in deletion logic in server.js, Manager.jsx
  • Loading branch information
jinx-vi-0 authored Nov 9, 2024
2 parents 309ca3f + c314efe commit a3d96c8
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 29 deletions.
35 changes: 26 additions & 9 deletions backend/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,28 @@ app.get("/", async (req, res) => {
app.get("/:id", async (req, res) => {
try {
const { id } = req.params;

// Validate ID format (optional but recommended)
if (!ObjectId.isValid(id)) {
return res.status(400).json({ success: false, message: "Invalid ID format" });
}

const db = client.db(dbName);
const collection = db.collection("passwords");

// Fetch the password item from the database
const item = await collection.findOne({ _id: new ObjectId(id) });

// Check if the password exists
if (!item) {
return res.status(404).json({ success: false, message: "Password not found" });
}

// Split and decrypt the password
const [iv, encryptedData] = item.password.split(':');
const decryptedPassword = decrypt({ iv, encryptedData });

// Return the item with the decrypted password
res.status(200).json({ ...item, password: decryptedPassword });
} catch (error) {
console.error("Error fetching password:", error);
Expand Down Expand Up @@ -173,21 +185,26 @@ app.delete("/:id", async (req, res) => {
const { id } = req.params;

if (!id) {
return res
.status(400)
.json({ success: false, message: "ID is required" });
return res.status(400).json({ success: false, message: "ID is required" });
}

const db = client.db(dbName);
const collection = db.collection("passwords");
const result = await collection.deleteOne({
_id: new ObjectId(id),
});

// Step 1: Find the password by ID
const password = await collection.findOne({ _id: new ObjectId(id) });

// Step 2: Check if the password exists
if (!password) {
return res.status(404).json({ success: false, message: "Password not found" });
}

// Step 3: Proceed to delete the password
const result = await collection.deleteOne({ _id: new ObjectId(id) });

// Step 4: Confirm deletion
if (result.deletedCount === 0) {
return res
.status(404)
.json({ success: false, message: "Password not found" });
return res.status(404).json({ success: false, message: "Password could not be deleted" });
}

res.status(200).json({
Expand Down
62 changes: 42 additions & 20 deletions src/components/Manager.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -166,28 +166,50 @@ const Manager = () => {
};

const deletePassword = async (id) => {
const confirmDelete = confirm(
"Do you really want to delete this password?"
);
const confirmDelete = confirm("Do you really want to delete this password?");

if (confirmDelete) {
setPasswordArray(passwordArray.filter((item) => item._id !== id));
await fetch(`http://localhost:3000/${id}`, {
method: "DELETE",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ id }),
});

toast("Password Deleted!", {
position: "top-right",
autoClose: 5000,
hideProgressBar: false,
closeOnClick: true,
draggable: true,
progress: undefined,
theme: "dark",
});
try {
// Send DELETE request to the server
const response = await fetch(`http://localhost:3000/${id}`, {
method: "DELETE",
headers: { "Content-Type": "application/json" },
});

// Check if the response is OK (status in the range 200-299)
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.message || "Failed to delete password");
}

// Update local state only after successful deletion
setPasswordArray(passwordArray.filter((item) => item._id !== id));

// Show success message
toast("Password Deleted!", {
position: "top-right",
autoClose: 5000,
hideProgressBar: false,
closeOnClick: true,
draggable: true,
progress: undefined,
theme: "dark",
});
} catch (error) {
// Handle errors (e.g., network issues, server errors)
console.error("Error deleting password:", error);
toast.error(`Error: ${error.message}`, {
position: "top-right",
autoClose: 5000,
hideProgressBar: false,
closeOnClick: true,
draggable: true,
progress: undefined,
theme: "dark",
});
}
}
};
};

const editPassword = (id) => {
const passwordToEdit = passwordArray.find((item) => item._id === id);
Expand Down

0 comments on commit a3d96c8

Please sign in to comment.